gpt4 book ai didi

java - 使用 Java Swing 控件的派生类为自己创建监听器

转载 作者:行者123 更新时间:2023-12-03 21:06:58 25 4
gpt4 key购买 nike

一开始:我知道我所做的是糟糕的设计。我尝试这样做是为了更好地了解 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() 并且它具有预期值。我不明白这里发生了什么。有人可以帮帮我吗?

最佳答案

您正在设置 JFrame 的背景颜色,但是 JFrame 包含的 JPanel 会保存您的按钮,占用了 JFrame 的所有空间,因此您看不到背景颜色的变化。

这有效。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

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);

this.buttonPanel = new JPanel();

PanelButton yellowButton = new PanelButton("Yellow", Color.YELLOW, buttonPanel);
PanelButton redButton = new PanelButton("Red", Color.RED, buttonPanel);
PanelButton blueButton = new PanelButton("Blue", Color.BLUE, buttonPanel);

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 JPanel buttonPanel;

public PanelButton(String title, Color buttonColor,
JPanel buttonPanel)
{
super(title);
this.buttonColor = buttonColor;
this.buttonPanel = buttonPanel;
this.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent event)
{
buttonPanel.setBackground(this.buttonColor);
}
}

关于java - 使用 Java Swing 控件的派生类为自己创建监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6802280/

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