swing - GridBagLayout 中带有长文本的 JLabel
swing - JLabel in GridBagLayout with long text
我正在尝试设计一个弹出窗口来通知收到新电子邮件。
这是代码:
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("luca.cillario@yahoo.it");
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();
}
}
当标签中的文本不太长时,一切正常,我得到如下信息:
但是当文本太长时,这就是我得到的:
有没有办法得到这样的东西?
我发现了关于这件事的其他问题,我尝试处理 label.setMaximumSize(),我尝试用 html 标签编写文本...但没有任何结果。有人可以帮助我吗?
在这种情况下,它有助于将图像的 weightx
设置为 1,而其他所有内容都为 0。还有其他各种更改。仔细检查代码是否有更改。
我还建议将全文设置为每个标签的工具提示。如果用户想查看全部详情,只需将鼠标指向标签即可。
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("luca.cillario@yahoo.itabcditabcditabcditabcditabcd");
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();
}
}
我正在尝试设计一个弹出窗口来通知收到新电子邮件。 这是代码:
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("luca.cillario@yahoo.it");
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();
}
}
当标签中的文本不太长时,一切正常,我得到如下信息:
但是当文本太长时,这就是我得到的:
有没有办法得到这样的东西?
我发现了关于这件事的其他问题,我尝试处理 label.setMaximumSize(),我尝试用 html 标签编写文本...但没有任何结果。有人可以帮助我吗?
在这种情况下,它有助于将图像的 weightx
设置为 1,而其他所有内容都为 0。还有其他各种更改。仔细检查代码是否有更改。
我还建议将全文设置为每个标签的工具提示。如果用户想查看全部详情,只需将鼠标指向标签即可。
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("luca.cillario@yahoo.itabcditabcditabcditabcditabcd");
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();
}
}