如何解决如何在 ActionListener 中使用 JLabel?
每当我按下写有颜色名称的按钮时,我都会尝试显示一个文本,上面写着“背景是(颜色)”,但文本没有显示。
它应该是怎样的:
但我的是这样的:
这是我的代码:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
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;
public class ThreeBtn extends JFrame {
private JButton btnRed;
private JButton btnGreen;
private JButton btnBlue;
public class ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent e) {
if (e.getSource() == btnRed) {
(getContentPane()).setBackground(Color.red);
JLabel label = new JLabel("빨간색 배경입니다."); //it means "The background is red" in Korean label.setFont(new Font("Serif",Font.BOLD,25));
label.setForeground(Color.yellow);
(getContentPane()).add(label);
}
else if(e.getSource()==btnGreen) {
(getContentPane()).setBackground(Color.green);
JLabel label = new JLabel("초록색 배경입니다."); //it means "The background is green" in Korean
label.setFont(new Font("Serif",25));
label.setForeground(Color.yellow);
(getContentPane()).add(label);
}
else if(e.getSource()==btnBlue) {
(getContentPane()).setBackground(Color.blue);
JLabel label = new JLabel("파란색 배경입니다."); //it means "The background is blue" in Korean
label.setFont(new Font("Serif",25));
label.setForeground(Color.yellow);
(getContentPane()).add(label);
}
}
}
public ThreeBtn() {
setSize(300,200);
setTitle("Three Button Example");
setDefaultCloSEOperation(EXIT_ON_CLOSE);
Container cPane = getContentPane();
cPane.setLayout(new FlowLayout());
btnRed = new JButton("RED");
btnGreen = new JButton("GREEN");
btnBlue = new JButton("Blue");
ButtonListener listener = new ButtonListener();
btnRed.addActionListener(listener);
btnGreen.addActionListener(listener);
btnBlue.addActionListener(listener);
cPane.add(btnRed);
cPane.add(btnGreen);
cPane.add(btnBlue);
}
public static void main(String[] args) {
(new ThreeBtn()).setVisible(true);
}
}
我想知道我是否在 ActionListener 中以错误的方式使用了 JLabel,但我想不通。
非常感谢您的帮助。
解决方法
在运行时向 GUI 添加或删除组件时,您必须告诉 Swing 您这样做了。为此,您应该在发生修改的容器上使用 revalidate()
和 repaint()
。
但是,在您的情况下,您实际上并不需要总是在运行时添加新的 JLabel
,因为您可以在设置 GUI 时简单地这样做,并且只需修改已经存在的标签。通过这种方式,您可以避免 revalidate()
和 repaint()
(而且这种方式也更简单、更干净)。
我更新了您的代码并进行了以下修改:
- 将
JLabel
的声明和初始化移出actionPerformed()
,因此您不必每次按下按钮时都创建一个新的JLabel
。现在,只更改了文本。 (此更改的副作用是,实际上不再需要revalidate()
和repaint()
,因为在运行时不会添加更多组件) - 通过
SwingUtilities.invokeLater()
在 Event Dispatch Thread 上启动 GUI。 - 通常,如果不是特别需要(例如您的情况),最好不要在 Swing 中创建
JFrame
的子类。更好的方法是子类化JPanel
,在那里添加必要的组件,覆盖getPreferredSize()
,然后将其添加到JFrame
。我没有在此处包含此更改,因为它可能会引起太多混乱。
更新代码:
public class ThreeBtn extends JFrame {
private JButton btnRed;
private JButton btnGreen;
private JButton btnBlue;
private JLabel label;
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Container contentPane = getContentPane();
if (e.getSource() == btnRed) {
contentPane.setBackground(Color.red);
label.setText("red in korean");
} else if (e.getSource() == btnGreen) {
contentPane.setBackground(Color.green);
label.setText("green in korean");
} else if (e.getSource() == btnBlue) {
contentPane.setBackground(Color.blue);
label.setText("blue in korean");
}
}
}
public ThreeBtn() {
setSize(300,200);
setTitle("Three Button Example");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container cPane = getContentPane();
cPane.setLayout(new FlowLayout());
btnRed = new JButton("RED");
btnGreen = new JButton("GREEN");
btnBlue = new JButton("BLUE");
label = new JLabel("");
label.setFont(new Font("Serif",Font.BOLD,25));
label.setForeground(Color.yellow);
ButtonListener listener = new ButtonListener();
btnRed.addActionListener(listener);
btnGreen.addActionListener(listener);
btnBlue.addActionListener(listener);
cPane.add(btnRed);
cPane.add(btnGreen);
cPane.add(btnBlue);
cPane.add(label);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> (new ThreeBtn()).setVisible(true));
}
}
输出:
这现在可以根据您使用的 FlowLayout
的要求工作。但是,如果您打算用它做更多事情,您可以使用另一个布局管理器(或组合不同的)。
框架和面板的尺寸很小,因此按下按钮时它们不会显示文本。
尺寸大小 = label.getPreferredSize(); label.setBounds(150,100,size.width,size.height);
,我不知道您的程序是否有任何规则,但我建议您将 ActionListener 中的 if 语句限制为设置前景的点。然后使您的 JLabel 对象成为类变量,并在构造函数中添加 JLabel。这是完整的编辑代码:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
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;
public class ThreeBtn extends JFrame {
private static final long serialVersionUID = 1L;
private JButton btnRed;
private JButton btnGreen;
private JButton btnBlue;
private JLabel text = new JLabel();
public class ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent e) {
if (e.getSource() == btnRed) {
(getContentPane()).setBackground(Color.red);
text.setText("red"); //it means "The background is red" in Korean
text.setFont(new Font("Serif",25));
text.setForeground(Color.yellow);
}
else if(e.getSource()==btnGreen) {
(getContentPane()).setBackground(Color.green);
text.setText("green"); //it means "The background is green" in Korean
text.setFont(new Font("Serif",25));
text.setForeground(Color.yellow);
}
else if(e.getSource()==btnBlue) {
(getContentPane()).setBackground(Color.blue);
text.setText("blue"); //it means "The background is blue" in Korean
text.setFont(new Font("Serif",25));
text.setForeground(Color.yellow);
(getContentPane()).add(text);
}
}
}
public ThreeBtn() {
setSize(300,200);
setTitle("Three Button Example");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container cPane = getContentPane();
cPane.setLayout(new FlowLayout());
btnRed = new JButton("RED");
btnGreen = new JButton("GREEN");
btnBlue = new JButton("Blue");
ButtonListener listener = new ButtonListener();
btnRed.addActionListener(listener);
btnGreen.addActionListener(listener);
btnBlue.addActionListener(listener);
cPane.add(btnRed);
cPane.add(btnGreen);
cPane.add(btnBlue);
cPane.add(text);
}
public static void main(String[] args) {
(new ThreeBtn()).setVisible(true);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。