如何在按钮事件Java上更改框架

时间:2016-04-19 14:39:57

标签: java

我正在制作一个简单的项目。它有这样的登录窗口enter image description here

当用户点击按钮登录时 - 它应该"重绘"窗口(它应该发生在同一个窗口中)然后窗口看起来像这样。 enter image description here

问题是 - 我无法重拍"窗口 - 我唯一能做的 - 它创建了一个新的框架,所以实际上总共有两个框架。 如何将整个事物放在同一个框架中。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;

public class Client
{
    private JFrame frame;
    private JTextArea allMessagesArea;
    private JTextArea inputArea;
    private JButton buttonSend;
    private JButton buttonExit;
    private String login;

    public  void addComponentsToPane(Container pane)
    {
        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;

        allMessagesArea = new JTextArea(25,50);
        c.weighty = 0.6;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx=0;
        c.gridy=0;
        c.gridwidth=2;
        pane.add(allMessagesArea, c);


        inputArea = new JTextArea(12,50);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth=2;
        c.weighty =0.3;
        c.gridx =0;
        c.gridy =1;
        pane.add(inputArea, c);

        buttonSend = new JButton("Send");
        c.weightx=0.5;
        c.weighty = 0.1;
        c.fill =  GridBagConstraints.HORIZONTAL;
        c.gridx =0;
        c.gridy=2;
        c.gridwidth =1;
        pane.add(buttonSend, c);


        buttonExit = new JButton("Exit");
        c.weightx =0.5;
        c.weighty = 0.1;
        c.fill =  GridBagConstraints.HORIZONTAL;
        c.gridx =1;
        c.gridy=2;
        c.gridwidth =1;
        pane.add(buttonExit, c);
    }


    public Client()
    {
        frame = new JFrame("Simple Client");

        frame.setSize(400,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        welcomePage();



        frame.setVisible(true);

    }

    public void welcomePage()
    {
        JPanel panel = new JPanel();
        JLabel label = new JLabel("Your login:");
        panel.add(label);

        JTextField textField = new JTextField(15);
        panel.add(textField);

        JButton loginButton = new JButton("log in");
        panel.add(loginButton);

        JButton exitButton = new JButton("exit");
        panel.add(exitButton);
        frame.add(panel, BorderLayout.CENTER);

        loginButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if(textField.getText().isEmpty())
                    JOptionPane.showMessageDialog(frame.getContentPane(), "Please enter your login");
                else
                {
                    login = textField.getText();
                    System.out.println(login);
                    frame = null;
                    frame = new JFrame("Simple Client");
                    frame.setSize(400,500);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    addComponentsToPane(frame.getContentPane());
                    frame.pack();
                    frame.setVisible(true);

                }
            }
        });

        exitButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });

    }

    public static void main(String[] args)
    {

        Client frame = new Client();
    }
}

2 个答案:

答案 0 :(得分:2)

Use CardLayout

此布局允许开发人员在面板之间切换。它的工作原理是创建一个“甲板”面板,它将包含可能显示的所有面板:

CardLayout layout = new CardLayout();
JPanel deck = new JPanel();
deck.setLayout(layout);

JPanel firstCard = new JPanel();
JPanel secondCard = new JPanel();
deck.add(firstCard, "first");
deck.add(secondCard, "second");

当您点击某个按钮时,该按钮ActionListener应拨打show(Container, String)上的next(Container)previous(Container)CardLayout来切换正在显示的面板:

public void actionPerformed(ActionEvent e) {
    layout.show(deck, "second");
}

答案 1 :(得分:1)

解决方案之一

您可以创建两个面板(每个视图一个)并向其添加所需的组件。首先,您将第一个面板添加到frame(使用frame.add(panel1))。如果要在同一窗口中显示第二个面板,可以删除第一个面板(使用frame.remove(panel1))并添加第二个面板(使用frame.add(panel2))。最后,您需要拨打frame.pack()

这是您的代码与上述解决方案:

public class Client
{
    private JFrame frame;
    private JTextArea allMessagesArea;
    private JTextArea inputArea;
    private JButton buttonSend;
    private JButton buttonExit;
    private String login;

    public  void addComponentsToPanel2()
    {               
        panel2.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;

        allMessagesArea = new JTextArea(25,50);
        c.weighty = 0.6;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx=0;
        c.gridy=0;
        c.gridwidth=2;
        panel2.add(allMessagesArea, c);


        inputArea = new JTextArea(12,50);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth=2;
        c.weighty =0.3;
        c.gridx =0;
        c.gridy =1;
        panel2.add(inputArea, c);

        buttonSend = new JButton("Send");
        c.weightx=0.5;
        c.weighty = 0.1;
        c.fill =  GridBagConstraints.HORIZONTAL;
        c.gridx =0;
        c.gridy=2;
        c.gridwidth =1;
        panel2.add(buttonSend, c);


        buttonExit = new JButton("Exit");
        c.weightx =0.5;
        c.weighty = 0.1;
        c.fill =  GridBagConstraints.HORIZONTAL;
        c.gridx =1;
        c.gridy=2;
        c.gridwidth =1;
        panel2.add(buttonExit, c);   
    }


    public Client()
    {
        frame = new JFrame("Simple Client");

        frame.setSize(400,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        welcomePage();

        frame.setVisible(true);
    }

    public void welcomePage()
    {
        panel1 = new JPanel();
        JLabel label = new JLabel("Your login:");
        panel1.add(label);

        JTextField textField = new JTextField(15);
        panel1.add(textField);

        JButton loginButton = new JButton("log in");
        panel1.add(loginButton);

        JButton exitButton = new JButton("exit");
        panel1.add(exitButton);
        frame.add(panel1, BorderLayout.CENTER);

        loginButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if(textField.getText().isEmpty())
                    JOptionPane.showMessageDialog(frame.getContentPane(), "Please enter your login");
                else
                {
                    login = textField.getText();
                    System.out.println(login);

                    panel2 = new JPanel();
                    addComponentsToPanel2();
                    frame.remove(panel1);
                    frame.add(panel2);
                    //frame.repaint();
                    frame.pack();
                }
            }
        });

        exitButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });

    }

    public static void main(String[] args)
    {

        Client frame = new Client();
    }

    private JPanel panel1;
    private JPanel panel2;
}