gpt4 book ai didi

java - swing - GridBagLayout 中带有长文本的 JLabel

转载 作者:行者123 更新时间:2023-11-29 06:52:06 25 4
gpt4 key购买 nike

我正在尝试设计一个弹出窗口来通知收到新电子邮件。这是代码:

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

}

当标签中的文本不太长时,一切正常,我得到如下信息: enter image description here

但是当文本太长时,这就是我得到的: enter image description here

有没有办法得到这样的东西? enter image description here

我发现了关于这件事的其他问题,我尝试处理 label.setMaximumSize(),我尝试用 html 标签编写文本...但没有任何结果。有人可以帮助我吗?

最佳答案

在这种情况下,将图像的 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("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();
}
}

关于java - swing - GridBagLayout 中带有长文本的 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44617172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com