在一个框架中使用多个面板

时间:2013-04-25 12:28:51

标签: java swing layout frames multiple-instances

我有一个问题,我希望程序在同一帧中的多个面板之间切换。我遇到的问题是,当面板切换时以及切换内容逐像素降低后,我无法设置布局。这是我的代码。

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;

public class Main {

    public static boolean logged_in = false;

    public static int width = 200, height = 400;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Dimension d = new Dimension(width, height);
                    First frame = new First();
                         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setSize(d);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                    frame.setResizable(true);
                    frame.setLayout(new FlowLayout());  
                } catch (Exception e) { 
                    e.printStackTrace();    
                }
            }
        });
    }
}

以下是保存帧的两个类。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class First extends JFrame {

    JPanel contentPane;
    private JButton button_1;
    private JTextField text;

    public First(){

        contentPane = new JPanel();
        setContentPane(contentPane);

        button_1 = new JButton("Second frame");
        button_1.setVisible(true);
        text = new JTextField(20);
        text.setVisible(true);

        contentPane.add(button_1);
        contentPane.add(text);

        thehandler handler = new thehandler();

        button_1.addActionListener(handler);
    }

    private class thehandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {

            if (event.getSource() == button_1) {
                contentPane.removeAll();
                contentPane.invalidate();
                Second frame = new Second();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setResizable(true);
                frame.setLayout(new FlowLayout());
                frame.contentPane.setVisible(true);
                contentPane.add(frame.contentPane);
                ((JPanel) contentPane).revalidate();
                contentPane.setSize(200, 400);
                contentPane.repaint();
            }
        }
    }
}

和第二个

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Second extends JFrame {

    JPanel contentPane;
    private JButton button_1;
    private JTextField text;

    public Second(){

        contentPane = new JPanel();
        setContentPane(contentPane);

        button_1 = new JButton("First frame");
        button_1.setVisible(true);
        text = new JTextField(20);
        text.setVisible(true);

        contentPane.add(button_1);
        contentPane.add(text);

        thehandler handler = new thehandler();

        button_1.addActionListener(handler);
    }

    private class thehandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {

            if (event.getSource() == button_1) {
                contentPane.removeAll();
                contentPane.invalidate();
                First frame = new First();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setResizable(true);
                frame.setLayout(new FlowLayout());
                frame.contentPane.setVisible(true);
                contentPane.add(frame.contentPane);
                ((JPanel) contentPane).revalidate();
                contentPane.setSize(200, 400);
                contentPane.repaint();
            }
        }
    }
}

感谢任何帮助。请记住,我对java GUI不太满意。 TY。

编辑经过大量时间寻找答案,我得到了一个。它可能并不完美但我会将其发布以供将来参考,或者是否有其他人需要此解决方案。这是代码。

保持面板的主框架:

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame extends JFrame {

    public static JPanel main_panel;
    private static FirstFrame first;

    public MainFrame(){

        setLayout(null);    
        setSize(400, 400);
        setLocationRelativeTo(null);
        setResizable(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        main_panel = new JPanel();
        main_panel.setBounds(0, 0, 400, 400);
        add(main_panel);

        main_panel.invalidate();
        main_panel.removeAll();

        first = new FirstFrame();
        main_panel.add(first);
        main_panel.revalidate();
        main_panel.repaint();
    }

    public static void main(String[] args) {
        MainFrame frame = new MainFrame();
        frame.setVisible(true);
    }
}

第一小组:

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

public class FirstFrame extends JPanel {

    private JButton button;

    public FirstFrame() {

        JPanel panel = new JPanel();

        panel.setPreferredSize(new Dimension(400, 400));
        panel.setLayout(null);

        button = new JButton("First");
        button.setBounds(20, 20, 200, 40);
        button.setVisible(true);

        thehandler handler = new thehandler();

        button.addActionListener(handler);

        panel.add(button);

        add(panel);
        revalidate();
        repaint();
    }
    private class thehandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == button) {
                MainFrame.main_panel.invalidate();
                MainFrame.main_panel.removeAll();
                SecondFrame frame = new SecondFrame();
                MainFrame.main_panel.add(frame);
                MainFrame.main_panel.revalidate();
                MainFrame.main_panel.repaint();
            }
        }
    }
}

第二

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

public class SecondFrame extends JPanel {

    private JButton button;

    public SecondFrame() {
        JPanel panel = new JPanel();

        panel.setPreferredSize(new Dimension(400, 400));
        panel.setLayout(null);

        button = new JButton("Second");
        button.setBounds(20, 20, 200, 40);
        button.setVisible(true);

        thehandler handler = new thehandler();

        button.addActionListener(handler);

        panel.add(button);

        add(panel);
        revalidate();
        repaint();
    }
    private class thehandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == button) {
                MainFrame.main_panel.invalidate();
                MainFrame.main_panel.removeAll();
                ThirdFrame frame = new ThirdFrame();
                MainFrame.main_panel.add(frame);
                MainFrame.main_panel.revalidate();
                MainFrame.main_panel.repaint();
            }
        }
    }
}

第三名:

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

public class ThirdFrame extends JPanel {

    private JButton button;

    public ThirdFrame() {
        JPanel panel = new JPanel();

        panel.setPreferredSize(new Dimension(400, 400));
        panel.setLayout(null);

        button = new JButton("Third");
        button.setBounds(20, 20, 200, 40);
        button.setVisible(true);

        thehandler handler = new thehandler();

        button.addActionListener(handler);

        panel.add(button);

        add(panel);
        revalidate();
        repaint();
    }
    private class thehandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == button) {
                MainFrame.main_panel.invalidate();
                MainFrame.main_panel.removeAll();
                FirstFrame frame = new FirstFrame();
                MainFrame.main_panel.add(frame);
                MainFrame.main_panel.revalidate();
                MainFrame.main_panel.repaint();
            }
        }
    }
}

如您所见,您可以从面板1 - > 2 - > 3切换回1而不是第二个。 你们的答案都很有帮助。欢迎任何进一步的建议。

1 个答案:

答案 0 :(得分:1)

解决方案是使用卡片布局。这很简单,

  1. 创建一个具有卡片布局的面板,其中添加了其他子面板 具有唯一字符串引用。
  2. 调用CardLayout的show方法 选择一个面板拿在前面 (docs
  3. 检查以下代码。

      final JPanel mainPanel=new JPanel();
        JPanel panelOne=new JPanel();
        JPanel panelTwo=new JPanel();
        final CardLayout card=new CardLayout();
        mainPanel.setLayout(card);
        mainPanel.add(panelOne, "one"); // id one refers panelOne
        mainPanel.add(panelTwo, "two"); // id two refers panelTwo
    
        panelOne.add(new JLabel("first panel"));
        JButton btn1=new JButton("Show second");
        panelOne.add(btn1);
        btn1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae){
        card.show(mainPanel, "two"); // shows panelTwo
        }});
    
      panelTwo.add(new JLabel("second panel"));
        JButton btn2=new JButton("Show First");
        panelTwo.add(btn2);
        btn2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae){
        card.show(mainPanel, "one"); // shows panelOne
        }});