如何使用 ActionListener 使 JPanel 可见

问题描述

我试图通过单击 JButton 使第一个 JPanel 消失而第二个 JPanel 可见。 到目前为止,我只显示一个 JPanel,单击 JButton 后,框架变空。 我也尝试用组合来做到这一点,所以我不必扩展类。所以我对如何理解不好 组合作品可能是问题所在。我研究了很多,但找不到合适的解决方案。

一个 JPanel 类:

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

public class Panel1 {

JPanel firstscreenpanel = new JPanel();
JButton jButton1 = new JButton();
    
Panel1() {
    
    jButton1.setBounds(300,300,400,200);
    jButton1.setBackground(Color.BLACK);
    jButton1.setVisible(true);
    jButton1.addActionListener(new ActionListener() {

    Panel2 test = new Panel2();
            
    public void actionPerformed(ActionEvent e) {
            
        
            firstscreenpanel.setVisible(false); 
            test.secondscreenpanel.setVisible(true);
            
            
            }
    });
    
    
}

public Component panelone() {
    
    firstscreenpanel.setSize(1280,1024);
    firstscreenpanel.setLayout(null);
    firstscreenpanel.setBackground(Color.BLUE);
    firstscreenpanel.add(jButton1);
    firstscreenpanel.setVisible(true);
    return firstscreenpanel;
            
    }

}

第二个 JPanel 类:

import java.awt.Color;
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Panel2 {

public JPanel secondscreenpanel = new JPanel();
public JButton jButton2 = new JButton();



Panel2() {
    
    jButton2.setBounds(100,100,200);
    jButton2.setBackground(Color.BLACK);
    jButton2.setVisible(true);
            
}
    
public Component paneltwo() {
    
    secondscreenpanel.setSize(1280,1024);
    secondscreenpanel.setLayout(null);
    secondscreenpanel.add(jButton2);
    secondscreenpanel.setBackground(Color.RED);
    secondscreenpanel.setVisible(false);
    return secondscreenpanel;
            
    }
    }

JFrame 类:

import javax.swing.JFrame;

public class Frame1 {


public JFrame frame1 = new JFrame();
Panel1 panel1 = new Panel1();
Panel2 panel2 = new Panel2();

Frame1() {
    
    frame1.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setState(JFrame.MAXIMIZED_BOTH);
    frame1.setSize(1280,1024);
    frame1.setLayout(null);
    frame1.add(panel1.panelone());
    frame1.add(panel2.paneltwo());
    frame1.setVisible(true);
    
            
    }
    }

主类:

    public class MainClass {

        
    private void showGUI() {

    Frame1 jframe = new Frame1();
                        
    }
    
    public static void main(String[] args) {
        
        final MainClass main = new MainClass();
        
        javax.swing.SwingUtilities.invokelater(new Runnable()

          {
            public void run() {
                main.showGUI();

            }
        
         });
        
      }

        }

解决方法

我没有检查整个代码(错误太多,empty 行太多)而是停在 Panel2 test = new Panel2();

这个实例是否被添加到某个可见组件中?如果不是,它将永远不会显示。

注意:通常不推荐使用 null 布局管理器,使用 CardLayout 甚至 JTabbedPane 来切换组件 - 参见教程 A Visual Guide to Layout Managers

,

这不是最好的实现,但它很简单,您可以遵循。我修改了您的代码以创建一个包含原始两个面板的框架(尽管这些面板类不是必需的 - 正如我在您发布的解决方案的评论中所解释的那样),以及一个用于切换面板可见性的按钮。我使用的是常规 JButton 而不是 JToggleButton 也不是类的最佳用法,只是为了让您理解。

Action Listener 被添加到框架上的按钮。请注意,我的动作侦听器不会创建任何新实例。那是原始问题的一部分。由于按钮是框架类的成员,如面板 1 和 2,它可以直接访问它们。所以,在侦听器中,我需要做的就是“切换”每个面板的可见性。

public class Frame1 extends JFrame {
    private Panel1 panel1 = new Panel1();
    private Panel2 panel2 = new Panel2();
    private JPanel btnPanel = new JPanel();
    private JButton button = new JButton("Toggle");

    public Frame1() {
    
        button.addActionListener(new ActionListener() {
        
            @Override
            public void actionPerformed(ActionEvent arg0) {
                boolean visible = panel1.isVisible();
                panel1.setVisible(!visible);
                panel2.setVisible(visible);
            
            }
        });

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setState(JFrame.MAXIMIZED_BOTH);
        setSize(1280,1024);
        btnPanel.setSize(400,100);
        btnPanel.add(button);
        setLayout(null);
        add(panel1);
        add(panel2);
        add(btnPanel);
    }
}

public class Panel1 extends JPanel {

    public Panel1() {
        setBounds(100,100,400,200);
        setBackground(Color.RED);
        setVisible(true);
    }
}

public class Panel2 extends JPanel {

    public Panel2() {
        setBounds(100,200);
        setBackground(Color.BLACK);
        setVisible(false);
    }
}

public class MainClass {

    private void showGUI() {

        Frame1 jframe = new Frame1();
        jframe.setVisible(true);
    }

    public static void main(String[] args) {

        final MainClass main = new MainClass();
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                main.showGUI();

            }

        });
    }
}
,

我通过向“Panel2 panel2 = new Panel2();”添加“public static”解决了我的问题

然后我就用了:

"Frame1.panel2.secondscreenpanel.setVisible(true);"

在 JButton ActionListener 中。

它现在可以工作了,但我想这是一种糟糕的做法。因为我听说使用太多的静态不是那么好。但我现在不知道为什么。

,

您可以通过将您的框架的引用传递给您的 panel1 来修复您的程序。请参阅下面的示例。

框架类

import javax.swing.*;

public class Frame1 {

    private JFrame frame = new JFrame();
    private Panel1 panel1;
    private Panel2 panel2;

    Frame1() {
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setState(JFrame.MAXIMIZED_BOTH);
         frame.setSize(1280,1024);
         frame.setLayout(null);
         panel1 = new Panel1(this);
         frame.add(panel1.getPanel());
         panel2 = new Panel2();
         frame.add(panel2.getPanel());
         frame.setVisible(true);
    }

    public Panel2 getPanel2() {
        return panel2;
    }

}

Panel1 类

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

public class Panel1 {
    private JPanel panel = new JPanel();
    private Frame1 frame1;
    private JButton jButton1 = new JButton();

    public Panel1(Frame1 frame1) {
        this.frame1 = frame1;
        panel.setSize(1280,1024);
        panel.setLayout(null);
        panel.setBackground(Color.BLUE);
        panel.add(jButton1);
        panel.setVisible(true);
        jButton1.setBounds(300,300,200);
        jButton1.setBackground(Color.BLACK);
        jButton1.setVisible(true);
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel.setVisible(false);
                frame1.getPanel2().getPanel().setVisible(true);
            }
        });
    }

    public JPanel getPanel() {
         return panel;
    }
}

Panel2 类

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

public class Panel2 {
     private JButton jButton2 = new JButton();
     private JPanel panel = new JPanel();

    public Panel2() {
        panel.setSize(1280,1024);
        panel.setLayout(null);
        panel.add(jButton2);
        panel.setBackground(Color.RED);
        panel.setVisible(false);

        jButton2.setBounds(100,200);
        jButton2.setBackground(Color.BLACK);
        jButton2.setVisible(true);
    }

    public JPanel getPanel() {
        return panel;
    }
}