大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
CS结构系统的退出如下:public void init() {\x0d\x0a this.setTitle("用户登录界面");\x0d\x0a this.add(createCenterPane());\x0d\x0a this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);\x0d\x0a this.setSize(new Dimension(450, 335));\x0d\x0a this.setLocationRelativeTo(null);\x0d\x0a // this.setVisible(true);\x0d\x0a this.addWindowListener(new WindowAdapter() {\x0d\x0a public void windowClosing(WindowEvent e) {\x0d\x0a int choose = JOptionPane.showConfirmDialog(null, "是否要退出登录界面?",\x0d\x0a "系统提示:", JOptionPane.YES_NO_OPTION);\x0d\x0a if (choose == JOptionPane.YES_OPTION) {\x0d\x0a System.exit(1);\x0d\x0a }\x0d\x0a }\x0d\x0a });\x0d\x0a }其中this为JFrame对象。BS结构的退出直接用windows.close()方法就行了!
大安网站建设公司创新互联建站,大安网站设计制作,有大型网站制作公司丰富经验。已为大安上千提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的大安做网站的公司定做!
方法一:
采用JOptionPane中的一个非常有用的静态方法 showOptionPane();
源码如下:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.BorderFactory;
public class Login1 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static void createAndShowGUI() {
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(250,250,400,300);
mainFrame.setVisible(false);
usernameField = new JTextField(10);
passwordField = new JPasswordField(10);
Object[] options = {"登录","取消"};
int i = JOptionPane.showOptionDialog(null,createLoginPanel(),"登录信息",JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE,null,options,options[0]);
if(i==0) {
String username = usernameField.getText();
String password = passwordField.getText();
if(!username.equals("") !password.equals("")) {
mainFrame.getContentPane().add(new JLabel("用户名:"+username+" 密码是:"+password,JLabel.CENTER));
mainFrame.show();
}
else {
JOptionPane.showMessageDialog(null,"用户名和密码不能为空","提示",JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
}
else System.exit(0);
}
static JPanel createLoginPanel() {
JPanel ret = new JPanel();
JPanel usernamePanel = new JPanel();
usernamePanel.add(new JLabel("用户名:",JLabel.RIGHT));
usernamePanel.add(usernameField);
JPanel passwordPanel = new JPanel();
passwordPanel.add(new JLabel("密 码:",JLabel.RIGHT));
passwordPanel.add(passwordField);
Box box = new Box(BoxLayout.Y_AXIS);
box.add(usernamePanel); box.add(passwordPanel);
ret.add(box);
ret.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(244,144,44)),"填写登录信息"));
return ret;
}
static JFrame mainFrame = null;
static JTextField usernameField = null;
static JPasswordField passwordField = null;
}
运行:
javac -deprecation Login1.java
java Login
(因为有一个过期的API,所以用了 -deprecation 命令)
方法二,使用了两个JFrame类共同实现,第一次显示第一个frame,当点了登录后且操作合法时,第一个窗口就被释放了 dispose();再显示第二个窗口:
源码如下:
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class Login2 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static void createAndShowGUI() {
//////////////////////////////////////////////////////////////
loginWindow = new JFrame("登录信息");
loginWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginWindow.setBounds(350,350,250,200);
loginWindow.setResizable(false);
JPanel usernamePanel = new JPanel();
usernamePanel.add(new JLabel("用户名:",JLabel.CENTER));
usernamePanel.add(usernameField);
JPanel passwordPanel = new JPanel();
passwordPanel.add(new JLabel("密 码:",JLabel.CENTER));
passwordPanel.add(passwordField);
Box box = new Box(BoxLayout.Y_AXIS);
box.add(usernamePanel); box.add(passwordPanel);
JPanel infoPanel = new JPanel();
infoPanel.add(box);
infoPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(244,144,44)),"填写登录信息"));
JButton submitButton = new JButton("登录");
JButton cancelButton = new JButton("取消");
submitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = passwordField.getText();
if(!username.equals("") !password.equals("")) {
loginWindow.dispose();
mainFrame.getContentPane().add(new JLabel("用户名:"+username+" 密码是:"+password,JLabel.CENTER));
mainFrame.setVisible(true);
}
else {
JOptionPane.showMessageDialog(null,"用户名和密码不能为空","提示",JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
}
});
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(submitButton); buttonPanel.add(cancelButton);
loginWindow.getContentPane().add(infoPanel,BorderLayout.CENTER);
loginWindow.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
loginWindow.getContentPane().add(new JPanel(),BorderLayout.EAST);
loginWindow.getContentPane().add(new JPanel(),BorderLayout.WEST);
loginWindow.setVisible(true);
/////////////////////////////////////////////////////////////////
mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(250,250,400,300);
mainFrame.setVisible(false);
}
static JFrame loginWindow,mainFrame;
static final JTextField usernameField = new JTextField(10);
static final JPasswordField passwordField = new JPasswordField(10);
}
运行:
javac -deprecation Login2.java
java Login2
import java.awt.*; \x0d\x0aimport javax.swing.*; \x0d\x0aimport java.awt.event.*; \x0d\x0aimport java.sql.*; \x0d\x0a\x0d\x0aclass LoginFrm extends JFrame implements ActionListener \x0d\x0a{ \x0d\x0aJLabel lbl1=new JLabel("用户名"); \x0d\x0aJLabel lbl2=new JLabel("密码"); \x0d\x0aJTextField txt=new JTextField(15); \x0d\x0aJPasswordField pf=new JPasswordField(); \x0d\x0aJButton btn1=new JButton("确定"); \x0d\x0aJButton btn2=new JButton("取消"); \x0d\x0a\x0d\x0apublic LoginFrm() \x0d\x0a{ \x0d\x0athis.setTitle("登陆"); \x0d\x0aJPanel jp=(JPanel)this.getContentPane(); \x0d\x0ajp.setLayout(new GridLayout(3,2,10,10)); \x0d\x0ajp.add(lbl1);jp.add(txt); \x0d\x0ajp.add(lbl2);jp.add(pf); \x0d\x0ajp.add(btn1);jp.add(btn2); \x0d\x0abtn1.addActionListener(this); \x0d\x0abtn2.addActionListener(this); \x0d\x0a} \x0d\x0a\x0d\x0apublic void actionPerformed(ActionEvent ae) \x0d\x0a{ \x0d\x0aif(ae.getSource()==btn1) \x0d\x0a{ \x0d\x0atry \x0d\x0a{ \x0d\x0aClass.forName("sun.jdbc.odbc.JdbcOdbcDriver"); \x0d\x0aConnection con=DriverManager.getConnection("jdbc:odbc:MyDB","",""); \x0d\x0aStatement cmd=con.createStatement(); \x0d\x0aResultSet rs=cmd.executeQuery("select * from loginAndpassword where login='"+txt.getText()+"' and password='"+pf.getText()+"'"); \x0d\x0aif(rs.next()) \x0d\x0a{ \x0d\x0aJOptionPane.showMessageDialog(null,"登陆成功!"); \x0d\x0a} \x0d\x0aelse \x0d\x0aJOptionPane.showMessageDialog(null,"用户名或密码错误!"); \x0d\x0a} catch(Exception ex){} \x0d\x0a\x0d\x0aif(ae.getSource()==btn2) \x0d\x0a{ \x0d\x0atxt.setText(""); \x0d\x0apf.setText(""); \x0d\x0a} \x0d\x0a} \x0d\x0a} \x0d\x0a\x0d\x0apublic static void main(String arg[]) \x0d\x0a{ \x0d\x0aJFrame.setDefaultLookAndFeelDecorated(true); \x0d\x0aLoginFrm frm=new LoginFrm(); \x0d\x0afrm.setSize(400,200); \x0d\x0afrm.setVisible(true); \x0d\x0a} \x0d\x0a}
用java做QQ登录界面的写法如下:
package ch10;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
1、//定义该类继承自JFrame,实现ActionListener接口
public class LoginTest extends JFrame implements ActionListener
{
2、//创建JPanel对象
private JPanel jp=new JPanel();
3、//创建3个标并加入数组
JLabel name = new JLabel("请输入用户名");
JLabel password = new JLabel("请输入密码");
JLabel show = new JLabel("");
private JLabel[] jl={name,password,show};
4、//创建登陆和重置按扭并加入数组
JButton login = new JButton("登陆");
JButton reset = new JButton("重置");
private JButton[] jb={login,reset};
5、//创建文本框以及密码框
private JTextField jName=new JTextField();
private JPasswordField jPassword =new JPasswordField();
public LoginTest()
{
6、//设置布局管理器为空布局,这里自己摆放按钮、标签和文本框
jp.setLayout(null);
for(int i=0;i2;i++)
{
7、//设置标签和按扭的位置与大小
jl[i].setBounds(30,20+40*i,180,20);
jb[i].setBounds(30+110*i,100,80,20);
8、//添加标签和按扭到JPanel容器中
jp.add(jl[i]);
jp.add(jb[i]);
//为2个按钮注册动作事件监听器
jb[i].addActionListener(this);
}
9、//设置文本框的位置和大小,注意满足美观并足够用户名的长度
jName.setBounds(130,15,100,20);
10、//添加文本框到JPanel容器中
jp.add(jName);
11、//为文本框注册动作事件监听器
jName.addActionListener(this);
12、//设置密码框的位置和大小,注意满足美观和足够密码的长度
jPassword.setBounds(130,60,100,20);
13、//添加密码框到JPanel容器中
jp.add(jPassword);
14、//设置密码框中的回显字符,这里设置美元符号
jPassword.setEchoChar('$');
15、//为密码框注册动作事件监听器
jPassword.addActionListener(this);
16、//设置用于显示登陆状态的标签大小位置,并将其添加进JPanel容器
jl[2].setBounds(10,180,270,20);
jp.add(jl[2]);
17、//添加JPanel容器到窗体中
this.add(jp);
18、//设置窗体的标题、位置、大小、可见性及关闭动作
this.setTitle("登陆窗口");
this.setBounds(200,200,270,250);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
19、//实现动作监听器接口中的方法actionPerformed
public void actionPerformed(ActionEvent e)
{
20、//如果事件源为文本框
if(e.getSource()==jName)
{
21、//切换输入焦点到密码框
jPassword.requestFocus();
}
22、//如果事件源为重置按扭
else if(e.getSource()==jb[1])
{
23、//清空姓名文本框、密码框和show标签中的所有信息
jl[2].setText("");
jName.setText("");
jPassword.setText("");
24、//让输入焦点回到文本框
jName.requestFocus();
}
25、//如果事件源为登陆按钮,则判断登录名和密码是否正确
else
{
26、//判断用户名和密码是否匹配
if(jName.getText().equals("lixiangguo")
String.valueOf(jPassword.getPassword()).equals("19801001"))
{
27、jl[2].setText("登陆成功,欢迎您的到来!");
}
else
{
28、jl[2].setText("对不起,您的用户名或密码错误!");
}
}
}
public static void main(String[] args)
{
29、//创建LoginTest窗体对象
new LoginTest();
}
}