gpt4 book ai didi

java - JLabel 对整个文本显示太短

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:55 24 4
gpt4 key购买 nike

这是一个 JLabel 显示太短的示例,没有足够的空间用于“网站”(请参阅​​代码)部分。

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

public class JLabeleg extends JFrame
{
public JLabeleg()
{
setTitle("Example");
setSize(500,100);
setVisible(true);
setLocationRelativeTo(null);
setLayout(new BorderLayout());

Container eg = getContentPane();
JPanel west = new JPanel(new GridLayout(2,1));
JPanel center = new JPanel(new GridLayout(2,3));

eg.add(west,BorderLayout.WEST);
eg.add(center,BorderLayout.CENTER);


west.add(new JLabel("Name : "));
west.add(new JLabel("Website : "));

center.add(new JLabel("Szekuns"));
center.add(new JLabel(""));
center.add(new JLabel(""));
center.add(new JLabel("www.example.com.my/example/example"));
center.add(new JLabel(""));
center.add(new JLabel(""));

}

public static void main(String[] args)
{
JLabeleg example = new JLabeleg();
example.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

我的原始代码与此示例的不同之处在于,JLabel 的源实际上是来自 IO 文件的对象加载的“setText”。那有关系吗?无论如何,“www.example.com/example/example”是否有完整的 View ?也许通过占用右侧 JLabel 的空间?

最佳答案

一种解决方案是在加载标签文本后使用 pack()。我不太喜欢这个解决方案,因为我觉得它会让用户感到不安。

GridLayout 也将很快向您展示一些重要的限制(如果您有一个大标签而其他标签非常小,则会丢失大量可用空间,因为所有标签都将获得最大的标签)。在所有情况下,我建议只设置 GridLayout 的“行”数或“列”数,这样更易​​于维护。

GridBagLayout 可能是替代方案,但需要更多经验才能掌握。

最后,JTable 在您的情况下可能是更好的选择(但我们缺少您要实现的目标的上下文)。

这里有 2 个示例展示了您如何完成工作(但还有许多其他可能更好的可能性,解释您的目标将有助于我们更加精确)。

示例 1(使用 pack()GridLayout):

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JLabeleg extends JFrame {

private List<JLabel> labels = new ArrayList<JLabel>();

public JLabeleg() {
setTitle("Example");
setLayout(new BorderLayout());

Container eg = getContentPane();
JPanel west = new JPanel(new GridLayout(2, 1));
final JPanel center = new JPanel(new GridLayout(0, 3));
JButton load = new JButton("Load labels");
load.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
int i = 0;
for (JLabel label : labels) {
label.setText("A very very very long long long long label " + String.valueOf(++i));
}
JLabeleg.this.pack();
JLabeleg.this.setLocationRelativeTo(null);
}
});
eg.add(west, BorderLayout.WEST);
eg.add(center, BorderLayout.CENTER);
eg.add(load, BorderLayout.SOUTH);

west.add(new JLabel("Name : "));
west.add(new JLabel("Website : "));

center.add(new JLabel("Szekuns"));
center.add(new JLabel(""));
center.add(new JLabel(""));
center.add(new JLabel("www.example.com.my/example/example"));
center.add(new JLabel(""));
center.add(new JLabel(""));
for (int i = 0; i < 12; i++) {
JLabel label = new JLabel();
center.add(label);
labels.add(label);
}
pack();
setLocationRelativeTo(null);
setVisible(true);

}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JLabeleg();
}
});

}
}

示例 2(使用 GridBagLayoutrevalidate())

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JLabeleg extends JFrame {

private List<JLabel> labels = new ArrayList<JLabel>();

public JLabeleg() {
setTitle("Example");
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container eg = getContentPane();
JPanel west = new JPanel(new GridLayout(0, 2));
final JPanel center = new JPanel(new GridBagLayout());
JButton load = new JButton("Load labels");
load.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
int i = 0;
for (JLabel label : labels) {
label.setText("A very long label " + String.valueOf(++i));
}
center.revalidate();
}
});
eg.add(west, BorderLayout.WEST);
eg.add(center, BorderLayout.CENTER);
eg.add(load, BorderLayout.SOUTH);

west.add(new JLabel("Name : "));
west.add(new JLabel("Szekuns"));
west.add(new JLabel("Website : "));
west.add(new JLabel("www.example.com"));
GridBagConstraints gbc = new GridBagConstraints();
GridBagConstraints endOfRow = new GridBagConstraints();
endOfRow.gridwidth = GridBagConstraints.REMAINDER;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
JLabel label = new JLabel();
if (j + 1 < 4) {
center.add(label, gbc);
} else {
center.add(label, endOfRow);
}
labels.add(label);
}
}
setSize(1000, 300);
setLocationRelativeTo(null);
setVisible(true);

}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JLabeleg();
}
});

}
}

关于java - JLabel 对整个文本显示太短,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16954600/

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