9
votes

Pour casser un message dans deux lignes ou plus dans 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());
        }
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.

0 commentaires

4 Réponses :


3
votes

Vous devez utiliser \ n pour casser la chaîne dans différentes lignes. Ou vous pouvez:

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.

http://ninopriore.com/2009/07/ 12 / The-Java-Joptionpane-Class / (Lien mort, voir copie archivée ).


1 commentaires

Le line.sparator (qui pourrait ne pas être \ n 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 dans un volet d'option est un jlabel .



26
votes

Entrez la description de l'image ici

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 &amp; "
                + "(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);
    }
}


1 commentaires

Votre réponse m'a sauvé des maux de tête. Merci Andrew.



0
votes

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: xxx pré>

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
);


0 commentaires

1
votes

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 &amp; (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.


0 commentaires