用java编写一个登录界面,用SWING组件

用java编写一个登录界面,用SWING组件,第1张

import javaawtColor;

import javaawtPoint;

import javaawtToolkit;

import javaawteventKeyEvent;

import javaawteventKeyListener;

import javaawteventMouseEvent;

import javaawteventMouseListener;

import javaxswingBorderFactory;

import javaxswingButtonGroup;

import javaxswingJButton;

import javaxswingJCheckBox;

import javaxswingJFrame;

import javaxswingJLabel;

import javaxswingJList;

import javaxswingJRadioButton;

import javaxswingJTextArea;

import javaxswingJTextField;

import javaxswingListSelectionModel;

/

一个简单的Swing窗口,输入内容单击“确定”按钮后,在文本域中显示输入的内容。

单击“取消”按钮,清空页面内容。

@author yzg

/

public class Register extends JFrame {

private static final long serialVersionUID = 1L;

private JLabel nameLabel;

private JTextArea context;

private JTextField name;

private JLabel pLabel;

JList speciality;

JLabel mLabel;

String[] data = { "计算机", "英语", "机械", "化工" };

ButtonGroup bg;

JRadioButton male;

JRadioButton female;

JLabel fLabel;

JCheckBox faverite1;

JCheckBox faverite2;

JCheckBox faverite3;

JCheckBox faverite4;

public Register(String title) {

super(title);

thisgetContentPane()setLayout(null);

// 下面两行是取得屏幕的高度和宽度

double lx = ToolkitgetDefaultToolkit()getScreenSize()getWidth();

double ly = ToolkitgetDefaultToolkit()getScreenSize()getHeight();

thissetLocation(new Point((int) (lx / 2) - 150, (int) (ly / 2) - 200));// 设定窗口出现位置

thissetSize(340, 440);// 设定窗口大小

}

public void showWin() {

// 确保窗体有一个好的外观装饰

// setDefaultLookAndFeelDecorated(true);

thissetDefaultCloseOperation(EXIT_ON_CLOSE);

// 姓名

nameLabel = new JLabel("姓名 :");

nameLabelsetBounds(30, 10, 50, 25);

name = new JTextField();

namesetBounds(80, 10, 120, 20);

namesetBorder(BorderFactorycreateLineBorder(ColorBLUE));

nameaddKeyListener(new KeyListener() {

public void keyPressed(KeyEvent e) {

}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

if (namegetText()length() > 6) {

namesetText(namegetText()substring(0, 6));

}

}

});

// 专业 组合框

pLabel = new JLabel("专业 :");

pLabelsetBounds(30, 40, 50, 25);

speciality = new JList(data);

specialitysetSelectionMode(ListSelectionModelSINGLE_SELECTION);

specialitysetBounds(80, 40, 80, 85);

specialitysetBorder(BorderFactorycreateLineBorder(ColorGREEN));

mLabel = new JLabel("性别 :");

mLabelsetBounds(30, 130, 50, 25);

// 性别 单选框

bg = new ButtonGroup();

male = new JRadioButton("男");

female = new JRadioButton("女");

bgadd(male);

bgadd(female);

malesetBounds(80, 130, 60, 25);

femalesetBounds(140, 130, 60, 25);

fLabel = new JLabel("爱好 :");

fLabelsetBounds(30, 160, 50, 25);

// 爱好 复选框

faverite1 = new JCheckBox("音乐");

faverite2 = new JCheckBox("足球");

faverite3 = new JCheckBox("高尔夫");

faverite4 = new JCheckBox("游戏");

faverite1setBounds(80, 160, 60, 25);

faverite2setBounds(140, 160, 60, 25);

faverite3setBounds(200, 160, 65, 25);

faverite4setBounds(265, 160, 60, 25);

// 内容 文本区域

JLabel conLabel = new JLabel("输入的内容 :");

conLabelsetBounds(30, 250, 90, 25);

context = new JTextArea();

contextsetBounds(30, 270, 260, 100);

contextsetBorder(BorderFactorycreateLineBorder(Colorblack));

// 确定按钮

JButton ok = new JButton("确定");

oksetBounds(50, 190, 60, 25);

okaddMouseListener(new MouseListener() {

public void mouseClicked(MouseEvent e) {

StringBuffer sb = new StringBuffer();

sbappend(nameLabelgetText())append(namegetText());

sbappend("\n");

int index = specialitygetSelectedIndex();

if (index >= 0) {

sbappend(pLabelgetText())append(data[index]);

} else {

sbappend(pLabelgetText());

}

sbappend("\n");

sbappend(mLabelgetText());

if (maleisSelected()) {

sbappend("男");

}

if (femaleisSelected()) {

sbappend("女");

}

sbappend("\n");

sbappend(fLabelgetText());

if (faverite1isSelected()) {

sbappend("音乐 ");

}

if (faverite2isSelected()) {

sbappend("足球 ");

}

if (faverite3isSelected()) {

sbappend("高尔夫 ");

}

if (faverite4isSelected()) {

sbappend("游戏 ");

}

contextsetText(sbtoString());

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

});

// 取消按钮

JButton cancel = new JButton("取消");

cancelsetBounds(120, 190, 60, 25);

canceladdMouseListener(new MouseListener() {

public void mouseClicked(MouseEvent e) {

namesetText("");

specialityclearSelection();

if (faverite1isSelected()) {

faverite1setSelected(false);

}

if (faverite2isSelected()) {

faverite2setSelected(false);

}

if (faverite3isSelected()) {

faverite3setSelected(false);

}

if (faverite4isSelected()) {

faverite4setSelected(false);

}

contextsetText("");

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

});

thisgetContentPane()add(nameLabel);

thisgetContentPane()add(name);

thisgetContentPane()add(pLabel);

thisgetContentPane()add(speciality);

thisgetContentPane()add(mLabel);

thisgetContentPane()add(male);

thisgetContentPane()add(female);

thisgetContentPane()add(fLabel);

thisgetContentPane()add(faverite1);

thisgetContentPane()add(faverite2);

thisgetContentPane()add(faverite3);

thisgetContentPane()add(faverite4);

thisgetContentPane()add(conLabel);

thisgetContentPane()add(context);

thisgetContentPane()add(ok);

thisgetContentPane()add(cancel);

// thispack();

thissetVisible(true);

}

/

@param args

/

public static void main(String[] args) {

Register reg = new Register("Register");

regshowWin();

}

}

应用的还是web的啊

我给你写了个应用的哈

你在C盘建个testtxt文件

里面写

username:用户名(这里可以随便写哈)

password:同上哈

注意是要换行的哦。。

比如

username:tiger

password:tiger

然后你建个Login的类

然后把下面的代码弄进去

运行就是了

注意登陆的时候你文件里面设定的什么用户名和密码就输入什么哈

输入错误就会提示输入错误的

import javaioBufferedReader;

import javaioFileReader;

import javaxswingJFrame;

import javaxswingJOptionPane;

/

@author thinkpad

/

public class Login extends javaxswingJFrame {

/ Creates new form Login /

private static String username;

private static String password;

public Login() {

initComponents();

try {

BufferedReader br = new BufferedReader(new FileReader("C:\\testtxt"));

username = brreadLine()split("\\:")[1];

password = brreadLine()split("\\:")[1];

Systemoutprintln(username + password);

} catch(Exception e) {

eprintStackTrace();

}

}

/ This method is called from within the constructor to

initialize the form

WARNING: Do NOT modify this code The content of this method is

always regenerated by the Form Editor

/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jPanel1 = new javaxswingJPanel();

jLabel1 = new javaxswingJLabel();

jLabel2 = new javaxswingJLabel();

jLabel3 = new javaxswingJLabel();

jTextField1 = new javaxswingJTextField();

jPasswordField1 = new javaxswingJPasswordField();

jButton1 = new javaxswingJButton();

jButton2 = new javaxswingJButton();

setDefaultCloseOperation(javaxswingWindowConstantsEXIT_ON_CLOSE);

jLabel1setFont(new javaawtFont("宋体", 0, 18)); // NOI18N

jLabel1setText("Login");

jLabel2setText("Username:");

jLabel3setText("Password:");

jTextField1addActionListener(new javaawteventActionListener() {

public void actionPerformed(javaawteventActionEvent evt) {

jTextField1ActionPerformed(evt);

}

});

jPasswordField1addActionListener(new javaawteventActionListener() {

public void actionPerformed(javaawteventActionEvent evt) {

jPasswordField1ActionPerformed(evt);

}

});

jButton1setText("Login");

jButton1addActionListener(new javaawteventActionListener() {

public void actionPerformed(javaawteventActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

jButton2setText("Reset");

jButton2addActionListener(new javaawteventActionListener() {

public void actionPerformed(javaawteventActionEvent evt) {

jButton2ActionPerformed(evt);

}

});

javaxswingGroupLayout jPanel1Layout = new javaxswingGroupLayout(jPanel1);

jPanel1setLayout(jPanel1Layout);

jPanel1LayoutsetHorizontalGroup(

jPanel1LayoutcreateParallelGroup(javaxswingGroupLayoutAlignmentLEADING)

addGroup(jPanel1LayoutcreateSequentialGroup()

addGroup(jPanel1LayoutcreateParallelGroup(javaxswingGroupLayoutAlignmentLEADING)

addGroup(jPanel1LayoutcreateSequentialGroup()

addGap(28, 28, 28)

addGroup(jPanel1LayoutcreateParallelGroup(javaxswingGroupLayoutAlignmentLEADING, false)

addGroup(jPanel1LayoutcreateSequentialGroup()

addGroup(jPanel1LayoutcreateParallelGroup(javaxswingGroupLayoutAlignmentLEADING)

addComponent(jLabel2)

addComponent(jLabel3))

addGap(33, 33, 33)

addGroup(jPanel1LayoutcreateParallelGroup(javaxswingGroupLayoutAlignmentTRAILING, false)

addComponent(jPasswordField1, 0, 0, ShortMAX_VALUE)

addComponent(jTextField1, javaxswingGroupLayoutDEFAULT_SIZE, 108, ShortMAX_VALUE)))

addGroup(jPanel1LayoutcreateSequentialGroup()

addComponent(jButton1)

addPreferredGap(javaxswingLayoutStyleComponentPlacementRELATED, javaxswingGroupLayoutDEFAULT_SIZE, ShortMAX_VALUE)

addComponent(jButton2))))

addGroup(jPanel1LayoutcreateSequentialGroup()

addGap(101, 101, 101)

addComponent(jLabel1)))

addContainerGap(javaxswingGroupLayoutDEFAULT_SIZE, ShortMAX_VALUE))

);

jPanel1LayoutsetVerticalGroup(

jPanel1LayoutcreateParallelGroup(javaxswingGroupLayoutAlignmentLEADING)

addGroup(jPanel1LayoutcreateSequentialGroup()

addContainerGap()

addComponent(jLabel1)

addGap(18, 18, 18)

addGroup(jPanel1LayoutcreateParallelGroup(javaxswingGroupLayoutAlignmentBASELINE)

addComponent(jTextField1, javaxswingGroupLayoutPREFERRED_SIZE, javaxswingGroupLayoutDEFAULT_SIZE, javaxswingGroupLayoutPREFERRED_SIZE)

addComponent(jLabel2))

addGap(18, 18, 18)

addGroup(jPanel1LayoutcreateParallelGroup(javaxswingGroupLayoutAlignmentBASELINE)

addComponent(jPasswordField1, javaxswingGroupLayoutPREFERRED_SIZE, javaxswingGroupLayoutDEFAULT_SIZE, javaxswingGroupLayoutPREFERRED_SIZE)

addComponent(jLabel3))

addPreferredGap(javaxswingLayoutStyleComponentPlacementRELATED, 35, ShortMAX_VALUE)

addGroup(jPanel1LayoutcreateParallelGroup(javaxswingGroupLayoutAlignmentBASELINE)

addComponent(jButton1)

addComponent(jButton2)))

);

javaxswingGroupLayout layout = new javaxswingGroupLayout(getContentPane());

getContentPane()setLayout(layout);

layoutsetHorizontalGroup(

layoutcreateParallelGroup(javaxswingGroupLayoutAlignmentLEADING)

addGroup(layoutcreateSequentialGroup()

addContainerGap()

addComponent(jPanel1, javaxswingGroupLayoutPREFERRED_SIZE, javaxswingGroupLayoutDEFAULT_SIZE, javaxswingGroupLayoutPREFERRED_SIZE)

addContainerGap(javaxswingGroupLayoutDEFAULT_SIZE, ShortMAX_VALUE))

);

layoutsetVerticalGroup(

layoutcreateParallelGroup(javaxswingGroupLayoutAlignmentLEADING)

addGroup(javaxswingGroupLayoutAlignmentTRAILING, layoutcreateSequentialGroup()

addComponent(jPanel1, javaxswingGroupLayoutDEFAULT_SIZE, javaxswingGroupLayoutDEFAULT_SIZE, ShortMAX_VALUE)

addContainerGap())

);

pack();

}// </editor-fold>

private void jPasswordField1ActionPerformed(javaawteventActionEvent evt) {

// TODO add your handling code here:

}

private void jTextField1ActionPerformed(javaawteventActionEvent evt) {

// TODO add your handling code here:

}

private void jButton2ActionPerformed(javaawteventActionEvent evt) {

thisjTextField1setText("");

thisjPasswordField1setText("");

}

private void jButton1ActionPerformed(javaawteventActionEvent evt) {

String inputUsername = thisjTextField1getText();

String inputPassword = StringvalueOf(thisjPasswordField1getPassword());

if(inputUsernameequals(username) && inputPasswordequals(password)) {

JOptionPaneshowMessageDialog(this, "Login success!");

} else {

JOptionPaneshowMessageDialog(this, "Login failed!");

}

}

/

@param args the command line arguments

/

public static void main(String args[]) {

javaawtEventQueueinvokeLater(new Runnable() {

public void run() {

JFramesetDefaultLookAndFeelDecorated(true);

Login login = new Login();

loginsetVisible(true);

loginsetLocationRelativeTo(null);

}

});

}

// Variables declaration - do not modify

private javaxswingJButton jButton1;

private javaxswingJButton jButton2;

private javaxswingJLabel jLabel1;

private javaxswingJLabel jLabel2;

private javaxswingJLabel jLabel3;

private javaxswingJPanel jPanel1;

private javaxswingJPasswordField jPasswordField1;

private javaxswingJTextField jTextField1;

// End of variables declaration

}

以上就是关于用java编写一个登录界面,用SWING组件全部的内容,包括:用java编写一个登录界面,用SWING组件、JAVA 设计swing登录界面验证用户名密码、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9486638.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存