摇摆设计师&布局& textField限制

时间:2015-07-21 09:42:57

标签: java swing

我试图实现我的programm的简单用户界面。这是我第一次使用swing designer插件。

第一个问题:用户输入开始和结束日期,程序会自动计算持续时间。但我不知道我怎么能写出yyyy / MM / dd的限制。我的意思是用户开始输入年份然后跳过另一个块而不做任何事情。

这是我的代码:

public class MyFrame extends JFrame {
    public MyFrame() {
    }

    private static JLabel labelBegin;
    private static JLabel labelEnd;
    private static JLabel labelDuration;
    private static JLabel labelA;
    private static JLabel labelB;
    private static JLabel labelC;
    private static JLabel labelD;
    private static JLabel labelTotal;
    private static JLabel labelSafety;
    private static JLabel labelSpeed;
    private static JLabel labelEfficiency;
    private static JLabel message;
    private static JTextField textFieldBegin;
    private static JTextField EndingField;
    private static JTextField DurationField;
    private static JTextField AField;
    private static JTextField BField;
    private static JTextField CField;
    private static JTextField DField;
    private static JTextField TotalField;
    private static JTextField SafetyField;
    private static JTextField SpeedField;
    private static JTextField EfficiencyField;
    private static JButton clear;
    private static JButton buttonNext;
    private static JButton buttonBack;
    private static JRadioButton PlacementResult;
    private static JRadioButton Collision;
    private static JPanel panelOptions;
    private static JPanel panelLogin;
    private static JFrame frame;
    private static JButton Mybutton;
    private static JPanel panelCons;

    private static void mydesign() {

        frame = new JFrame("Placement Tool V1.0");

        frame.getContentPane().setLayout(new GridLayout(1, 1));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panelLogin = new JPanel();
        panelLogin.setLayout(null);
        message = new JLabel ("In order to begin your testing, please enter the data");
        labelBegin = new JLabel("Enter Begin Date :");
        textFieldBegin = new JTextField(10);
        labelEnd = new JLabel("Test Duration :");
        EndingField = new JTextField(10);
        labelDuration = new JLabel ("Enter End Date :");
        DurationField = new JTextField(10);
        labelA = new JLabel ("Enter Group A :");
        AField = new JTextField (10);
        labelB = new JLabel ("Enter Group B:");
        BField = new JTextField (10);
        labelC = new JLabel ("Enter Group C:");
        CField = new JTextField (10);
        labelD = new JLabel ("Enter Group D:");
        DField = new JTextField (10);
        labelTotal = new JLabel ("Total Test Devices :");
        TotalField = new JTextField (10);
        labelSafety = new JLabel ("Enter Safety (%) : ");
        SafetyField = new JTextField (10);
        labelSpeed = new JLabel ("Enter Speed (kpbs)");
        SpeedField = new JTextField (10);
        labelEfficiency = new JLabel ("Enter Efficiency (%)");
        EfficiencyField = new JTextField (10);
        buttonNext = new JButton("Next");
        clear = new JButton("Cancel");

        panelLogin.add(buttonNext);
        panelLogin.add(clear);
        panelLogin.add(labelBegin);
        panelLogin.add(message);
        panelLogin.add(textFieldBegin);
        panelLogin.add(labelEnd);
        panelLogin.add(EndingField);
        panelLogin.add(labelDuration);
        panelLogin.add(DurationField);
        panelLogin.add (labelA);
        panelLogin.add(labelB);
        panelLogin.add(labelC);
        panelLogin.add(labelD);
        panelLogin.add(labelTotal);
        panelLogin.add(AField);
        panelLogin.add(BField);
        panelLogin.add(CField);
        panelLogin.add(DField);
        panelLogin.add(TotalField);
        panelLogin.add(labelSafety);
        panelLogin.add(labelSpeed);
        panelLogin.add(labelEfficiency);
        panelLogin.add(SpeedField);
        panelLogin.add(EfficiencyField);
        panelLogin.add(SafetyField);

        message.setBounds(50, 70, 500, 20);
        textFieldBegin.setBounds(200, 120, 100, 20);
        labelBegin.setBounds(50, 120, 120, 20);

        EndingField.setBounds(200, 140, 100, 20);
        labelEnd.setBounds(50, 160, 120, 20);

        DurationField.setBounds(200, 160, 100, 20);
        labelDuration.setBounds(50, 140, 120, 20);

        AField.setBounds(200, 180, 100, 20);
        labelA.setBounds(50, 180, 120, 20);

        BField.setBounds(200, 200, 100, 20);
        labelB.setBounds(50, 200, 120, 20);

        CField.setBounds(200, 220, 100, 20);
        labelC.setBounds(50, 220, 120, 20);

        DField.setBounds(200, 240, 100, 20);
        labelD.setBounds(50, 240, 120, 20);

        TotalField.setBounds(200, 260, 100, 20);
        labelTotal.setBounds(50, 260, 120, 20);

        SafetyField.setBounds(200, 280, 100, 20);
        labelSafety.setBounds(50, 280, 120, 20);

        SpeedField.setBounds(200, 300, 100, 20);
        labelSpeed.setBounds(50, 300, 120, 20);

        EfficiencyField.setBounds(200, 320, 100, 20);
        labelEfficiency.setBounds(50, 320, 120, 20);


       buttonNext.setBounds(70, 350, 100, 20);
       clear.setBounds(200, 350, 100, 20);

        buttonNext.addActionListener(actionEvent);
        clear.addActionListener(actionEvent);
        frame.getContentPane().add(panelLogin);
        //frame ends.

        //Options frame starts.

        panelOptions = new JPanel();
        panelOptions.setLayout(null);
        PlacementResult = new JRadioButton ("Show the placement result.");
        Collision = new JRadioButton ("Show the collision (%)");
        buttonBack = new JButton("Back");
        buttonNext = new JButton("Next");
        panelOptions.add(buttonBack);
        panelOptions.add(buttonNext);
        panelOptions.add(Collision);
        panelOptions.add(PlacementResult);
        Collision.setBounds(150, 25, 200, 25);
        PlacementResult.setBounds(150, 50, 200, 25);
        buttonBack.setBounds(150, 100, 200, 25);
        buttonNext.setBounds(150, 150, 200, 25);
        buttonBack.addActionListener(actionEvent);
        buttonNext.addActionListener(actionEvent);
        //Options Frame ends.




        panelCons = new JPanel();
        panelCons.setLayout(null);
        Mybutton = new JButton("Button");
        panelCons.add(Mybutton);
        Mybutton.setBounds(180, 180, 150, 25);
        Mybutton.addActionListener(actionEvent);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }


    public static void main(String args[])  {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                mydesign();
            }
        });
    }
    private static ActionListener actionEvent = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
             if(frame == null)
                 frame = new JFrame();
             else {
                 //remove the previous JFrame
                 frame.setVisible(false);
                 frame.dispose();
                 //create a new one
                 frame = new JFrame();
             }
            String action = e.getActionCommand();
            if ("Login".equals(action)) {
                System.out.println("Login action" + e.getActionCommand());
                if ("".equals(textFieldBegin.getText()) || "".equals(EndingField.getText()) ||"".equals(DurationField.getText()) ) {
                    JOptionPane.showMessageDialog(null,
                            "Enter Both begin test date and end test date");

                } else if ("admin".equals(textFieldBegin.getText())&&"admin".equals(AField.getText())&&
                "admin".equals(BField.getText())&&
                "admin".equals(CField.getText())&&"admin".equals(DField.getText())&& "admin".equals(TotalField.getText())&&
                "admin".equals(EfficiencyField.getText()) && "admin".equals(SafetyField.getText()) &&
                "admin".equals(SpeedField.getText()) && "admin".equals(EndingField.getText())) 
                {
                    System.out.println("Test begin date" + textFieldBegin.getText() + " Test end date" + EndingField.getText()
                            //+" Test duration" + (EndingField.getText() - textFieldBegin.getText())
                            +" Group A devices" + AField.getText() +" Group B devices"+ BField.getText()
                            + " Group C devices "+ CField.getText()
                            + "Group D devices"+ DField.getText()
                            + "Total devices" + (AField.getText() + BField.getText() + CField.getText() + DField.getText())
                            +"Max speed (kbps)" + SpeedField.getText()
                            + "Efficiency (%)" + EfficiencyField.getText() + "Safety (%)" + SafetyField.getText()
                            );
                    frame.remove(panelLogin);
                    frame.getContentPane().add(panelOptions);
                    frame.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null,
                            "Date format that you entered is not appropriate! Hint: YYYY/MM/DD");

                }
            } else if ("Cancel".equals(action)) {
                System.out.println(e.getActionCommand());
                System.exit(0);

            } 

              {
                frame.remove(panelOptions);
               frame.getContentPane().add(panelCons);

                frame.setVisible(true);
            }

        }
        ;

    };
}

第二个问题是我想让我的程序用户友好,所以我想让每个组件彼此相似的组成。这是输出:

感谢所有帮助

1 个答案:

答案 0 :(得分:1)

解决方案:

  1. 使用TF(For 4 digits) + LB(For Slash) + TF(For 2 digits) + LB(For Slash) + TF(For 2 digits)在日期[TF:TextField和LB:Label]中获取该格式或者您可以在Next按钮上单击验证日期格式是否与特定格式匹配,甚至是日期是否有效。
  2. 使用JPanel并设置其边框进行分组。 (提示:使用BorderFactory
  3. 我没有让你知道这个具体的问题.SO不能说什么。