当前位置: 代码迷 >> J2SE >> 登录的确定和取消按钮为什么不出现。该如何处理
  详细解决方案

登录的确定和取消按钮为什么不出现。该如何处理

热度:16   发布时间:2016-04-24 02:05:34.0
登录的确定和取消按钮为什么不出现。
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 组件排版有什么好建议。


------解决方案--------------------
Java code
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);    }}
  相关解决方案