2011-10-08 38 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()); 
     } 

Khi có lỗi, nó hiển thị hộp tin nhắn JOptionPane dài hơn chiều rộng của màn hình máy tính. Làm thế nào tôi có thể phá vỡ e.toString() thành hai hoặc nhiều phần.Để ngắt thư trong hai hoặc nhiều dòng trong JOptionPane

+1

Đặt xuống dòng (\ n) nhân vật. – adatapost

Trả lời

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

Bạn phải sử dụng \n để ngắt chuỗi trong các dòng khác nhau. Hoặc bạn có thể:

Một cách khác để hoàn thành nhiệm vụ này là để phân lớp lớp JOptionPane và ghi đè lên getMaxCharactersPerLineCount để nó trả về số ký tự mà bạn muốn thể hiện như là tối đa cho một dòng văn bản .

http://ninopriore.com/2009/07/12/the-java-joptionpane-class/ (liên kết chết, xem archived copy).

+0

'line.separator' (có thể không phải là' \ n' BTW), sẽ chỉ hoạt động nếu văn bản được đưa vào thành phần nhiều dòng như 'JTextArea'. Thành phần được sử dụng để hiển thị một 'Chuỗi' trong một ngăn tùy chọn là một' JLabel'. –

0

Tôi đang thiết lập một giới hạn ký tự, sau đó tìm kiếm các nhân vật không gian cuối cùng trong môi trường đó và viết một "\ n" ở đó. (Hoặc tôi buộc "\ n" nếu không có ký tự khoảng trắng). Như thế này:

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

Và tôi gọi nó là như thế này trong (thử) -catch khung:

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

Tương tự như Andrew Thomson 's câu trả lời, đoạn code sau bạn hãy tải một tập tin HTML từ thư mục gốc của dự án và hiển thị nó trong một JOptionPane. Lưu ý rằng bạn cần thêm Maven dependency for Apache Commons IO. Ngoài ra, việc sử dụng HTMLCompressor là một ý tưởng hay nếu bạn muốn đọc mã HTML được định dạng từ tệp mà không vi phạm hiển thị.

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

Điều này cho phép bạn quản lý mã HTML tốt hơn so với trong Java Strings.

Đừng quên để tạo ra một tập tin có tên document.html với nội dung sau:

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

Kết quả:

Các vấn đề liên quan