如何在不从另一个类创建新窗口的情况下添加按钮

时间:2015-03-11 17:47:10

标签: java awt

也许我理解这个概念是完全错误的,但我正在尝试为按钮创建类,并让它们在主GUI类中调用,以便它们出现在该窗口上,但我并不完全知道如何调用这些按钮类到主GUI类。

我目前只是通过创建一个新对象来创建它们,但这会创建一个新窗口,而不是将其添加到我的主GUI类中当前存在的窗口中。我该如何解决这个问题,还是我完全错了?


GUI类

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

            class GUI extends Frame implements WindowListener
            {

                public GUI()
                {
                    super("GUI");

                    setLayout(new FlowLayout());
                    addWindowListener(this);


                }

                public void windowDeiconified(WindowEvent event){}
                public void windowIconified(WindowEvent event){} 
                public void windowActivated(WindowEvent event){} 
                public void windowDeactivated(WindowEvent event){}
                public void windowOpened(WindowEvent event){} 
                public void windowClosed(WindowEvent event)  {}
                public void windowClosing(WindowEvent e) { System.exit(0); }

                public static void main(String[] args)
                {
                    GUI screen = new GUI();

                    new Button1("TEST");

                    screen.setSize(500,100);
                    screen.setLocationRelativeTo(null);
                    screen.setVisible(true);
                }
            }

Button1类

            import java.awt.*;
            import java.awt.event.*;

            public class Button1 extends GUI implements ActionListener {

                private Button addButton;

                public Button1(String s) {
                    addButton = new Button(s);
                    add(addButton);
                    addButton.addActionListener(this);
                    setVisible(true);
                    System.out.println("It atleast works here");
                }

                public void actionPerformed(ActionEvent e) {
                    System.out.println("It works - Button1");
                }

                public static void main(String[] args) {

                }
            }

1 个答案:

答案 0 :(得分:0)

首先:为什么你真的想为每个按钮创建新的类? 第二:是的,我认为你没有做到这一点。这段代码应该做你想要的。如果你没有得到它,只需阅读或观看有关Java中面向对象编程的教程。

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

        class GUI extends Frame implements WindowListener
        {

            public GUI()
            {
                super("GUI");

                setLayout(new FlowLayout());
                addWindowListener(this);
            }

            public void windowDeiconified(WindowEvent event){}
            public void windowIconified(WindowEvent event){} 
            public void windowActivated(WindowEvent event){} 
            public void windowDeactivated(WindowEvent event){}
            public void windowOpened(WindowEvent event){} 
            public void windowClosed(WindowEvent event)  {}
            public void windowClosing(WindowEvent e) { System.exit(0); }

            public static void main(String[] args)
            {
                GUI screen = new GUI();

                screen.setSize(500,100);
                Button button1 = new Button("Button 1");
                button1.addActionListener(new ActionListener() {
                    System.out.println("It works - Button1");
                });
                screen.add(button1);
                screen.setLocationRelativeTo(null);
                screen.setVisible(true);
            }
        }