Java ActionEvent在没有明显原因的情况下停止工作?

Java ActionEvent在没有明显原因的情况下停止工作?,java,swing,actionevent,Java,Swing,Actionevent,最近我一直在玩JavaSwing,并试图创建一个定制的Minecraft服务器启动器。当我按下标有“属性”的按钮时,它应该会切换到一个新面板,并在某一点上进行了更改。然而,由于某种原因,我无法看到它不再工作。有人能帮忙吗 package custommcserver; import java.awt.GridLayout; import java.awt.event.*; import javax.swing.*; class Window extends JFrame implements

最近我一直在玩JavaSwing,并试图创建一个定制的Minecraft服务器启动器。当我按下标有“属性”的按钮时,它应该会切换到一个新面板,并在某一点上进行了更改。然而,由于某种原因,我无法看到它不再工作。有人能帮忙吗

package custommcserver;

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

class Window extends JFrame implements ActionListener , ItemListener
{
    JPanel mainPnl = new JPanel(new GridLayout(2,1));
    JPanel propPnl = new JPanel();
    JButton startBtn = new JButton("Start");
    JButton stopBtn = new JButton("Stop");
    JButton propBtn = new JButton("Properties");

    JCheckBox allowNether = new JCheckBox("Allow players to visit the Nether dimension");

    public Window()
    {
        super("Custom Minecraft Server Launcher") ;
        setSize(500,200) ; 
        setDefaultCloseOperation(EXIT_ON_CLOSE) ;
        add(mainPnl) ;
        mainPnl.add(startBtn);
        mainPnl.add(stopBtn);
        mainPnl.add(propBtn);
        stopBtn.setEnabled(false);
        startBtn.addActionListener(this);
        stopBtn.addActionListener(this);
        propBtn.addActionListener(this);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent event) 
    {

        if (event.getSource() == stopBtn)
        {
            stopBtn.setEnabled(false);
            startBtn.setEnabled(true);
        }

        if (event.getSource() == startBtn)
        {
            stopBtn.setEnabled(true);
            startBtn.setEnabled(false);


        if (event.getSource() == propBtn)
        {
            add(propPnl);
            mainPnl.setVisible(false);
            propPnl.setVisible(true);
            propPnl.add(allowNether);
        }

    }
}

    @Override
    public void itemStateChanged(ItemEvent event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
2个问题:

  • 线路

    if(event.getSource()==propBtn){

    if
    语句检查的
    startBtn
    中,因此实际上没有对该按钮进行检查。将其移出该
    if
    语句块

  • JFrame


  • 旁注:

    • CardLayout
      提供用另一个组件替换组件的功能。请阅读
    • 使用匿名
      ActionListener
      类实例来避免问题1

    谢谢。经过测试,效果良好。这是我在Java世界学到的又一个教训。我认为这对JFrame来说已经足够了