Java GUI,根据actionListener更改面板 [英] Java GUI, Change Panel according to actionListener

查看:75
本文介绍了Java GUI,根据actionListener更改面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个不同的面板中添加了两个按钮,如果单击第一个按钮,则需要使用第二个按钮进入下一个面板。但是当我点击第一个按钮时,按钮没有被替换。

I have two buttons added in two different panels, if first button is clicked then it need to take to next panel with the second button in it. But the button was not replaced when I click first button.

/*Java GUI*/

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


public class TestFrame extends JFrame{

    private JPanel panel1, panel2;
    private JButton but,but2; 

    public TestFrame()
    {
        createPanel();
        addPanel();
    }

    private void createPanel()
    {
        panel1 = new JPanel();
        but = new JButton("TestButton");
        but.addActionListener(new addButtonListener());

        panel2 = new JPanel();
        but2 = new JButton("TestButton2");

    }

    private void addPanel()
    {
        panel1.add(but);
        panel2.add(but2);

        add(panel1);

    }

    class addButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent ae) 
        {
            getContentPane().removeAll();
            add(panel2);

            repaint();
        }
    }


    public static void main(String args[])
    {
        JFrame frame = new TestFrame();
        frame.setTitle("Test Software");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);

        frame.setVisible(true);
    }



}


推荐答案

从contentPane中重新启动所有内容后,尝试将面板添加到ContentPane。第二件事是重新粉刷。如果您不更新面板内容,则会在调整大小后进行绘制。这里是示例解决方案:

After remowing all from contentPane, try add panel to ContentPane. Second thing is repainting. If you will not update panel content, it will be painted after resize. Here you are example solution:



    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import javax.swing.*;

    public class Frame extends JFrame{
        private JPanel panel1, panel2;
        private JButton but,but2; 
        public Frame()
        {
           createPanel();
           addPanel();
        }
        private void createPanel()
        {
            panel1 = new JPanel();
            but = new JButton("TestButton");
            but.addActionListener(new addButtonListener());
            but.setBounds(50, 90, 190, 30);//There are example values but remember about setting size
            panel2 = new JPanel();
            but2 = new JButton("TestButton2");
            but2.setBounds(50, 50, 90, 30);//There are example values but remember about setting size
        }
        private void addPanel()
        {
            panel1.add(but);
            panel2.add(but2);
            add(panel1);
        }

        class addButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent ae) 
            {
                getContentPane().removeAll();
                getContentPane().add(panel2);//Adding to content pane, not to Frame
                repaint();
                printAll(getGraphics());//Extort print all content
            }
        }

        public static void main(String args[])
        {
            Frame frame = new Frame();
            frame.setTitle("Test Software");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(500,500);
            frame.setVisible(true);
        }

    }

Oracle docs解释差异beetwen直接添加到contentPane或Frame。

http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html

Oracle docs explains difference beetwen adding to contentPane or to Frame directly.
http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html

这篇关于Java GUI,根据actionListener更改面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆