//这个绝对能够知足您的需求。
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
public class ShowSame extends JFrame implements ActionListener{
JLabel aLabel=new JLabel(“数 A:”);
JLabel bLabel=new JLabel(“数 B:”);
JLabel cLabel=new JLabel(“数 C:”);
public static JTextField aVal=new JTextField(10);
public static JTextField bVal=new JTextField(10);
public static JTextField cVal=new JTextField(10);
public static char cs[]={‘1′,’2′,’3’};
public static JButton butnShow=new JButton(” “);//显示效果的button
JButton butnQuit= new JButton(“退出”);
public ShowSame(String s) {
super(s);
init();
setLayout(null);
add(aLabel);
aLabel.setBounds(45, 15, 100, 25);
add(bLabel);
bLabel.setBounds(45, 50, 100, 25);
add(cLabel);
cLabel.setBounds(45, 85, 100, 25);
add(aVal);
aVal.setBounds(105, 15, 110, 25);
add(bVal);
bVal.setBounds(105, 50, 110, 25);
add(butnShow);
cVal.setBounds(105, 85, 110, 25);
add(cVal);
butnShow.setBounds(45, 120, 80, 25);
add(butnQuit);
butnQuit.setBounds(135, 120, 80, 25);
butnQuit.addActionListener(this);
setVisible(true);
setSize(300, 200);
setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 3,
Toolkit.getDefaultToolkit().getScreenSize().width / 6);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();//刷新
}
private void init() {//初始化JTextFiled的document和添加监听事宜
aVal.setDocument(new Digit123Docunent());
bVal.setDocument(new Digit123Docunent());
cVal.setDocument(new Digit123Docunent());
}
public static void main(String[] args) {
ShowSame add = new ShowSame(“相同的数”);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==butnQuit){
System.exit(1);
}
}
}
//自界说仅能输入数字123且不能重复的文档类型
class Digit123Docunent extends PlainDocument{
int size=0;
public void insertString(int off,String s,AttributeSet a){
if(s.matches(“[123]”)){
try {
String hasInput=getText(0, size);//已经输入的
if(size=0&&b.indexOf(ShowSame.cs[i])>=0&&c.indexOf(ShowSame.cs[i])>=0){
result+=ShowSame.cs[i];
}
}
if(!””.equals(result)){
ShowSame.butnShow.setText(result);
}else {
ShowSame.butnShow.setText(” “);
}
}
}
今天加入了华为的面试,被一个算法题水了?如