Java GUI:如何打开现有表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20071090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:52:59  来源:igfitidea点击:

Java GUI: How do I open an existing Form?

javaformsswinguser-interface

提问by Villager

I'm trying to reach an existing form that I made from another form. I want to click a button, and make the other form appear. Here's what I wrote:

我正在尝试使用另一种形式制作的现有形式。我想单击一个按钮,然后显示另一个表单。这是我写的:

import javax.swing.JFrame;

public class CustomerUI extends JFrame {

    public CustomerUI(java.awt.Frame parent, boolean modal)
    {        
        initComponents();        
        setTitle("Customer Data Input");
        setLocationRelativeTo(null);
        pack();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    @SuppressWarnings("unchecked")

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Customer c = new Customer(jTextField1.getText(), jTextField2.getText(), jTextField3.getText());
        CinemaUI form = new CinemaUI();

        form.CUST.SetName(jTextField1.getText()); //Customer's Name
        form.CUST.SetID(jTextField2.getText()); //Customer's ID
        form.CUST.SetCard(jTextField3.getText()); //Customer's card
        form.NUM_OF_SEATS = Integer.parseInt(jTextField4.getText()); //Number of seats

        form.pack(); //Shows the next Form
        form.setVisible(true);
        CustomerUI.this.setVisible(false); //Hides this Form
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {


        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CustomerUI dialog = new CustomerUI(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }                  
}

CinemaUI is the name of the form I'd like to open.

CinemaUI 是我要打开的表单的名称。

After clicking jButton1 (the one I want to open the new window) I get the following error:

单击 jButton1(我想打开新窗口的那个)后,出现以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
    at my.Cinema.CinemaUI.<init>(CinemaUI.java:30)
    at my.Cinema.CustomerUI.jButton1ActionPerformed(CustomerUI.java:126)
    at my.Cinema.CustomerUI.access0(CustomerUI.java:11)
    at my.Cinema.CustomerUI.actionPerformed(CustomerUI.java:60)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access0(EventQueue.java:103)
    at java.awt.EventQueue.run(EventQueue.java:694)
    at java.awt.EventQueue.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue.run(EventQueue.java:708)
    at java.awt.EventQueue.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Edit:
I'll try explaining what my objective is: A certain person wants to order tickets to a movie. I save his data (Name, credit card, etc.) using textboxes, and proceed to selecting the person's seats. The first window I'm looking at is the one with the textboxes, and after I click a button I want the Window that shows a list of available seats to show up (CinemaUI).

编辑:
我会尝试解释我的目标是什么:某个人想要订购电影票。我使用文本框保存他的数据(姓名、信用卡等),然后继续选择该人的座位。我看到的第一个窗口是带有文本框的窗口,单击按钮后,我希望显示可用座位列表的窗口 ( CinemaUI)。

Thanks again for the help, I'm sorry its so long.

再次感谢您的帮助,抱歉这么久。

采纳答案by alex2410

I think you are looking for somthing like this:

我想你正在寻找这样的东西:

public class Form extends JFrame {

    public Form(){
        JButton show = new JButton("show Form2");
        show.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                new Form2();
                Form.this.setVisible(false);
            }

        });
        setTitle("Form 1");
        getContentPane().add(show,BorderLayout.CENTER);
        setLocationRelativeTo(null);
        pack();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    class Form2 extends JFrame {

        public Form2(){
            JButton show = new JButton("show Form");
            show.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    new Form();
                    Form2.this.setVisible(false);
                }

            });
            setTitle("Form 2");
            getContentPane().add(show,BorderLayout.CENTER);
            setLocationRelativeTo(null);
            pack();
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setVisible(true);
        }
    }


    public static void main(String[] args) {
        new Form();
    }
}

I hope it helps you.

我希望它能帮助你。

According you Formis form for credentials and Form2is CinemaUI.

根据您Form是凭据形式,Form2CinemaUI.

回答by Raveline

Errr... without any details on CinemaUI implementation, it's going to be a bit hard to help. Let's assume CinemaUI extends JFrame. Are you sure you called pack() on it first ? If it's not a frame, you're probably already in a frame of some sort... so you should add CinemaUI to it first.

呃……没有关于 CinemaUI 实现的任何细节,这将有点难以帮助。让我们假设 CinemaUI 扩展了 JFrame。你确定你先调用了 pack() 吗?如果它不是一个框架,那么您可能已经处于某种框架中……所以您应该先将 CinemaUI 添加到其中。

回答by Villager

I found my problem. I shouldv'e made a new JFrame instead of a new 'class'. That way it worked fine using this example:

我发现了我的问题。我应该创建一个新的 JFrame 而不是一个新的“类”。这样使用这个例子就可以正常工作:

Form1 form = new Form1();
form.setVisibile(true);
this.setVisibile(false);