import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Test_Login extends javax.swing.JFrame {
private JPanel jPanel1,jPanel2;
private JButton bntLogin;
private JButton bntCannel;
private JPasswordField pwd;
private JTextField username;
private JTextField computerid;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel1;public Test_Login() {
   super();
   initGUI();
}private void initGUI() {
   try {
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    {     getContentPane().setLayout(null);
     jPanel1 = new JPanel();
     jPanel2 = new JPanel();
    getContentPane().add(jPanel1, BorderLayout.NORTH);
     getContentPane().add(jPanel2, BorderLayout.SOUTH);
     jPanel1.setBounds(0,0,300,150);
     jPanel2.setBounds(0,150,300,80);
     jPanel2.setBackground(new java.awt.Color(102, 255, 204));//     jPanel1.setLayout(null);
      jLabel1 = new JLabel();
     jLabel1.setText("用户名");
      jLabel1.setBounds(45, 30, 75, 25);
      jLabel2 = new JLabel();
      jLabel2.setText("密码");
      jLabel2.setBounds(45, 75, 55, 15);
      username = new JTextField();
      username.setBounds(100, 30, 140, 25);
      pwd = new JPasswordField();
      pwd.setBounds(100, 70, 140, 25);      
      jPanel1.add(jLabel1);
      jPanel1.add(jLabel2);
      jPanel1.add(username);
      jPanel1.add(pwd);
      
 //     jPanel2.setLayout(new FlowLayout());//new FlowLayout
     jPanel2.setLayout(null);//new FlowLayout
      bntLogin = new JButton();     
      bntLogin.setText("登陆");
      bntLogin.setBounds(0,220, 60, 30);
      bntCannel = new JButton();    
      bntCannel.setText("取消");
      bntCannel.setBounds(250,220, 60, 30);
      jPanel2.add(bntLogin);
      jPanel2.add(bntCannel);
      //jPanel2.add(bntLogin);
      //jPanel2.add(bntCannel);
      pack();
      setSize(300, 230);
      
      
      bntLogin.addMouseListener(new MouseAdapter() {     public void mouseClicked(MouseEvent e) {
        if (username.getText().equals("lisong")
          && pwd.getText().equals("lisong")) {
         JOptionPane.showMessageDialog(Test_Login.this,
           "登录成功");
        } else {
         JOptionPane.showMessageDialog(Test_Login.this,
           "登录失败");
        }       }
      });      bntCannel.addMouseListener(new MouseAdapter() {
       public void mouseClicked(MouseEvent e) {        System.exit(0);
       }
      });
     }
   }
   catch (Exception e) {
    e.printStackTrace();
   }
}public static void main(String[] args) {
Test_Login inst = new Test_Login();
   inst.setLocationRelativeTo(null);
//    inst.setResizable (false); //设置窗口不可放大缩小
   inst.setVisible(true); }
}下面那个面板我设置null ,然后绝对定位,为什么确定按钮和取消按钮没有出现?
关于swing 组件排版有什么好建议。

解决方案 »

  1.   


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Test_Login extends javax.swing.JFrame
    {
    private JPanel jPanel1, jPanel2;
    private JButton bntLogin;
    private JButton bntCannel;
    private JPasswordField pwd;
    private JTextField username;
    private JTextField computerid;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel1; public Test_Login()
    {
    super();
    initGUI();
    } private void initGUI()
    {
    try
    {
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    { getContentPane().setLayout(null);
    jPanel1 = new JPanel();
    jPanel2 = new JPanel();
    getContentPane().add(jPanel1, BorderLayout.CENTER);

    jPanel1.setBounds(0, 0, 300, 150);
    jPanel2.setBounds(0, 150, 300, 80);
    jPanel2.setBackground(new java.awt.Color(102, 255, 204)); jPanel1.setLayout(null);
    jLabel1 = new JLabel();
    jLabel1.setText("用户名");
    jLabel1.setBounds(45, 30, 75, 25);
    jLabel2 = new JLabel();
    jLabel2.setText("密码");
    jLabel2.setBounds(45, 75, 55, 15);
    username = new JTextField();
    username.setBounds(100, 30, 140, 25);
    pwd = new JPasswordField();
    pwd.setBounds(100, 70, 140, 25); jPanel1.add(jLabel1);
    jPanel1.add(jLabel2);
    jPanel1.add(username);
    jPanel1.add(pwd); // jPanel2.setLayout(new FlowLayout());//new FlowLayout
    //jPanel2.setLayout(null);// new FlowLayout
    bntLogin = new JButton();
    bntLogin.setText("登陆");
    //bntLogin.setBounds(0, 220, 60, 30);
    bntCannel = new JButton();
    bntCannel.setText("取消");
    //bntCannel.setBounds(250, 220, 60, 30);
    jPanel2.add(bntLogin);
    jPanel2.add(bntCannel);
    getContentPane().add(jPanel2, BorderLayout.SOUTH);
    // jPanel2.add(bntLogin);
    // jPanel2.add(bntCannel);
    pack();
    setSize(300, 230); bntLogin.addMouseListener(new MouseAdapter()
    { public void mouseClicked(MouseEvent e)
    {
    if (username.getText().equals("lisong")
    && pwd.getText().equals("lisong"))
    {
    JOptionPane.showMessageDialog(Test_Login.this,
    "登录成功");
    } else
    {
    JOptionPane.showMessageDialog(Test_Login.this,
    "登录失败");
    } }
    }); bntCannel.addMouseListener(new MouseAdapter()
    {
    public void mouseClicked(MouseEvent e)
    { System.exit(0);
    }
    });
    }
    } catch (Exception e)
    {
    e.printStackTrace();
    }
    } public static void main(String[] args)
    {
    Test_Login inst = new Test_Login();
    inst.setLocationRelativeTo(null);
    // inst.setResizable (false); //设置窗口不可放大缩小
    inst.setVisible(true); }
    }
      

  2.   

    上楼,你那个出现,是因为jPanel2.setLayout(new FlowLayout());这样才出现。我知道。但是我想知道为什么我下面面板是设置null的,然后定位这样为什么不行吗?为什么不出现
      

  3.   

    你的jPanel2是放在整个大的容器中的,那个容器的布局不是null吧  所以你设置的jPanel2的大小没用的,需要的话个它上个jPanel2.setPreraredSize();//具体是不是这个忘了,就是设置在布局下的大小
      

  4.   

    是NULL的。
    我终于找出原因了我那个按钮的Y方向的坐标错误了,比其面板都还要长了。