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 组件排版有什么好建议。
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 组件排版有什么好建议。
解决方案 »
- 请教一个有关JInternalFrame的问题
- 多线程的流程控制问题
- 如何还原double科学计数法后的值
- 模的幂计算问题,为什么同样的算法用JAVA做就会越界?请教数论和JAVA高手
- 熟悉使用Eclipse编程环境的帮小弟回答下一个问题...!
- JDBC连接SQLServer2005报No suitable driver错误
- {高手进}这个方法为什么会打印出(线程名+sell tickets:0)来
- 多行文字TextArea的问题
- 急问!
- J builder5.0中出现neither class nor source found for java.lang.Object
- java打印机获得当前打印机
- Person对象p1,p2,将p2中的非空属性赋给p1,如何做?
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); }
}
我终于找出原因了我那个按钮的Y方向的坐标错误了,比其面板都还要长了。