在动作事件中查找区域

时间:2016-06-19 15:11:12

标签: java user-interface floor

我正在为课程编写一个程序,而且我已经完成了所有这些程序,但有一部分让我很困惑。

我必须创建一个地板系统,人们可以选择地板类型,输入地板的长度和宽度,然后计算并生成订单摘要。

现在,我能够得到所有这些,但计算正确(我甚至没有开始使用数据库连接。)

我的教授对她的指示极其模糊,甚至对她的问题的答案都是模糊的,所以作为最后的努力,我想我会在这里尝试。

我不能为我的生活弄明白所以任何帮助都非常感激。

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class FloorMart
{

    private static JFrame frame = null;

    private static Integer cost;
    private static String floorSize;
    private static String floorType;
    private double floorLength;
    private double floorWidth;


    public static void main(String[] args)
    {

        frame = new JFrame("FloorMart");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(320, 300);

        JTabbedPane tPane = new JTabbedPane();

        JPanel panel1 = new JPanel();
        panel1.setLayout(new FlowLayout());
        panel1.setPreferredSize(new Dimension(200, 200));

        JLabel jLabel = new JLabel();
        jLabel.setText("Welcome to the FloorMart ordering system! ");
        panel1.add(jLabel);

        JLabel jLabel1 = new JLabel();
        jLabel1.setText("Enter your Name: ");
        panel1.add(jLabel1);

        JTextField text1 = new JTextField(10);
        panel1.add(text1);

        JLabel jLabel2 = new JLabel();
        jLabel2.setText("Enter your Pnone Number: ");
        panel1.add(jLabel2);

        JTextField text2 = new JTextField(10);
        panel1.add(text2);

        tPane.addTab("Customer", panel1);

        JPanel panel2 = new JPanel();
        panel2.setLayout(new FlowLayout());
        panel2.setPreferredSize(new Dimension(200, 200));

        jLabel = new JLabel();
        jLabel.setText("Floor Type? ");
        panel2.add(jLabel);

        ButtonGroup group = new ButtonGroup();
        JRadioButton RadioButton = new JRadioButton("Carpet - $10 per sq ft",               true);
        panel2.add(RadioButton);
        group.add(RadioButton);

        JRadioButton RadioButton1 = new JRadioButton("Hardwood - $20 per sq ft");
        panel2.add(RadioButton1);
        group.add(RadioButton1);

        tPane.addTab("Floor Type", panel2);

        JPanel panel3 = new JPanel();
        panel3.setLayout(new FlowLayout());
        panel3.setPreferredSize(new Dimension(200, 200));

        jLabel = new JLabel();
        jLabel.setText("Enter the length and width of your floor! ");
        panel3.add(jLabel);

        JLabel jLabel3 = new JLabel();
        jLabel3.setText("Enter the floor length: ");
        panel3.add(jLabel3);

        JTextField length = new JTextField(10);
        panel3.add(length);
        //length.setText(cost.toString());  //


        JLabel jLabel4 = new JLabel();
        jLabel4.setText("Enter the floor width: ");
        panel3.add(jLabel4);

        JTextField width = new JTextField(10);
        panel3.add(width);
        //width.setText(cost.toString()); //


        tPane.addTab("Floor Size", panel3);

        JPanel panel4 = new JPanel();
        panel4.setLayout(new FlowLayout());
        panel4.setPreferredSize(new Dimension(200, 200));

        jLabel = new JLabel();
        jLabel.setText("Total Cost: ");
        panel4.add(jLabel);

        JTextField text3 = new JTextField(10);
        panel4.add(text3);

       JButton button = new JButton("Order Summary");
       button.addActionListener(new ActionListener()
       {

           @Override
           public void actionPerformed(ActionEvent e)
           {
               JPanel panelNew = new JPanel();
               panelNew.setLayout(new BoxLayout(panelNew, BoxLayout.Y_AXIS));

               JLabel jLabeln = new JLabel("Order Summary");
               JLabel jLabeln1 = new JLabel("Customer Name: " + text1.getText());
               JLabel jLabeln2 = new JLabel("Phone Number: " + text2.getText());
               JLabel jLabeln3 = new JLabel("Floor Type: " + floorType);
               JLabel jLabeln4 = new JLabel("Floor Area: " + floorSize);
               JLabel jLabeln5 = new JLabel("Total: $" + new Double(cost) + "0");
               JLabel jLabeln6 = new JLabel("Thank you for shopping at FloorMart!");

                panelNew.add(jLabeln);
                panelNew.add(jLabeln1);
                panelNew.add(jLabeln2);
                panelNew.add(jLabeln3);
                panelNew.add(jLabeln4); 
                panelNew.add(jLabeln5);
                panelNew.add(jLabeln6);

                frame.invalidate();
                frame.remove(panel1);frame.remove(panel2);frame.remove(panel3);
                frame.remove(panel4);
                frame.remove(tPane);

                frame.add(panelNew);
                frame.revalidate();
                frame.repaint();

           }


       });

         panel4.add(button);
         tPane.addTab("Total", panel4);


         ChangeListener changeListener= new ChangeListener()
         {
             public void stateChanged(ChangeEvent changeEvent)
             {

                 JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
                 int index = sourceTabbedPane.getSelectedIndex();

                 if(index == 2)
                 {
                     double floorLength = Double.parseDouble(length.getText());
                     double floorWidth = Double.parseDouble(width.getText());
                     floorSize = floorLength * floorWidth;
                     if(RadioButton.isSelected())
                     {
                         cost = 10; //per sq ft
                         floorType = RadioButton.getText();

                     }
                     else if(RadioButton1.isSelected())
                     {
                         cost = 20; //per sq ft
                         floorType = RadioButton1.getText();
                     }

                     text3.setText(cost.toString());

                 }
             }

         };

         tPane.addChangeListener(changeListener);

         frame.add(tPane);
         frame.setVisible(true);

    }

}

1 个答案:

答案 0 :(得分:0)

我可以看到两个需要改变的基本内容:
一个。这不编译(floorSize应该是double)

     String floorSize;
     double floorLength = Double.parseDouble(length.getText());
     double floorWidth = Double.parseDouble(width.getText());
     floorSize = floorLength * floorWidth;

湾不应由ChangeListener触发floorSize的计算(这会导致计算在输入值之前完成)。 使用"订单摘要" JButton触发计算。

这是代码的工作版本。看我的评论:

    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.BoxLayout;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextField;

    public class FloorMart
    {

        private static JFrame frame = null;

        private static Integer cost;  //better use int

        //////////////////////////////////////
        //changed from string to double
        private static double floorSize;
        ////////////////////////////////////
        private static String floorType;
        private double floorLength;
        private double floorWidth;


        public static void main(String[] args)
        {

            frame = new JFrame("FloorMart");

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(320, 300);

            JTabbedPane tPane = new JTabbedPane();

            JPanel panel1 = new JPanel();
            panel1.setLayout(new FlowLayout());
            panel1.setPreferredSize(new Dimension(200, 200));

            JLabel jLabel = new JLabel();
            jLabel.setText("Welcome to the FloorMart ordering system! ");
            panel1.add(jLabel);

            JLabel jLabel1 = new JLabel();
            jLabel1.setText("Enter your Name: ");
            panel1.add(jLabel1);

            JTextField text1 = new JTextField(10);
            panel1.add(text1);

            JLabel jLabel2 = new JLabel();
            jLabel2.setText("Enter your Pnone Number: ");
            panel1.add(jLabel2);

            JTextField text2 = new JTextField(10);
            panel1.add(text2);

            tPane.addTab("Customer", panel1);

            JPanel panel2 = new JPanel();
            panel2.setLayout(new FlowLayout());
            panel2.setPreferredSize(new Dimension(200, 200));

            jLabel = new JLabel();
            jLabel.setText("Floor Type? ");
            panel2.add(jLabel);

            ButtonGroup group = new ButtonGroup();
            JRadioButton RadioButton = new JRadioButton("Carpet - $10 per sq ft",               true);
            panel2.add(RadioButton);
            group.add(RadioButton);

            JRadioButton RadioButton1 = new JRadioButton("Hardwood - $20 per sq ft");
            panel2.add(RadioButton1);
            group.add(RadioButton1);

            tPane.addTab("Floor Type", panel2);

            JPanel panel3 = new JPanel();
            panel3.setLayout(new FlowLayout());
            panel3.setPreferredSize(new Dimension(200, 200));

            jLabel = new JLabel();
            jLabel.setText("Enter the length and width of your floor! ");
            panel3.add(jLabel);

            JLabel jLabel3 = new JLabel();
            jLabel3.setText("Enter the floor length: ");
            panel3.add(jLabel3);

            JTextField length = new JTextField(10);
            panel3.add(length);
            //length.setText(cost.toString());  //


            JLabel jLabel4 = new JLabel();
            jLabel4.setText("Enter the floor width: ");
            panel3.add(jLabel4);

            JTextField width = new JTextField(10);
            panel3.add(width);
            //width.setText(cost.toString()); //


            tPane.addTab("Floor Size", panel3);

            JPanel panel4 = new JPanel();
            panel4.setLayout(new FlowLayout());
            panel4.setPreferredSize(new Dimension(200, 200));

            jLabel = new JLabel();
            jLabel.setText("Total Cost: ");
            panel4.add(jLabel);

            JTextField text3 = new JTextField(10);
            panel4.add(text3);

           JButton button = new JButton("Order Summary");
           button.addActionListener(new ActionListener()
           {

               @Override
               public void actionPerformed(ActionEvent e)
               {

                   //////////////////////////////////////////////////////////////
                   //calculation moved to here
                   double floorLength = Double.parseDouble(length.getText());
                   double floorWidth = Double.parseDouble(width.getText());
                   floorSize = floorLength * floorWidth;
                   if(RadioButton.isSelected())
                   {
                       cost = 10; //per sq ft
                       floorType = RadioButton.getText();

                   }
                   else if(RadioButton1.isSelected())
                   {
                       cost = 20; //per sq ft
                       floorType = RadioButton1.getText();
                   }

                   text3.setText(cost.toString());
                   //////////////////////////////////////////////////////////////

                   JPanel panelNew = new JPanel();
                   panelNew.setLayout(new BoxLayout(panelNew, BoxLayout.Y_AXIS));

                   JLabel jLabeln = new JLabel("Order Summary");
                   JLabel jLabeln1 = new JLabel("Customer Name: " + text1.getText());
                   JLabel jLabeln2 = new JLabel("Phone Number: " + text2.getText());
                   JLabel jLabeln3 = new JLabel("Floor Type: " + floorType);
                   JLabel jLabeln4 = new JLabel("Floor Area: " + floorSize);
                   ///////////////////////////////////////////////////////////////////////////
                   // changed JLabel jLabeln5 = new JLabel("Total: $" + new Double(cost*floorSize) + "0");
                   //to:
                   JLabel jLabeln5 = new JLabel("Total: $" + new Double(cost*floorSize) + "0");
                   ///////////////////////////////////////////////////////////////////////////
                   JLabel jLabeln6 = new JLabel("Thank you for shopping at FloorMart!");

                    panelNew.add(jLabeln);
                    panelNew.add(jLabeln1);
                    panelNew.add(jLabeln2);
                    panelNew.add(jLabeln3);
                    panelNew.add(jLabeln4);
                    panelNew.add(jLabeln5);
                    panelNew.add(jLabeln6);

                    frame.invalidate();
                    frame.remove(panel1);frame.remove(panel2);frame.remove(panel3);
                    frame.remove(panel4);
                    frame.remove(tPane);

                    frame.add(panelNew);
                    frame.revalidate();
                    frame.repaint();

               }


           });

             panel4.add(button);
             tPane.addTab("Total", panel4);

             /////////////////////////////////
             //removed  ChangeListener
             ////////////////////////////////

             frame.add(tPane);
             frame.setVisible(true);

        }
    }