java一个简单的登录界面制作
在做登陆界面的时候怎么把各个组件放的整齐一点,希望大神可以写一个例子给我,就用户名+文本框和密码+文本框再有两个确定取消的按钮就可以了希望可以给我一个简单例子,别太复杂,...
在做登陆界面的时候怎么把各个组件放的整齐一点,希望大神可以写一个例子给我,就用户名+文本框和密码+文本框再有两个确定取消的按钮就可以了 希望可以给我一个简单例子,别太复杂,还是个新手,而且我还要在确定中加入监视器,太复杂的话,我就不知道该在什么地方改了
展开
展开全部
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* 2014年12月28日下午7:18:41
* @author season
*
*/
public class LoginDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginDemo frame = new LoginDemo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LoginDemo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 370, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblAccount = new JLabel("Account:");
lblAccount.setFont(new Font("Consolas", Font.BOLD, 14));
lblAccount.setBounds(25, 90, 104, 28);
contentPane.add(lblAccount);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setFont(new Font("Consolas", Font.BOLD, 14));
lblPassword.setBounds(25, 128, 104, 28);
contentPane.add(lblPassword);
textField = new JTextField();
textField.setBounds(113, 93, 154, 21);
contentPane.add(textField);
textField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(113, 131, 154, 21);
contentPane.add(passwordField);
final JLabel lblNewLabel = new JLabel("");
lblNewLabel.setFont(new Font("Consolas", Font.BOLD, 12));
lblNewLabel.setBounds(0, 222, 344, 30);
contentPane.add(lblNewLabel);
JButton btnSignIn = new JButton("Sign in");//登录按钮和监听
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String userName =textField.getText();
String userPwd = new String(passwordField.getPassword());
if(userName.equals("haiyan")&&userPwd.equals("haiyan")){
lblNewLabel.setText("You success login ");
}else{
lblNewLabel.setText("Fail to login ,check please ");
}
}
});
btnSignIn.setFont(new Font("Consolas", Font.BOLD, 14));
btnSignIn.setBounds(60, 189, 93, 23);
contentPane.add(btnSignIn);
JButton btnReset = new JButton("Reset");//重置按钮和监听
btnReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
passwordField.setText("");
}
});
btnReset.setFont(new Font("Consolas", Font.BOLD, 14));
btnReset.setBounds(174, 189, 93, 23);
contentPane.add(btnReset);
JLabel lblHaiyan = new JLabel("Haiyan");
lblHaiyan.setFont(new Font("Consolas", Font.BOLD, 14));
lblHaiyan.setBounds(123, 10, 104, 28);
contentPane.add(lblHaiyan);
}
}
更多追问追答
追问
希望,用另一个主函数生成,这个里面就不用main方法,但是生成的时候没有反应
追答
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* 2014年12月28日下午7:18:41
* @author season
*
*/
public class LoginDemo extends JFrame {
.........
/* public static void main(String[] args) {//只注释主函数,其他全部不变
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginDemo frame = new LoginDemo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
*/
}
public class Main {
public static void main(String[] args){
LoginDemo frame = new LoginDemo();
frame.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询