java 五个纵向的文本框和一个按钮,向五个文本框分别输入一个数字,按钮后数字在原五个文本框中重新排序
发布网友
发布时间:2022-05-30 07:16
我来回答
共1个回答
热心网友
时间:2023-10-14 09:45
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Ex1 extends JFrame implements ActionListener
{
private JTextField f1 = null;
private JTextField f2 = null;
private JTextField f3 = null;
private JTextField f4 = null;
private JButton button = null;
private static int num1;
private static int num2;
private static int num3;
private static int num4;
public Ex1()
{
f1 = new JTextField(5);
f2 = new JTextField(5);
f3 = new JTextField(5);
f4 = new JTextField(5);
button = new JButton("重新排列");
button.addActionListener(this);
this.setLayout(new FlowLayout());
this.add(f1);
this.add(f2);
this.add(f3);
this.add(f4);
this.add(button);
this.setBounds(300, 300, 50, 200);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String[] args)
{
new Ex1();
}
@Override
public void actionPerformed(ActionEvent arg0)
{
String str = arg0.getActionCommand();
num1 = Integer.parseInt(f1.getText().trim());
num2 = Integer.parseInt(f2.getText().trim());
num3 = Integer.parseInt(f3.getText().trim());
num4 = Integer.parseInt(f4.getText().trim());
int[] num = {num1, num2, num3, num4};
if ("重新排列".equals(str))
{
int a = (int)(Math.random() * 4);
int b = 0;
int c = 0;
int d = 0;
f1.setText(num[a] + "");
while (true)
{
b = (int)(Math.random() * 4);
if (a != b)
{
f2.setText(num[b] + "");
break;
}
}
while (true)
{
c = (int)(Math.random() * 4);
if (c != a && c != b)
{
f3.setText(num[c] + "");
break;
}
}
while (true)
{
d = (int)(Math.random() * 4);
if (d != a && d != b && d != c)
{
f4.setText(num[d] + "");
break;
}
}
}
}
}
javascript 刚学不会,就用笨方法了,但是本代码局限性很强,希望你能找到好的答案,给我一份,谢谢!!