基于JavaSwing的例子-非连接数据库
项目结构:
Constant.java
package com.mstf.test; import java.io.Serializable; public class Constant implements Serializable { public static final long serialVersionUID = 1L; // 超链接 public static final String URI1 = "第一个需要打开的网址"; public static final String URI2 = "第二个需要打开的网址"; // 帐号和密码 public static final String userName = "admin"; public static final String passWord = "123456"; }
Test.java
package com.mstf.test; import java.awt.Desktop; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.awt.event.ActionEvent; import java.awt.Color; import java.awt.Font; import java.awt.Toolkit; public class Test extends JFrame { private static final long serialVersionUID = 1L; private JPanel contentPane; private JTextField text_userName; private JTextField text_passWord; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Test frame = new Test(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Test() { setIconImage(Toolkit.getDefaultToolkit().getImage(Test.class.getResource("/images/favicon.png"))); setTitle("雨落秋垣-后台登录"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel labe_l1 = new JLabel("用户名:"); labe_l1.setForeground(Color.RED); labe_l1.setBounds(86, 86, 54, 15); contentPane.add(labe_l1); JLabel label_2 = new JLabel("密 码:"); label_2.setForeground(Color.RED); label_2.setBounds(86, 130, 54, 15); contentPane.add(label_2); text_userName = new JTextField(); text_userName.setForeground(Color.DARK_GRAY); text_userName.setBounds(144, 83, 166, 21); contentPane.add(text_userName); text_userName.setColumns(10); text_passWord = new JPasswordField(); text_passWord.setForeground(Color.DARK_GRAY); text_passWord.setBounds(145, 127, 165, 21); contentPane.add(text_passWord); text_passWord.setColumns(10); JButton login = new JButton("登录后台"); login.setForeground(Color.RED); login.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // 登录按钮的方法 if (text_userName.getText().trim().equals(Constant.userName) && text_passWord.getText().trim().equals(Constant.passWord)) { JOptionPane.showMessageDialog(contentPane, "登陆成功!", "标题", JOptionPane.WARNING_MESSAGE); StartBrowse(Constant.URI1); System.exit(0); } else { JOptionPane.showMessageDialog(contentPane, "用户名或者密码错误!!", "标题", JOptionPane.WARNING_MESSAGE); // 帐号密码错误,自动清空帐号和密码 text_userName.setText(""); text_passWord.setText(""); } } }); login.setBounds(70, 194, 93, 23); contentPane.add(login); JButton regeist = new JButton("查看权限"); regeist.setForeground(Color.BLUE); regeist.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 注册按钮的方法 if (text_userName.getText().trim().equals(Constant.userName) && text_passWord.getText().trim().equals(Constant.passWord)) { JOptionPane.showMessageDialog(contentPane, "登陆成功!", "标题", JOptionPane.WARNING_MESSAGE); StartBrowse(Constant.URI2); System.exit(0); } else { JOptionPane.showMessageDialog(contentPane, "您未登录!!", "标题", JOptionPane.WARNING_MESSAGE); // 帐号密码错误,自动清空帐号和密码 text_userName.setText(""); text_passWord.setText(""); } } }); regeist.setBounds(255, 194, 93, 23); contentPane.add(regeist); JLabel lblHttpsceetgovtop = new JLabel("官方网站: https://ceet-gov.top"); lblHttpsceetgovtop.setForeground(Color.RED); lblHttpsceetgovtop.setBounds(116, 236, 222, 15); contentPane.add(lblHttpsceetgovtop); JLabel label = new JLabel("雨落秋垣-后台管理系统"); label.setForeground(Color.MAGENTA); label.setFont(new Font("宋体", Font.PLAIN, 36)); label.setBounds(23, 23, 378, 39); contentPane.add(label); } /** * 用默认的浏览器,打开指定超链接 * * @param uri */ public void StartBrowse(String uri) { Desktop desktop = Desktop.getDesktop(); try { desktop.browse(new URI(uri)); } catch (IOException e1) { e1.printStackTrace(); } catch (URISyntaxException e1) { e1.printStackTrace(); } } }
我们有两个方法来进行软件设计:一个是让其足够的简单以至于让BUG无法藏身;另一个就是让其足够的复杂,让人找不到BUG。前者更难一些。