我有一个带有Jframe
属性的主JPanel
:
public class Principal extends javax.swing.JFrame
{
private javax.swing.JPanel jPanel1;
private javax.swing.JButton jButton3;
public Principal()
{
initComponents();
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
addPerson p = new addPerson();
jPanel1.add(p);
p.setVisible(true);
jPanel1.setVisible(true);
}
}
当我按jButton3ActionPerformed
时,我想知道如何在JFrame
中显示位于同一个包中的另一个面板(但是不同的文件)?
答案 0 :(得分:0)
一种可能的解决方案可能如下所示:
我创建了你的PricipalPanel作为扩展JPanel的自己的类,我也使用AddPerson Panel做了这个。然后你可以在你的MainFrame中添加一个JMenuBar并在那里显示JMenuItems,以便切换面板。
MainFrame类,此处为revalidateView(JPanel面板)方法从JFrame中删除所有组件并添加新组件并调用revalidate。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
/**
*
* @author Patrick Ott <Patrick.Ott@professional-webworkx.de>
* @version 1.0
*/
public class MainFrame extends javax.swing.JFrame implements ActionListener {
private JMenuItem addPersonItem;
private JMenuItem showPrincipalItem;
private JPanel jPanel1;
public MainFrame() {
this.setSize(new Dimension(1024, 768));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setJMenuBar(initMenuBar());
this.add(initComponents());
}
private JPanel initComponents() {
this.jPanel1 = new JPanel(new BorderLayout(10, 10));
this.jPanel1.add(new PricipalPanel(), BorderLayout.CENTER);
return jPanel1;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(addPersonItem)) {
revalidateView(new AddPerson());
}
if(e.getSource().equals(showPrincipalItem)) {
revalidateView(new PricipalPanel());
}
}
private void revalidateView(final JPanel jPanel) {
this.getContentPane().removeAll();
this.setJMenuBar(initMenuBar());
this.add(jPanel);
this.revalidate();
}
private JMenuBar initMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu changeJPanel = new JMenu("Change Panels");
addPersonItem = new JMenuItem("add PersonPanel");
addPersonItem.addActionListener(this);
showPrincipalItem = new JMenuItem("show Pricipal Panel");
showPrincipalItem.addActionListener(this);
changeJPanel.add(addPersonItem);
changeJPanel.add(showPrincipalItem);
menuBar.add(changeJPanel);
return menuBar;
}
}
AddPerson面板
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class AddPerson extends JPanel {
public AddPerson() {
this.setLayout(new BorderLayout(10, 10));
this.setPreferredSize(new Dimension(1024, 768));
this.add(new JLabel("I am the Person Panel"));
}
}
The PricipalPanel
import javax.swing.JLabel;
import javax.swing.JPanel;
class PricipalPanel extends JPanel {
public PricipalPanel() {
this.add(new JLabel("Hej there, i am the Pricipal Panel, i have to display this Text because the initComponents Method wasn't posted on StackOverflow!"));
}
}
启动它
import javax.swing.SwingUtilities;
public class AddJPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MainFrame p = new MainFrame();
});
}
}
尝试一下,如果您有疑问,请询问。
帕特里克