2014-02-11 80 views
0

我需要我的程序幫助,因爲我想將密碼和用戶名添加到我的程序中,所以如果用戶名是test & &密碼是12345我的程序會出現一個新框架,但不幸的是我的第二個框架不適用於我的標籤,按鈕等,這裏是我的代碼到目前爲止。Java框架問題

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(); 
       } 

      } 
     }); 
    } 
} 
+0

我使用JCreator的及其版本的JDK 1.7.0_10 – Batusai

+1

你能澄清你的意思與_「但不幸的是我的第二個沒爲我的標籤,按鈕等工作「_? – Bnrdo

回答

0

變化

panel.setLayout(null); 

panel.setLayout(new Flowlayout());