Java框架麻烦

时间:2014-02-11 08:41:39

标签: java swing jframe multiple-instances

我需要帮助我的程序,因为我想在我的程序中输入密码和用户名,所以如果用户名是test&&密码是12345我的程序将出现一个新的框架但不幸的是我的第二个不适用于我的标签,按钮等这里是我的代码到目前为止。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SwapFields extends JFrame{

    public static void main(String[] args) {
        SwapFields frameTabel = new SwapFields();
    }

    JButton blogin = new JButton("Login");
    JPanel panel = new JPanel();
    JTextField txuser = new JTextField(15);
    JPasswordField pass = new JPasswordField(15);
    JLabel lab = new JLabel("Username :");
    JLabel pas = new JLabel("Password :");
    JLabel cos;
    // JPanel panel = new JPanel();
    JButton y1;
    JButton y2;

    SwapFields() {
        super("Enter Your Account !");
        setSize(300, 200);
        setLocation(500, 280);
        panel.setLayout(null);

        txuser.setBounds(90, 30, 150, 20);
        pass.setBounds(90, 65, 150, 20);
        blogin.setBounds(110, 100, 80, 20);
        lab.setBounds(15, 28, 150, 20);
        pas.setBounds(15, 63, 150, 20);

        panel.add(lab);
        panel.add(pas);
        panel.add(blogin);
        panel.add(txuser);
        panel.add(pass);

        getContentPane().add(panel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        actionlogin();
    }

    public void actionlogin() {
        blogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                String puname = txuser.getText();
                String ppaswd = pass.getText();
                if (puname.equals("test") && ppaswd.equals("12345")) {

                    JFrame frame = new JFrame("Customer");
                    JPanel panel1 = new JPanel();

                    frame.setVisible(true);
                    frame.setSize(300, 200);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                    cos = new JLabel("Do you have a Customer ?");
                    y1 = new JButton("Yes");
                    y2 = new JButton("No");

                    panel1.setLayout(null);
                    cos.setBounds(70, 30, 150, 20);
                    y1.setBounds(80, 65, 150, 20);
                    y2.setBounds(140, 65, 150, 20);

                    y1.setSize(55, 30);
                    y2.setSize(55, 30);
                    panel1.add(y1);
                    panel1.add(y2);
                    panel1.add(cos);
                    frame.getContentPane().add(panel1);
                    // this is the code ill change this is the second frame .

                    dispose();
                } else {

                    JOptionPane.showMessageDialog(null, "Wrong Password / Username");
                    txuser.setText("");
                    pass.setText("");
                    txuser.requestFocus();
                }

            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

更改

    panel.setLayout(null);

    panel.setLayout(new Flowlayout());