GUI构建和使用不同的Java类

时间:2014-02-04 20:09:47

标签: java user-interface jframe jpanel

所以我开始了一个新项目,我想知道如何为不同的JPanel设置多个类。一堂课看起来很乱。让我告诉你这个例子。我希望JPanel在它自己的类中有一个菜单屏幕。

import java.awt.EventQueue;


public class TakeAwayLogin {

    private JFrame frmTakeAwaySystem;
    private JTextField textFieldId;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     * @return 
     */
    public void login() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TakeAwayLogin window = new TakeAwayLogin();
                    window.frmTakeAwaySystem.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TakeAwayLogin() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmTakeAwaySystem = new JFrame();
        frmTakeAwaySystem.setTitle("Take Away System Alpha");
        frmTakeAwaySystem.setBounds(100, 100, 450, 300);
        frmTakeAwaySystem.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmTakeAwaySystem.getContentPane().setLayout(new CardLayout(0, 0));

        final JPanel panelLogin = new JPanel();
        frmTakeAwaySystem.getContentPane().add(panelLogin, "name_254735117500687");
        panelLogin.setLayout(null);
        panelLogin.setVisible(true);

        final JLabel lblIncorrect = new JLabel("Incorrect login, try again");
        lblIncorrect.setHorizontalAlignment(SwingConstants.CENTER);
        lblIncorrect.setFont(new Font("Tahoma", Font.PLAIN, 9));
        lblIncorrect.setForeground(Color.RED);
        lblIncorrect.setBounds(148, 74, 139, 14);
        panelLogin.add(lblIncorrect);
        lblIncorrect.setVisible(false);

        final JPanel panelPassword = new JPanel();
        frmTakeAwaySystem.getContentPane().add(panelPassword, "name_254738265432897");
        panelPassword.setLayout(null);

        passwordField = new JPasswordField();
        passwordField.setBounds(112, 157, 205, 41);
        panelLogin.add(passwordField);
        passwordField.setHorizontalAlignment(JPasswordField.CENTER);

        JButton btnNewButton = new JButton("Lock Application");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panelPassword.setVisible(false);
                panelLogin.setVisible(true);
                passwordField.setText("");
                textFieldId.disable();
            }
        });
        btnNewButton.setBounds(135, 155, 172, 50);
        panelPassword.add(btnNewButton);
        panelPassword.setVisible(false);

        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String login = textFieldId.getText();
                char[] pass = passwordField.getPassword();
                String p = new String(pass);
                String password = "pass";
                if (login.equalsIgnoreCase("milan") &&  p.equals(password)) {
                    panelPassword.setVisible(true);
                    panelLogin.setVisible(false);
                }else {
                    lblIncorrect.setVisible(true);
                }
            }
        });
        btnLogin.setBounds(160, 209, 97, 41);
        panelLogin.add(btnLogin);

        textFieldId = new JTextField();
        textFieldId.setBounds(112, 88, 205, 43);
        panelLogin.add(textFieldId);
        textFieldId.setColumns(10);
        textFieldId.setHorizontalAlignment(JTextField.CENTER);

        JLabel lblId = new JLabel("Enter the business login:");
        lblId.setHorizontalAlignment(SwingConstants.CENTER);
        lblId.setBounds(112, 63, 205, 14);
        panelLogin.add(lblId);


        JLabel lblEnterYourPassword = new JLabel("Enter your password");
        lblEnterYourPassword.setHorizontalAlignment(SwingConstants.CENTER);
        lblEnterYourPassword.setBounds(148, 142, 139, 14);
        panelLogin.add(lblEnterYourPassword);

        JPanel panelPizza = new JPanel();
        frmTakeAwaySystem.getContentPane().add(panelPizza, "name_254741096954780");

    }
}

所以帮助会很棒。

非常感谢。

2 个答案:

答案 0 :(得分:0)

我不知道为什么要创建第二堂课。如果您只是希望代码看起来不那么混乱,那么在Panels之间添加注释分隔符或其他东西。我认为将Panel分成他们自己的类只是因为你必须编写更多的调用才能创建相同的功能。但如果你必须,那么在你的初级课堂上上课并在那里工作。

答案 1 :(得分:0)

简化,你需要类似的东西:

public class MyMainJFrame extends JFrame{
     private JPanel panel1 = new MyFooPanel();
     private JPanel panel2 = new MyBarPanel();

     public MyMainPanel(){
         initialize();
     }

     void initialize(){
         //init the JFrame
         this.setTitle("Take Away System Alpha");
         this.setBounds(100, 100, 450, 300);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.setLayout(new CardLayout(0, 0));

         //and add the panels
         this.add(panel1);
         this.add(panel2);
     }
}

// a file for each one

public class MyFooPanel extends JPanel{
     private JButton button;
     //... add components and logic
}

public class MyBarPanel extends JPanel{
     private JTextField field;
     //... add components and logic
}

如果要为每个自定义面板使用特定方法,请更改自定义类的相同名称的变量类型。

相关问题