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

时间:2013-11-19 12:04:18

标签: java forms swing user-interface

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

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是我想要打开的表单的名称。

单击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.access$100(CustomerUI.java:11)
    at my.Cinema.CustomerUI$2.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.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.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)

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

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

3 个答案:

答案 0 :(得分:2)

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

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();
    }
}

我希望它可以帮到你。

根据您Form提供凭据表格,Form2CinemaUI

答案 1 :(得分:0)

错误...没有关于CinemaUI实现的任何细节,它将有点难以帮助。我们假设CinemaUI扩展了JFrame。你确定先打电话给pack()吗?如果它不是一个框架,你可能已经处于某种框架......所以你应该首先添加CinemaUI。

答案 2 :(得分:0)

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

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