gpt4 book ai didi

ActionListener 中的 Java swing 问题

转载 作者:行者123 更新时间:2023-12-02 00:22:31 25 4
gpt4 key购买 nike

//GUI.java
public class GUI extends JFrame implements ActionListener {

private static final long serialVersionUID = 870343916997182570L;
private JPanel btmPanel;

public GUI(String arg0) throws HeadlessException {
super(arg0);
createGUI();
}

private void createGUI() {
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

//ResultPanel rslt = new ResultPanel();
//this.getContentPane().add(rslt.createPanel(), BorderLayout.CENTER);

btmPanel = new JPanel();
btmPanel.setBackground(Color.LIGHT_GRAY);
btmPanel.setLayout(new FlowLayout());

JButton blueSearch = new JButton("Search");
blueSearch.setBackground(Color.WHITE);
blueSearch.addActionListener(this);
btmPanel.add(blueSearch);

JButton blackChart = new JButton("Chart");
blackChart.setBackground(Color.WHITE);
blackChart.addActionListener(this);
btmPanel.add(blackChart);

this.getContentPane().add(btmPanel, BorderLayout.SOUTH);
}

@Override
public void actionPerformed(ActionEvent e) {
String buttonString = e.getActionCommand();

if (buttonString.equals("Search")) {
ResultPanel rslt = new ResultPanel();
this.getContentPane().add(rslt.createPanel(), BorderLayout.CENTER);
}
}

}

//ResultPanel.java
public class ResultPanel extends JPanel implements ActionListener {

private static final long serialVersionUID = -7851502165390304971L;
private JPanel textPanel;
private JTextArea textDisplay;

public ResultPanel() {
textPanel = new JPanel();
textDisplay = new JTextArea("Text Area:");
}

public JPanel createPanel() {

textDisplay.setEditable(true);
textPanel.setBackground(Color.LIGHT_GRAY);
textPanel.setLayout(new BorderLayout());
textPanel.add(textDisplay,BorderLayout.CENTER);

return textPanel;
}

@Override
public void actionPerformed(ActionEvent e) {


}

}

我的主机上有两个按钮,我希望按下按钮时改变面板。

问题是“actionPerformed”中的代码不起作用,

但如果我将它们放入 creatGUI() 中,效果会很好......(请参阅标记部分)。

有什么问题吗?

最佳答案

在运行中添加组件后,您需要调用 revalidate()repaint() 来将组件更改为可见。

尽管正如您所说,如果您将它们添加到 createGUI() 中,那么它是可见的,因为当时它是 Swing 应用程序的静态添加,您首先添加了它,然后然后将其设置为可见。

虽然您的代码有一些漏洞,但我可以快速告诉您的最好方法是,您为 ResultPanel 扩展了 JPanel,尽管您从未使用过 ResultPanel ,所以我修改了您的代码以考虑 ResultPanel 。这里尝试一下您的示例中的修改后的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AddComponentExample
{
private JFrame frame;
private JPanel btmPanel;
private ResultPanel resultPanel;

public AddComponentExample()
{
resultPanel = new ResultPanel();
}

private void display()
{
frame = new JFrame("Adding Component Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

btmPanel = new JPanel();
btmPanel.setBackground(Color.LIGHT_GRAY);
btmPanel.setLayout(new FlowLayout());

JButton blueSearch = new JButton("Search");
blueSearch.setBackground(Color.WHITE);
blueSearch.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (!resultPanel.isShowing())
{
resultPanel = resultPanel.createPanel();
frame.getContentPane().add(resultPanel, BorderLayout.CENTER);
frame.revalidate(); // For Java 7 and above.
// frame.getContentPane().revalidate(); // For Java 1.6 or below.
frame.repaint(); // required sometimes
}
else
System.out.println("Panel is already Visible");
}
});
btmPanel.add(blueSearch);

JButton blackChart = new JButton("Chart");
blackChart.setBackground(Color.WHITE);
//blackChart.addActionListener(this);
btmPanel.add(blackChart);

frame.getContentPane().add(btmPanel, BorderLayout.PAGE_END);
frame.setSize(500, 500);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new AddComponentExample().display();
}
});
}
}

class ResultPanel extends JPanel implements ActionListener {

private static final long serialVersionUID = -7851502165390304971L;
private JPanel textPanel;
private JTextArea textDisplay;

public ResultPanel() {
textPanel = new JPanel();
textDisplay = new JTextArea("Text Area:");
}

public ResultPanel createPanel() {

textDisplay.setEditable(true);
textPanel.setBackground(Color.LIGHT_GRAY);
textPanel.setLayout(new BorderLayout());
textPanel.add(textDisplay,BorderLayout.CENTER);
add(textPanel);
return this;
}

@Override
public void actionPerformed(ActionEvent e) {


}

}

关于ActionListener 中的 Java swing 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10671449/

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