java - 在 JPanel 上使用 Jbutton 隐藏/显示 JFrame

标签 java swing jframe

在我的 Swing 应用程序中,我有 2 个 JFrame A 和 B。当我单击 JFrame A 上的按钮时,它会打开 JFrame B 并隐藏自身(我设法做到了这部分)

在 JFrame B 上,我在 JTabbedPane 上放置了 4 个 JPanel。每个 JPanel 有 2 个 JButton。

当我单击 JPanel 上的 Jbutton 并再次显示 Jframe A 时,我试图隐藏 JFrame B。

我该怎么做?

//JPanel 类

公共(public)类 AddItemPanel 扩展了 javax.swing.JPanel {

  public AddItemPanel() {
        initComponents();
  }

  private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          

      if(evt.getSource() == btnCancel)
      {
        ItemFrame d = new ItemFrame();      
             d.setVisible(false);// not working
             this.setVisible(false);// not working
      }
}

}

//JFrame 类

公共(public)类 ItemFrame 扩展 javax.swing.JFrame {

  public ItemFrame() {

        initComponents();
        jTabbedPane1.add("Add Items",new AddItemPanel());
        jTabbedPane1.add("Delete Items",new DeleteItemPanel());
        jTabbedPane1.add("Update Items",new UpdateItemPanel());
        jTabbedPane1.add("Search Items",new SearchItemPanel());
 }

}

最佳答案

尝试这个例子希望它对你有用

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

public class JframeTest implements ActionListener
{
    JButton b1;
    JButton b2;
    JFrame f1 ;
    JFrame f2;

public void init()
{
 f1 = new JFrame("Frame one");
 f2 = new JFrame("Frame two");

 f1.setSize(400,400);
 f2.setSize(400,400);

 f1.setLayout(new FlowLayout());
 f2.setLayout(new FlowLayout());

 b1 = new JButton("Open Frame two");
 b2= new JButton("Open Fram one");
 JPanel p1 = new JPanel();
 JPanel p2 = new JPanel();

 p1.setBackground(Color.white);
 p2.setBackground(Color.white);
 p1.add(b1);
 p2.add(b2);

f1.getContentPane().add(p1);
f2.getContentPane().add(p2);

f1.setVisible(true);
f2.setVisible(false);
f1.setDefaultCloseOperation(3);
f2.setDefaultCloseOperation(3);

b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
    if(evt.getSource() == b1)
    {
        f1.setVisible(false);
        f2.setVisible(true);
    }else if(evt.getSource()==b2)
    {
        f1.setVisible(true);
        f2.setVisible(false);
    }

}

public JframeTest()
{
    this.init();
}
public static void main(String...argS)
{
    new JframeTest();
}
}

关于java - 在 JPanel 上使用 Jbutton 隐藏/显示 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15051247/

相关文章:

java - 从 Autowiring bean 获取数据源时出现空指针异常

java - 当一个数据库发生变化时,自动更新jar文件中的所有数据库

java - 如何在 Java Swing 中对齐组件?

java - 显示 javax.swing 组件(特别是 JPanel)时出现问题

java - Perf4j 私有(private)方法的 @profiled 注释

java - 销毁当前类对象并通过线程运行同一个类

java - 在 ParallelFlux 完成时执行任务而不执行顺序(引入副作用)

java - 绘制扩展 JPanel 的类

java - 图形程序适用于 Windows,但不适用于 Mac

Java:如何在 JFrame/JPanel 中打开 Google 之类的网页?