Java swing超詳細(xì)翻譯 變色 焦點獲取事件,判斷輸入內(nèi)容,獲取打印下拉框【詩書畫唱】

要做的功能是點擊按鈕的時候判斷用戶輸入的內(nèi)容是否大于6位并且小于12位,如果符合就打印符合內(nèi)容
否則打印不符合內(nèi)容

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;
//在構(gòu)造方法里寫內(nèi)容
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.設(shè)置控件的位置
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));
//實例化一個類的時候首先執(zhí)行的是構(gòu)造方法
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) {
//接收窗體的內(nèi)容
}
@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
//獲取文本框中的內(nèi)容
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);
}
}
}




