在JOptionPane中断两行或多行中的消息

try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://"+hostName.getText()+";" + "databaseName="+dbName.getText()+";user="+userName.getText()+";password="+password.getText()+";"; Connection con = DriverManager.getConnection(connectionUrl); if(con!=null){JOptionPane.showMessageDialog(this, "Connection Established");} } catch (SQLException e) { JOptionPane.showMessageDialog(this, e); //System.out.println("SQL Exception: "+ e.toString()); } catch (ClassNotFoundException cE) { //System.out.println("Class Not Found Exception: "+ cE.toString()); JOptionPane.showMessageDialog(this, cE.toString()); } 

当出现错误时,它会显示一个长的JOptionPane消息框,该消息框长于计算机屏幕的宽度。 如何将e.toString()分成两个或多个部分。

在此处输入图像描述

 import java.awt.*; import javax.swing.*; class FixedWidthLabel { public static void main(String[] args) { Runnable r = new Runnable() { public void run() { String pt1 = "

Label Width

" + "

Many Swing components support HTML 3.2 &" + " (simple) CSS. By setting a body width we can cause the " + " component to find the natural height needed to display" + " the component.

" + "

The body width in this text is set to " + ""; String pt3 = " pixels."; JPanel p = new JPanel( new BorderLayout() ); int width = 175; String s = pt1 + width + pt2 + width + pt3 ; JOptionPane.showMessageDialog(null, s); } }; SwingUtilities.invokeLater(r); } }

你必须使用\n来打破不同行中的字符串。 或者你可以:

完成此任务的另一种方法是子类化JOptionPane类并覆盖getMaxCharactersPerLineCount,以便它返回您想要表示的字符数作为一行文本的最大值。

→ http://ninopriore.com/2009/07/12/the-java-joptionpane-class/ (死链接,请参阅存档副本 )。

与Andrew Thomson的回答类似,以下代码允许您从项目根目录加载HTML文件并将其显示在JOptionPane 。 请注意,您需要为Apache Commons IO添加Maven依赖项 。 如果你想从文件中读取格式化的HTML代码而不破坏渲染,那么使用HTMLCompressor也是一个好主意。

 import com.googlecode.htmlcompressor.compressor.HtmlCompressor; import org.apache.commons.io.FileUtils; import javax.swing.*; import java.io.File; import java.io.IOException; public class HTMLRenderingTest { public static void main(String[] arguments) throws IOException { String html = FileUtils.readFileToString(new File("document.html")); HtmlCompressor compressor = new HtmlCompressor(); html = compressor.compress(html); JOptionPane.showMessageDialog(null, html); } } 

这让你比Java字符串更好地管理HTML代码。

不要忘记使用以下内容创建名为document.html

  

Label Width

Many Swing components support HTML 3.2 & (simple) CSS. By setting a body width we can cause the component to find the natural height needed to display the component.

The body width in this text is set to 175 pixels.

结果:

我正在设置字符限制,然后在该环境中搜索最后一个空格字符并在那里写一个“\ n”。 (或者如果没有空格字符,我强制“\ n”)。 喜欢这个:

 /** Force-inserts line breaks into an otherwise human-unfriendly long string. * */ private String breakLongString( String input, int charLimit ) { String output = "", rest = input; int i = 0; // validate. if ( rest.length() < charLimit ) { output = rest; } else if ( !rest.equals("") && (rest != null) ) // safety precaution { do { // search the next index of interest. i = rest.lastIndexOf(" ", charLimit) +1; if ( i == -1 ) i = charLimit; if ( i > rest.length() ) i = rest.length(); // break! output += rest.substring(0,i) +"\n"; rest = rest.substring(i); } while ( (rest.length() > charLimit) ); output += rest; } return output; } 

我在(try)-catch括号中称之为:

 JOptionPane.showMessageDialog( null, "Could not create table 't_rennwagen'.\n\n" + breakLongString( stmt.getWarnings().toString(), 100 ), "SQL Error", JOptionPane.ERROR_MESSAGE );