求JAVA算法
现在三个JTextField,每个JTextField都可以在里面输入1,2,3这三个数,也可以不输,但不准重复,且最多只能输三个,最后在一个JButton里面显示出这个...
现在三个JTextField,每个JTextField都可以在里面输入1,2,3这三个数,也可以不输,但不准重复,且最多只能输三个,最后在一个JButton里面显示出这个三个JTextField里面相同的数。
例如三个JT中的数分别为12,1,1;那么JButton中要显示1;
三个JT中的数分别为123,13,3;那么JButton中要显3;
三个JT中的数分别为23,123,23;那么JButton中要显示23;
如果三个JT中没有相同的,JButton中就什么都不显示
急啊,那位大虾能赶快帮下我 展开
例如三个JT中的数分别为12,1,1;那么JButton中要显示1;
三个JT中的数分别为123,13,3;那么JButton中要显3;
三个JT中的数分别为23,123,23;那么JButton中要显示23;
如果三个JT中没有相同的,JButton中就什么都不显示
急啊,那位大虾能赶快帮下我 展开
2个回答
展开全部
//这个绝对能够满足您的需求。
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<3&&!hasInput.contains(s)){//小于3且不能重复
super.insertString(off,s,a);
size++;
setButnShowText();
}
} catch (Exception e) {
}
}
}
public void remove(int off,int length){
size=size-length;
try {
super.remove(off,length);
setButnShowText();
} catch (Exception e) {
}
}
private void setButnShowText(){//设置按钮的显示文本
String a=ShowSame.aVal.getText();
String b=ShowSame.bVal.getText();
String c=ShowSame.cVal.getText();
String result="";
for (int i = 0; i <ShowSame.cs.length; i++) {
if(a.indexOf(ShowSame.cs[i])>=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(" ");
}
}
}
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<3&&!hasInput.contains(s)){//小于3且不能重复
super.insertString(off,s,a);
size++;
setButnShowText();
}
} catch (Exception e) {
}
}
}
public void remove(int off,int length){
size=size-length;
try {
super.remove(off,length);
setButnShowText();
} catch (Exception e) {
}
}
private void setButnShowText(){//设置按钮的显示文本
String a=ShowSame.aVal.getText();
String b=ShowSame.bVal.getText();
String c=ShowSame.cVal.getText();
String result="";
for (int i = 0; i <ShowSame.cs.length; i++) {
if(a.indexOf(ShowSame.cs[i])>=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(" ");
}
}
}
参考资料: 有问题的话,给我发百度消息
展开全部
public class Guess {
private JFrame jFrame = new JFrame("我要找相同的数据");
private JTextField jField = new JTextField("",20);
private JButton jButton = new JButton("查找");
private JLabel jLabel = new JLabel("");
Guess(){
}
public void giveJframe(){
jFrame.setLayout(new FlowLayout());
jFrame.setSize(500,300);
jFrame.add(jField);
jButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String str = jField.getText();
Guess guess = new Guess();
int[] array = guess.change2Array(str);
int find = guess.getMin(array);
String result = "";
result = guess.findString(str, find+"");
jLabel.setText(result);
}
});
jFrame.add(jButton);
jFrame.add(jLabel);
jFrame.setVisible(true);
}
public static void main(String[] args) {
new Guess().giveJframe();
}
public int getMin(int[] array){
int min = array[0];
for(int i=1;i<array.length;i++){
if(array[i]<min)min=array[i];
}
return min;
}
public String findString(String str,String find){
String[] temp = str.split(",");
int count = 0;
int i = 0;
while(i<temp.length){
if(temp[i].indexOf(find)!=-1){
count++;
}
i++;
}
if(count==temp.length){
return find;
}else{
return "没找到相同的数据";
}
}
public int[] change2Array(String str){
String[] temp = str.split(",");
int length = temp.length;
int[] intTemp = new int[length];
for(int i=0;i<length;i++){
intTemp[i] = Integer.parseInt(temp[i]);
}
return intTemp;
}
}
刚才不好意思!刚才传上去的是个错的!这个绝对可以满足你的要求!
private JFrame jFrame = new JFrame("我要找相同的数据");
private JTextField jField = new JTextField("",20);
private JButton jButton = new JButton("查找");
private JLabel jLabel = new JLabel("");
Guess(){
}
public void giveJframe(){
jFrame.setLayout(new FlowLayout());
jFrame.setSize(500,300);
jFrame.add(jField);
jButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String str = jField.getText();
Guess guess = new Guess();
int[] array = guess.change2Array(str);
int find = guess.getMin(array);
String result = "";
result = guess.findString(str, find+"");
jLabel.setText(result);
}
});
jFrame.add(jButton);
jFrame.add(jLabel);
jFrame.setVisible(true);
}
public static void main(String[] args) {
new Guess().giveJframe();
}
public int getMin(int[] array){
int min = array[0];
for(int i=1;i<array.length;i++){
if(array[i]<min)min=array[i];
}
return min;
}
public String findString(String str,String find){
String[] temp = str.split(",");
int count = 0;
int i = 0;
while(i<temp.length){
if(temp[i].indexOf(find)!=-1){
count++;
}
i++;
}
if(count==temp.length){
return find;
}else{
return "没找到相同的数据";
}
}
public int[] change2Array(String str){
String[] temp = str.split(",");
int length = temp.length;
int[] intTemp = new int[length];
for(int i=0;i<length;i++){
intTemp[i] = Integer.parseInt(temp[i]);
}
return intTemp;
}
}
刚才不好意思!刚才传上去的是个错的!这个绝对可以满足你的要求!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询