如何使用按钮打开新窗口或切换?

时间:2013-11-12 20:12:57

标签: java button canvas window awt

我试图弄清楚这个年龄,开始怀疑它是否可能!

我的应用程序有一个启动窗口 - 我需要它,以便当我单击我创建的按钮时,窗口关闭并打开一个新窗口或窗口调整大小并仅留下画布(准备放置新的小部件,精灵等...)。

我知道我需要一个处理程序事件,但我无法让代码工作。

1 个答案:

答案 0 :(得分:0)

我不太清楚你的问题是什么,但我用JFrame和3个按钮编写了一个例子。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class OpenWindowAndResizeWindow
{

    private JFrame  frame;
    private JButton btnOpenNewWindow;
    private JButton btnResizeWindow;
    private JButton btnRemoveAllButtons;

    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    OpenWindowAndResizeWindow window = new OpenWindowAndResizeWindow();
                    window.frame.setVisible(true);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public OpenWindowAndResizeWindow()
    {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize()
    {
        frame = new JFrame();
        frame.setBounds(100, 100, 300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(getBtnOpenNewWindow(), BorderLayout.NORTH);
        frame.getContentPane().add(getBtnResizeWindow(), BorderLayout.SOUTH);
        frame.getContentPane().add(getBtnRemoveAllButtons(), BorderLayout.CENTER);
        frame.setVisible(true);
    }
    private void openNewWindow()
    {
        OpenWindowAndResizeWindow newWindow = new OpenWindowAndResizeWindow();
        frame.dispose();

    }
    private void removeButtons()
    {
        getBtnOpenNewWindow().setVisible(false);
        getBtnRemoveAllButtons().setVisible(false);
        getBtnResizeWindow().setVisible(false);
    }
    private void resizeWindow()
    {
        Rectangle rectangle = frame.getBounds();
        rectangle.width = (int)rectangle.getWidth() + 100;
        rectangle.height = (int)rectangle.getHeight() + 100;
        frame.setBounds(rectangle);

    }

    private JButton getBtnOpenNewWindow() {
        if (btnOpenNewWindow == null) {
            btnOpenNewWindow = new JButton("Open new Window");
            btnOpenNewWindow.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    openNewWindow();
                }
            });
        }
        return btnOpenNewWindow;
    }
    private JButton getBtnResizeWindow() {
        if (btnResizeWindow == null) {
            btnResizeWindow = new JButton("Resize Window");
            btnResizeWindow.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    resizeWindow();
                }
            });
        }
        return btnResizeWindow;
    }
    private JButton getBtnRemoveAllButtons() {
        if (btnRemoveAllButtons == null) {
            btnRemoveAllButtons = new JButton("Remove All Buttons");
            btnRemoveAllButtons.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    removeButtons();
                }
            });
        }
        return btnRemoveAllButtons;
    }
}

此代码已准备好使用javac进行编译,或者只是将其粘贴到IDE中。
也许这有点帮助。 Java SE API Documentation也很有用。