2017-06-18 79 views
1

我試圖設計一個彈出窗口來通知接收新的電子郵件。 這是代碼:swing - GridBagLayout中的JLabel與長文本

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.ImageIcon; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

/** 
* 
* @author luca 
*/ 
public class Popup extends JDialog { 


    public Popup() { 
     super.setSize(260, 100); 

     this.setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 

     gbc.insets = new Insets(5, 5, 5, 5); 


     JLabel header = new JLabel("Hai ricevuto una nuova email"); 
     gbc.gridx = 1; 
     gbc.gridy = 0; 
     gbc.gridheight = 1; 
     gbc.anchor = GridBagConstraints.WEST; 
     add(header, gbc); 

     JLabel mittente = new JLabel("[email protected]"); 
     gbc.gridx = 1; 
     gbc.gridy = 1; 
     gbc.gridheight = 1; 
     add(mittente, gbc); 

     JLabel argomento = new JLabel("info voto esame"); 
     gbc.gridx = 1; 
     gbc.gridy = 2; 
     gbc.gridheight = 1; 
     add(argomento, gbc); 

     JLabel icona = new JLabel(new ImageIcon("img/email.png")); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridheight = 3; 
     gbc.fill = GridBagConstraints.BOTH; 
     add(icona, gbc); 


     this.setLocation(400, 400); 
     this.setUndecorated(true); 
     this.setVisible(true); 
     mittente.setMaximumSize(new Dimension(180, 16)); 
    } 

    public static void main(String args[]){ 
     new Popup(); 
    } 

} 

當標籤文本不會太長一切正常,我得到這樣的: enter image description here

但是當文本太長,這是我得到: enter image description here

有沒有辦法得到這樣的東西? enter image description here

我發現這個東西別人的質疑,我試圖在label.setMaximumSize工作(),我試着寫HTML標記的文字......但是毫無效果的。有人能幫我嗎?

回答

6

在這種情況下,它有助於將圖像的weightx設置爲1,而其他所有設置都爲0.還有其他各種更改。仔細檢查代碼以瞭解更改。

我還建議將全文設置爲每個標籤的工具提示。如果用戶想要查看整個細節,只需將鼠標指向標籤即可。

enter image description here

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

public class Popup extends JDialog { 

    public Popup() { 
     this.setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 

     gbc.insets = new Insets(5, 5, 5, 5); 

     JLabel header = new JLabel("Hai ricevuto una nuova email"); 
     gbc.gridx = 1; 
     gbc.gridy = 0; 
     gbc.gridheight = 1; 
     gbc.weightx = 1; 
     gbc.anchor = GridBagConstraints.WEST; 
     add(header, gbc); 

     gbc.weightx = 0; 
     JLabel mittente = new JLabel("[email protected]"); 
     gbc.gridx = 1; 
     gbc.gridy = 1; 
     gbc.gridheight = 1; 
     add(mittente, gbc); 

     JLabel argomento = new JLabel("info voto esame"); 
     gbc.gridx = 1; 
     gbc.gridy = 2; 
     gbc.gridheight = 1; 
     add(argomento, gbc); 

     JLabel icona = new JLabel(new ImageIcon(
       new BufferedImage(40,60,BufferedImage.TYPE_INT_RGB))); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridheight = 3; 
     gbc.fill = GridBagConstraints.BOTH; 
     add(icona, gbc); 

     this.setLocation(400, 400); 
     this.setUndecorated(true); 
     pack(); 
     super.setSize(260, 100); 
     this.setVisible(true); 
     mittente.setMaximumSize(new Dimension(180, 16)); 
    } 

    public static void main(String args[]) { 
     new Popup(); 
    } 
} 
+1

非常感謝,這正是我一直在尋找! – Cilla