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()); } When there is an error it shows a long JOptionPane message box that is longer than the width of the computer screen. How can I break e.toString() into two or more parts.
4 Réponses :
Vous devez utiliser Une autre façon d'accomplir cette tâche est de sous-classer le JOPTIONPANE
classe et remplacer le getmaxcharactertersperlinocount afin qu'il revienne
le nombre de caractères que vous souhaitez représenter comme maximum pour
une ligne de texte. p>
blockQuote>
→ http://ninopriore.com/2009/07/ 12 / The-Java-Joptionpane-Class / (Lien mort, voir copie archivée ). P> \ n code> pour casser la chaîne dans différentes lignes. Ou vous pouvez: p>
Le line.sparator code> (qui pourrait ne pas être
\ n code> BTW) ne fonctionnera que si le texte est mis dans un composant multiligne tel qu'un
jtextarea < / code>. Le composant utilisé pour afficher une chaîne code> dans un volet d'option est un
jlabel code>.
import javax.swing.*; class FixedWidthLabel { public static void main(String[] args) { Runnable r = () -> { String html = "<html><body width='%1s'><h1>Label Width</h1>" + "<p>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.<br><br>" + "<p>The body width in this text is set to %1s pixels."; // change to alter the width int w = 175; JOptionPane.showMessageDialog(null, String.format(html, w, w)); }; SwingUtilities.invokeLater(r); } }
Votre réponse m'a sauvé des maux de tête. Merci Andrew.
Je fixe une limite de caractères, puis recherchez le dernier caractère d'espace dans cet environnement et écrivez un "\ n" là-bas. (Ou je force le "\ n" s'il n'y a pas de caractère d'espace). Comme ceci: et je l'appelle comme ceci dans le support (Essayez) -Catch: p> JOptionPane.showMessageDialog(
null,
"Could not create table 't_rennwagen'.\n\n"
+ breakLongString( stmt.getWarnings().toString(), 100 ),
"SQL Error",
JOptionPane.ERROR_MESSAGE
);
Semblable à Andrew Thomson de réponse 'S, le code suivant vous permet de charger un html code> Fichier du répertoire racine de projet et affichez-le dans un
JOPTIONPANE CODE>. Notez que vous devez ajouter un Dépendance Maven pour Apache Commons IO . Aussi l'utilisation de
htmlcompresseur code>
est une bonne idée si tu veux Pour lire le code HTML formaté à partir d'un fichier sans casser le rendu. <html>
<body width='175'><h1>Label Width</h1>
<p>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.<br><br>
<p>The body width in this text is set to 175 pixels.