欢迎光临散文网 会员登陆 & 注册

Java swing超详细翻译 变色 焦点获取事件,判断输入内容,获取打印下拉框【诗书画唱】

2020-05-28 13:11 作者:诗书画唱  | 我要投稿

要做的功能是点击按钮的时候判断用户输入的内容是否大于6位并且小于12位,如果符合就打印符合内容

否则打印不符合内容


package swing;


public class mains {


public static void main(String[] args) {


new swingBianSe();

}


}


package swing;

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.FocusEvent;

import java.awt.event.FocusListener;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;


import javax.swing.*;

public class swingBianSe extends JFrame{

public static JLabel lb1,lb2,lb3=null;

public static JCheckBox ck1,ck2,ck3=null;

public static JRadioButton rb1,rb2=null;

public static JTextArea jt1=null;

public static JButton btn1=null;

public static JTextField txt=null;

public static JPasswordField pwd=null;

public static JComboBox com1=null;

//在构造方法里写内容

public swingBianSe(){

//使用控件的三个步骤

lb1=new JLabel("请输入你的用户名");

lb2=new JLabel("请输入你的用户密码");

lb1.setBounds(250,100,150,30);

lb2.setBounds(250,140,150,30);

this.add(lb1);

this.add(lb2);

com1=new JComboBox();

com1.addItem("管理员");

com1.addItem("普通用户");

com1.addItemListener(new swingBianSe_shijian(this));

com1.setBounds(100,50,140,30);

this.add(com1);

pwd=new JPasswordField();

pwd.setBounds(100,140,150,30);

this.add(pwd);

//1.声明控件

txt=new JTextField();

txt.addFocusListener(new swingBianSe_shijian(this));

//2.设置控件的位置

txt.setBounds(100,100,150,30);

//3.添加到窗体上

this.add(txt);

btn1=new JButton("提交");

btn1.setBounds(100,140,70,30);

btn1.addActionListener(new swingBianSe_shijian(this));

//实例化一个类的时候首先执行的是构造方法

this.add(btn1);

this.setTitle("诗书画唱的窗体 ");

this.setLayout(null);

this.setSize(600,600);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

}


class swingBianSe_shijian implements ActionListener,

ItemListener,FocusListener{

//声明一个空变量用来接收传入的窗体

public swingBianSe ss;

public swingBianSe_shijian(swingBianSe s){

ss=s;

}

@Override

public void actionPerformed(ActionEvent arg0) {

//接收窗体的内容



}

@Override

public void itemStateChanged(ItemEvent arg0) {

// TODO Auto-generated method stub

String str=ss.com1.getSelectedItem().toString();

JOptionPane.showMessageDialog(null,str);

}

@Override

public void focusGained(FocusEvent arg0) {

// TODO Auto-generated method stub


}

//失去焦点

@Override

public void focusLost(FocusEvent arg0) {

// TODO Auto-generated method stub

//获取文本框中的内容

String uname=ss.txt.getText();

if(uname.length()>6&&uname.length()<12){

ss.lb1.setText("用户名输入正确");

ss.lb1.setForeground(Color.green);

}else{

ss.lb1.setText("请输入6到12位的用户名");

ss.lb1.setForeground(Color.red);

}

}



}



Java swing超详细翻译 变色 焦点获取事件,判断输入内容,获取打印下拉框【诗书画唱】的评论 (共 条)

分享到微博请遵守国家法律