一、介绍

Swing 是一个为Java设计的GUI工具包。

Swing是JAVA基础类的一部分。

Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。

Swing提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java本身一样可以跨平台运行,这一点不像AWT。它们是JFC的一部分。它们支持可更换的面板和主题(各种操作系统默认的特有主题),然而不是真的使用原生平台提供的设备,而是仅仅在表面上模仿它们。这意味着你可以在任意平台上使用JAVA支持的任意面板。轻量级组件的缺点则是执行速度较慢,优点就是可以在所有平台上采用统一的行为。

二、效果

三、代码

  1. package com.test.jframe;
  2. import java.awt.Color;
  3. import java.awt.EventQueue;
  4. import java.awt.Label;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.JButton;
  8. import javax.swing.JFormattedTextField;
  9. import javax.swing.JFrame;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JPasswordField;
  12. public class JFrameTest {
  13. private JFrame frame;
  14. private JPasswordField passwordField;
  15. private boolean isLogin = false;
  16. /**
  17. * Launch the application.
  18. */
  19. public static void main(String[] args) {
  20. EventQueue.invokeLater(new Runnable() {
  21. public void run() {
  22. try {
  23. JFrameTest window = new JFrameTest();
  24. window.frame.setVisible(true);
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. });
  30. }
  31. /**
  32. * Create the application.
  33. */
  34. public JFrameTest() {
  35. initialize();
  36. }
  37. /**
  38. * Initialize the contents of the frame.
  39. */
  40. private void initialize() {
  41. String userName = "111";
  42. String userPwd = "111";
  43. frame = new JFrame();
  44. frame.setBounds(100, 100, 667, 453);
  45. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46. frame.getContentPane().setLayout(null);
  47. Label label = new Label("账号:");
  48. label.setAlignment(Label.CENTER);
  49. label.setBounds(116, 49, 50, 23);
  50. frame.getContentPane().add(label);
  51. Label label_1 = new Label("密码:");
  52. label_1.setAlignment(Label.CENTER);
  53. label_1.setBounds(116, 85, 50, 23);
  54. frame.getContentPane().add(label_1);
  55. Label label_2 = new Label("用户状态:");
  56. label_2.setBounds(433, 49, 60, 23);
  57. frame.getContentPane().add(label_2);
  58. Label label_3 = new Label("未登录");
  59. label_3.setForeground(new Color(255, 0, 0));
  60. label_3.setBounds(499, 49, 40, 23);
  61. frame.getContentPane().add(label_3);
  62. JFormattedTextField formattedTextField = new JFormattedTextField();
  63. formattedTextField.setBounds(172, 49, 166, 23);
  64. frame.getContentPane().add(formattedTextField);
  65. passwordField = new JPasswordField();
  66. passwordField.setBounds(172, 85, 166, 23);
  67. frame.getContentPane().add(passwordField);
  68. JButton button = new JButton("login");
  69. button.setBackground(new Color(255, 255, 255));
  70. button.setBounds(126, 121, 212, 23);
  71. frame.getContentPane().add(button);
  72. button.addActionListener(new ActionListener() {
  73. public void actionPerformed(ActionEvent e) {
  74. String getUserName = formattedTextField.getText();
  75. String getUserPwd = passwordField.getText();
  76. if (userName.equals(getUserName) && userPwd.equals(getUserPwd)) {
  77. isLogin = true;
  78. } else {
  79. isLogin = false;
  80. }
  81. if (isLogin) {
  82. JOptionPane.showMessageDialog(null, "登录成功!", "消息", JOptionPane.PLAIN_MESSAGE);
  83. label_3.setText("已登录");
  84. label_3.setForeground(Color.BLUE);
  85. } else {
  86. JOptionPane.showMessageDialog(null, "登录失败!", "消息", JOptionPane.WARNING_MESSAGE);
  87. label_3.setText("未登录");
  88. label_3.setForeground(Color.RED);
  89. }
  90. }
  91. });
  92. }
  93. }

 四、解决中文乱码问题

Run as > Run Condiguration,在Arguments中增加下面这句话:

-Dfile.encoding=gbk

 

 

版权声明:本文为zhanzhuang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zhanzhuang/p/9993673.html