在我的 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/