大于 JLabel 大小的 JLabel 文本并变为"..."



我对Java真的很陌生,我想制作一个简单的GUI,上面有一个按钮,当你点击它时,它很重要,我按照YouTube上的本教程到达那里。一切顺利,一切正常。但有一件事没有,JLabel文本的结尾从数字(9(变成了...。 手动调整窗口大小会恢复数字,因此我尝试在启动时调整代码中的字体、窗口、按钮和标签大小,这确实有效,但我仍然遇到同样的问题。

这是我的按钮代码:

JButton button = new JButton("Click me!");
button.addActionListener(this);

。我相信会这样称呼:

public void actionPerformed(ActionEvent e) {
count++;
label.setText("Number of clicks: " + count);
}

我已经尝试了其他一些其他解决方案,但它们根本没有帮助。

我的 GUI 完整代码是这样的:

package javaGUI;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI implements ActionListener {
private int count = 0;
private JLabel label;
private JFrame frame;
private JPanel panel;
public GUI() {
frame = new JFrame();
JButton button = new JButton("Click me!");
button.addActionListener(this);
button.setPreferredSize(new Dimension(10, 75));
label = new JLabel("Number of clicks: 0");
label.setFont(new Font("System", Font.PLAIN, 22));
panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(60, 100, 30, 100));
panel.setLayout(new GridLayout(0, 1));
panel.add(button);
panel.add(label);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("GUI");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
@Override
public void actionPerformed(ActionEvent e) {
count++;
label.setText("Number of clicks: " + count);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
}
}

当框架在可见之前被"打包"时,所有组件都以其首选大小显示。

问题是,当您需要在标签中显示多个数字时,标签首选大小会增加,但框架中没有足够的空间来完全显示标签文本,因此文本被截断。

因此,解决方案是 pack(( 框架,以便 GridLayout 中的组件可以再次以其首选大小显示:

label.setText("Number of clicks: " + count);
frame.pack();

另请注意,不应在操作侦听器中设置标签属性。创建标签时应设置以下属性:

label = new JLabel("Number of clicks: 0");
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);

基本上,你必须给JLabel成长的空间。

这里有一种方法可以做到这一点。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ClickMe implements ActionListener, Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new ClickMe());
}
private int count = 0;
private JLabel label;
private JFrame frame;
@Override
public void run() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Click Me");
JPanel buttonPanel = new JPanel();
JButton button = new JButton("Click me!");
button.addActionListener(this);
button.setFont(new Font("System", Font.PLAIN, 22));
button.setPreferredSize(new Dimension(300, 75));
buttonPanel.add(button);
frame.add(buttonPanel, BorderLayout.CENTER);
JPanel labelPanel = new JPanel();
label = new JLabel("Number of clicks: 0");
label.setFont(new Font("System", Font.PLAIN, 22));
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
labelPanel.add(label);
frame.add(labelPanel, BorderLayout.AFTER_LAST_LINE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
count++;
label.setText("Number of clicks: " + count);
}
}

最新更新