JLabel 文本大于 JLabel 大小并变为“...”
JLabel text bigger than JLabel size and turning to "..."
我是 Java 的新手,我想制作一个带有按钮的简单 GUI,当您单击它时它会计数,我按照 YouTube 上的 this 教程实现了目标。一切顺利,一切正常。但有一件事没有,JLabel
文本的末尾从数字 (9
) 变为 ...
。
手动调整 window 的大小可以恢复数字,所以我尝试在代码启动时调整字体、window、按钮和标签的大小, 会 工作,但我仍然遇到同样的问题。
这是我的按钮代码:
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);
}
}
当框架 "packed" 在可见之前,所有组件都以其首选大小显示。
问题是,当您需要在标签中显示多个数字时,标签的首选大小会增加,但框架中 space 不足以完全显示标签文本,因此文本被截断。
因此解决方案是 pack() 框架,以便 GridLayout 中的组件可以再次以其首选大小显示:
label.setText("Number of clicks: " + count);
frame.pack();
另请注意,您不应在 ActionListener 中设置标签属性。创建标签时应设置以下属性:
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);
}
}
我是 Java 的新手,我想制作一个带有按钮的简单 GUI,当您单击它时它会计数,我按照 YouTube 上的 this 教程实现了目标。一切顺利,一切正常。但有一件事没有,JLabel
文本的末尾从数字 (9
) 变为 ...
。
手动调整 window 的大小可以恢复数字,所以我尝试在代码启动时调整字体、window、按钮和标签的大小, 会 工作,但我仍然遇到同样的问题。
这是我的按钮代码:
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);
}
}
当框架 "packed" 在可见之前,所有组件都以其首选大小显示。
问题是,当您需要在标签中显示多个数字时,标签的首选大小会增加,但框架中 space 不足以完全显示标签文本,因此文本被截断。
因此解决方案是 pack() 框架,以便 GridLayout 中的组件可以再次以其首选大小显示:
label.setText("Number of clicks: " + count);
frame.pack();
另请注意,您不应在 ActionListener 中设置标签属性。创建标签时应设置以下属性:
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);
}
}