2011-10-08 5 views
6
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()); 
     } 

In caso di errore, viene visualizzata una finestra di messaggio JOptionPane lunga più lunga della larghezza dello schermo del computer. Come posso rompere e.toString() in due o più parti.Per interrompere un messaggio in due o più righe in JOptionPane

+1

Put nuova riga (\ n). – adatapost

risposta

21

enter image description here

import java.awt.*; 
import javax.swing.*; 

class FixedWidthLabel { 

    public static void main(String[] args) { 

     Runnable r = new Runnable() { 
      public void run() { 
       String pt1 = "<html><body width='"; 
       String pt2 = 
        "'><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 " + 
        ""; 
       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); 
    } 
} 
3

È necessario utilizzare \n per interrompere la stringa in linee diverse. Oppure si può:

Un altro modo per eseguire questa operazione è quello di creare una sottoclasse della classe JOptionPane e ignorare il getMaxCharactersPerLineCount in modo che restituisca il numero di caratteri che si vuole rappresentare il massimo per una riga di testo .

http://ninopriore.com/2009/07/12/the-java-joptionpane-class/ (link morto, vedere archived copy).

+0

'line.separator' (che potrebbe non essere' \ n' BTW), funzionerà solo se il testo è inserito in un componente multi-linea come un 'JTextArea'. Il componente utilizzato per visualizzare un 'String' in un riquadro delle opzioni è un' JLabel'. –

0

che sto fissando un limite di caratteri, quindi cercare l'ultimo carattere di spazio in quell'ambiente e scrivere un "\ n" lì. (O forza il "\ n" se non c'è spazio). Come questo:

/** 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; 
} 

E io lo chiamo così nel (provare) Staffa-Catch:

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

Simile a Andrew Thomson 's risposta, il seguente ha lasciati codice che caricare un file HTML dal directory radice del progetto e visualizzarla in un JOptionPane. Si noti che è necessario aggiungere uno Maven dependency for Apache Commons IO. Anche l'uso di HTMLCompressor è una buona idea se si desidera leggere il codice HTML formattato da un file senza interrompere il rendering.

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

di questo consentono di gestire il codice HTML meglio che in Java Strings.

Non dimenticare di creare un file denominato document.html con il seguente contenuto:

<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. 

Risultato: