编写actionListener以在确认时在控制台上显示表单输入

时间:2014-11-28 14:44:23

标签: java user-interface

我目前正在构建一个GUI,允许我向系统添加新的游轮。我想编写一个actionListner,一旦用户点击" ok"然后表单输入将在控制台上显示为输出。

我目前有以下草稿来完成此任务:

    ok = new JButton("Add Cruise");
            ok.setToolTipText("To add the Cruise to the system");
            ok.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event){
                int selected = typeList2.getSelectedIndex();
                String tText = typeList2[selected];
                Boolean addtheCruise = false;
                addtheCruise = fleet.addCruise(); 

                fleet.printCruise();

                if (addtheCruise)
                { 
                    frame.setVisible(false);
                }
                else
                {   // Report error, and allow form to be re-used
                    JOptionPane.showMessageDialog(frame,
                    "That Cruise already exists!", "Error",
                    JOptionPane.ERROR_MESSAGE);
                }                       
            }
        });        
        buttonPanel.add(ok);

以下是Form输入框的其余代码:

 contentPane2 = new JPanel (new GridLayout(18, 1)); //row, col 
            nameInput = new JLabel ("Input Cruise Name:");
            nameInput.setForeground(normalText);
            contentPane2.add(nameInput);
            Frame2.add(contentPane2);

            Cruisename = new JTextField("",10);
            contentPane2.add(Cruisename);
            Frame2.add(contentPane2, BorderLayout.CENTER);
            Frame2.setVisible(true);

            confirmPanel = new JPanel ();
            confirmPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
            confirmPanel.setBorder(BorderFactory.createLineBorder(Color.PINK));

            addItinerary = new JButton("Add Itinerary");
            confirmPanel.add(Add);
            confirmPanel.add(addItinerary );
            Frame2.add(confirmPanel, BorderLayout.PAGE_END);
            Frame2.setVisible(true);

                contentPane2.add(Cruisename);
                Frame2.add(contentPane2);

            // add spacing between comboboxes
            contentPane2.add(Box.createRigidArea(new Dimension(5,0)));

            contentPane2.setBorder(BorderFactory.createLineBorder(Color.black));

           //Label for start and end location jcombobox
            CruiseMessage = new JLabel ("Enter Start and End Location of new Cruise:");
            CruiseMessage.setForeground(normalText);
            contentPane2.add(CruiseMessage);
            Frame2.add(contentPane2);               

            /**
             * creating start location JComboBox
             */

            startL = new JComboBox();
            final JComboBox typeList2;
            final String[] typeStrings2 = {
                "Select Start Location", "Tobermory","Oban", "Isle of Mull", "Isle of Harris",
                "Lewis and Harris", "Stornoway", "Skye", "Portree"};

            startL = new JComboBox(typeStrings2);

            contentPane2.add(startL);
            Frame2.add(contentPane2, BorderLayout.CENTER);
            Frame2.setVisible(true);

            // add spacing between comboboxes
            contentPane2.add(Box.createRigidArea(new Dimension(5,0)));

            /**
             * creating end location JComboBox
             */

            endL = new JComboBox();
            final JComboBox typeList3;
            final String[] typeStrings3 = {
                "Select End Location", "Tobermory","Oban", "Isle of Mull", "Isle of Harris",
                "Lewis and Harris", "Stornoway", "Skye", "Portree"};

            endL = new JComboBox(typeStrings3);

            contentPane2.add(endL);

            Frame2.add(contentPane2, BorderLayout.CENTER);
            Frame2.setVisible(true);

            // add spacing between comboboxes
            contentPane2.add(Box.createRigidArea(new Dimension(5,0)));

              //Label for select ship jcombobox
            selectShipM = new JLabel ("Select Ship to assign Cruise:");
            selectShipM.setForeground(normalText);
            contentPane2.add(selectShipM);
            Frame2.add(contentPane2);   

            /**
             * creating select ship JCombobox
             * select ship to assign cruise
             */

             selectShip = new JComboBox();
            final JComboBox typeList4;
            final String[] typeStrings4 = {
                "Select Ship", "Dalton Princess", "Stafford Princess" };

            selectShip = new JComboBox(typeStrings4);

            contentPane2.add(selectShip);
            Frame2.add(contentPane2, BorderLayout.CENTER);
            Frame2.setVisible(true);

我需要上面代码中的所有表单输入在完成后显示在控制台上。

要点: 我在一支舰队中有两艘船 2.要添加新巡航,必须选择所有字段(名称,开始日期,结束日期,发货)。

问题: 我在制作&#34时不断出现错误。 fleet = new Fleet();"在我的构造函数中。即使我在课堂上宣布了这一点。 2.在下面的草案代码中,第5行表示" typeList2"但是,我有两个JComboBox&# - 两个不同类型的字符串用于下拉菜单' s(其余部分显示)代码)。如何将两个typeLists输入到输出而不仅仅是一个?

谢谢。

0 个答案:

没有答案