从另一个面板上的Button在JFrame上打开JPanel

时间:2019-02-14 15:46:13

标签: java swing user-interface jframe jpanel

我正在尝试关闭JPanel顶部的JFrame

现在,我想通过单击该面板顶部的按钮来关闭此面板,然后在同一框架上打开另一个面板。

我所有的面板和框架都在一个单独的类中。

这是我的主意:

JFrame

public MainFrame() throws IOException {
        InitializeStartScreen();

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setLocationRelativeTo(null);
        this.setIconImage(FrameTopIcon.getImage());
        this.setTitle("test");
        this.setVisible(true);
}

private void InitializeStartScreen() {
    StartPanel startPNL = new StartPanel();
    this.add(startPNL);
   // pack();
}

public static void main(String args[]) throws IOException {
    try {
        new MainFrame();
    }
    catch (IOException e) {
        throw  e;
    }
}

JPanel

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

public class StartPanel extends JPanel {
private LabelButtonCategory Button1         = new LabelButtonCategory("test");

public StartPanel() {
    this.setLayout(new GridBagLayout());

    InitializeLabelButtons();       

    this.setBackground(backgroundColor);
    this.add(gridPanel);
    this.setVisible(true);
}

    private void InitializeLabelButtons() {
         button1Panel.setBackground(backgroundColor);
         ImageIcon iconBtn1 = new ImageIcon("./src/images/CreateBill.png");
         Button1.setIcon(iconBtn1);
         Button1.addMouseListener(new java.awt.event.MouseAdapter() {
         public void mouseClicked(java.awt.event.MouseEvent evt) {
            Button1MouseClicked(evt);
         }
         public void mouseEntered(java.awt.event.MouseEvent evt) {
            Button1MouseEntered(evt);
         }
         public void mouseExited(java.awt.event.MouseEvent evt) {
            Button1MouseExited(evt);
         }
         });
        button1Panel.add(Button1);
        gridPanel.add(button1Panel);

   private void Button1MouseClicked(java.awt.event.MouseEvent evt) {
      // CLOSE THIS PANEL AND OPEN ANOTHER PANEL ON FRAME
   }

0 个答案:

没有答案