使用 Java Swing 控件的派生类为自身创建侦听器
首先:我知道我所做的设计很糟糕。我尝试这样做是为了更好地了解 Java——什么是可能的,什么是不可能的,为什么?
我编写了以下代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
@SuppressWarnings("serial")
class ButtonFrame extends JFrame
{
private final static int DEFAULT_WIDTH = 300;
private final static int DEFAULT_HEIGHT = 200;
private final JPanel buttonPanel;
public ButtonFrame()
{
this.setTitle("Button Frame");
this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
PanelButton yellowButton = new PanelButton("Yellow", Color.YELLOW, this);
PanelButton redButton = new PanelButton("Red", Color.RED, this);
PanelButton blueButton = new PanelButton("Blue", Color.BLUE, this);
this.buttonPanel = new JPanel();
this.buttonPanel.add(yellowButton);
this.buttonPanel.add(redButton);
this.buttonPanel.add(blueButton);
// add panel to frame
this.add(this.buttonPanel);
}
}
@SuppressWarnings("serial")
class PanelButton extends JButton implements ActionListener
{
private final Color buttonColor;
private final ButtonFrame containingFrame;
public PanelButton(String title, Color buttonColor,
ButtonFrame containingFrame)
{
super(title);
this.buttonColor = buttonColor;
this.containingFrame = containingFrame;
this.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent event)
{
this.containingFrame.setBackground(this.buttonColor);
}
}
但这不起作用。我从调试器中看到 actionPerformed()
正在被调用,并且它具有预期的值。我无法理解这里发生了什么。有人可以帮我吗?
At the outset: I know that what I am doing is bad design. I am trying this to get a better feel of Java - what is possible, what is not and why?
I have written the following code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
@SuppressWarnings("serial")
class ButtonFrame extends JFrame
{
private final static int DEFAULT_WIDTH = 300;
private final static int DEFAULT_HEIGHT = 200;
private final JPanel buttonPanel;
public ButtonFrame()
{
this.setTitle("Button Frame");
this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
PanelButton yellowButton = new PanelButton("Yellow", Color.YELLOW, this);
PanelButton redButton = new PanelButton("Red", Color.RED, this);
PanelButton blueButton = new PanelButton("Blue", Color.BLUE, this);
this.buttonPanel = new JPanel();
this.buttonPanel.add(yellowButton);
this.buttonPanel.add(redButton);
this.buttonPanel.add(blueButton);
// add panel to frame
this.add(this.buttonPanel);
}
}
@SuppressWarnings("serial")
class PanelButton extends JButton implements ActionListener
{
private final Color buttonColor;
private final ButtonFrame containingFrame;
public PanelButton(String title, Color buttonColor,
ButtonFrame containingFrame)
{
super(title);
this.buttonColor = buttonColor;
this.containingFrame = containingFrame;
this.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent event)
{
this.containingFrame.setBackground(this.buttonColor);
}
}
But this is not working. I see from the debugger that actionPerformed()
is being invoked and it has the expected values. I am not able to understand what is happening here. Can someone please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在设置
JFrame
的背景颜色,但是JFrame
包含的JPanel
(然后保存您的按钮)占据了所有内容JFrame
的空间,因此您看不到背景颜色的变化。这有效。
You are setting the background color of the
JFrame
, but theJPanel
that is contained by theJFrame
, which then holds your buttons, takes up all of the space of theJFrame
, so you can't see the background color changing.This works.
您可以跳过传递容器并直接执行
You can skip passing the container and just do