请设计一个GUI界面,参考如下界面原型实现。求大神用JAVA
功能要求:在界面中的7个按钮上随机产生1-33之间的整数,并且按照从小到大升序进行排列,各个按钮上的数值不能够重复。
(1) 当点击“创建数字”按钮时,可以随机为7个数字按钮赋值(随机值),并且数值范围在1-33之间的整数(互不重复),点击“排序”按钮可以对数值进行排序操作。
(2) 当点击7个数字按钮中的任何一个按钮时,可以产生新的数字,并且新的随机数字与其它6个数字按钮上的数字不能够重复。然后点击“排序”按钮可以进行排序。
(3)当点击“重置”按钮,可以将7个数字按钮上的数字全部重置为0。
排序算法:不允许使用Arrays类中的工具方法。 展开
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyPanel extends JFrame {
private JButton[] btns = new JButton[7];
private Random rn = new Random();
private boolean[] flags = new boolean[33];// 用来判别重复
public MyPanel() {
init();
}
private void init() {
setTitle("MyPanel....");
setSize(540, 250);
setContentPane(createPane());
}
private JPanel createPane() {
JPanel pane = new JPanel(null);
pane.setBackground(Color.YELLOW);
for (int i = 0; i < btns.length; i++) {
btns[i] = new JButton("0");
btns[i].setBounds(20 + (i * 70), 30, 54, 24);
btns[i].setBackground(Color.PINK);
pane.add(btns[i]);
btns[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
while (true) {
int n = rn.nextInt(34);
// 如果数字被使用,或为 33 34 就重新选数字
if (n == 0 || n > 33 || flags[n - 1]) {
continue;
}
// 把使用了的数字设置为true,即已使用
flags[n - 1] = true;
// 把不用了的数字设置成未使用状态
if (Integer.parseInt(((JButton) e.getSource())
.getText()) != 0) {
flags[Integer.parseInt(((JButton) e.getSource())
.getText()) - 1] = false;
}
((JButton) e.getSource()).setText(String.valueOf(n));
break;
}
}
});
}
JButton creNum = new JButton("创建数字");
creNum.setBounds(50, 150, 100, 30);
creNum.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < btns.length; i++) {
while (true) {
int n = rn.nextInt(34);
// 如果数字被使用,或为 33 34 就重新选数字
if (n == 0 || n > 33 || flags[n - 1]) {
continue;
}
// 把使用了的数字设置为true,即已使用
flags[n - 1] = true;
// 把不用了的数字设置成未使用状态
if (Integer.parseInt(btns[i].getText()) != 0) {
flags[Integer.parseInt(btns[i].getText()) - 1] = false;
}
btns[i].setText(String.valueOf(n));
break;
}
}
}
});
pane.add(creNum);
JButton sort = new JButton("排序");
sort.setBounds(200, 150, 100, 30);
sort.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 排序
String[] strs = new String[7];
// 把按钮上面的数字拿出来
for (int i = 0; i < btns.length; i++) {
strs[i] = btns[i].getText();
}
// 开始排序
for (int i = strs.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (Integer.parseInt(strs[i]) < Integer
.parseInt(strs[j])) {
String temp;
temp = strs[i];
strs[i] = strs[j];
strs[j] = temp;
}
}
}
// 排好,在显示到按钮上
for (int j = 0; j < strs.length; j++) {
btns[j].setText(strs[j]);
}
}
});
pane.add(sort);
JButton reset = new JButton("重置");
reset.setBounds(350, 150, 100, 30);
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flags = new boolean[33];
for (int i = 0; i < btns.length; i++) {
btns[i].setText("0");
}
}
});
pane.add(reset);
return pane;
}
public static void main(String[] args) {
MyPanel mp = new MyPanel();
mp.setVisible(true);
}
}
根据你的要求,数字不会重复,用的是冒泡排序
希望对你有帮助
如图:
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class SortRandomNumber extends JPanel {
private int[] numbers = new int[33];
private Random r = new Random();
public SortRandomNumber() {
setPreferredSize(new Dimension(400, 100));
setBackground(new Color(249, 249, 0));
final JButton[] buttons = { new JButton("0"), new JButton("0"),
new JButton("0"), new JButton("0"), new JButton("0"),
new JButton("0"), new JButton("0") };
JPanel btnPanel = new JPanel();
btnPanel.setOpaque(false);
setLayout(new BorderLayout());
add(btnPanel, BorderLayout.NORTH);
for (JButton b : buttons) {
b.setBackground(new Color(205, 151, 249));
b.setFocusPainted(false);
btnPanel.add(b);
}
JPanel opPanel = new JPanel();
opPanel.setOpaque(false);
add(opPanel, BorderLayout.SOUTH);
final JButton[] opButtons = { new JButton("创建数字"), new JButton("排序"),
new JButton("重置") };
for (JButton b : opButtons) {
b.setBackground(new Color(152, 205, 249));
b.setFocusPainted(false);
b.setFont(new Font("微软雅黑", Font.PLAIN, 16));
opPanel.add(b);
}
opButtons[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingWorker<int[], Object> worker = new SwingWorker<int[], Object>() {
@Override
protected int[] doInBackground() throws Exception {
Arrays.fill(numbers, 0);
for (int i = 0; i < numbers.length; i++) {
int temp = r.nextInt(33);
if (numbers[temp] != 0) {
i--;
continue;
}
numbers[temp] = i + 1;
}
return numbers;
}
@Override
protected void done() {
for (int i = 0; i < buttons.length; i++) {
buttons[i].setText(String.valueOf(numbers[i]));
}
}
};
worker.execute();
}
});
opButtons[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final int[] temp = new int[7];
for (int i = 0; i < temp.length; i++) {
temp[i] = Integer.parseInt(buttons[i].getActionCommand());
}
SwingWorker<int[], Object> worker = new SwingWorker<int[], Object>() {
@Override
protected int[] doInBackground() throws Exception {
sort(temp, temp.length);
return null;
}
@Override
protected void done() {
for (int i = 0; i < temp.length; i++) {
buttons[i].setText(String.valueOf(temp[i]));
}
}
};
worker.execute();
}
});
opButtons[2].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < buttons.length; i++) {
buttons[i].setText("0");
}
}
});
for (int i = 0; i < buttons.length; i++) {
final int j = i;
buttons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
int value0 = Integer
.parseInt(buttons[0].getActionCommand());
int value1 = Integer
.parseInt(buttons[1].getActionCommand());
int value2 = Integer
.parseInt(buttons[2].getActionCommand());
int value3 = Integer
.parseInt(buttons[3].getActionCommand());
int value4 = Integer
.parseInt(buttons[4].getActionCommand());
int value5 = Integer
.parseInt(buttons[5].getActionCommand());
int value6 = Integer
.parseInt(buttons[6].getActionCommand());
int newValue;
while (true) {
newValue = r.nextInt(33) + 1;
if (newValue != value0 && newValue != value1
&& newValue != value2 && newValue != value3
&& newValue != value4 && newValue != value5
&& newValue != value6) {
break;
}
}
b.setText(String.valueOf(newValue));
}
});
}
}
private void sort(int[] numbers, int length) {
for (int i = 0; i < length - 1; i++) {
for (int j = i + 1; j < length; j++) {
if (numbers[i] > numbers[j]) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Sort Random Numbers");
f.add(new SortRandomNumber());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Arrays;
import java.util.Random;
import javax.swing.JButton;
public class Test extends Frame{
private Label[] text=new Label[7];
private Button b1,b2,b3;
private Panel p1,p2;
private Random r=new Random();
private int[] number=new int[7];
public static void main(String[] arg){
new Test();
}
public Test(){
this.setSize(500, 300);
this.setLocation(500, 200);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
LabelListener ll=new LabelListener();
for(int i=0;i<7;i++){
text[i]=new Label("0");
text[i].addMouseListener(ll);
}
b1=new Button("创建数字");
b1.setActionCommand("create");
b2=new Button("排序");
b2.setActionCommand("sort");
b3=new Button("重置");
b3.setActionCommand("resert");
p1=new Panel();
p2=new Panel();
p1.setLayout(new GridLayout(1,7));
p2.setLayout(new GridLayout(1,3));
for(int i=0;i<7;i++)
p1.add(text[i]);
p2.add(b1);
p2.add(b2);
p2.add(b3);
this.add(p1,BorderLayout.NORTH);
this.add(p2,BorderLayout.SOUTH);
MyListener ml=new MyListener();
b1.addActionListener(ml);
b2.addActionListener(ml);
b3.addActionListener(ml);
}
private class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="create"){
for(int i=0;i<7;i++){
int temp=createNum();
number[i]=temp;
text[i].setText(temp+"");
}
}
if(e.getActionCommand()=="sort"){
for(int i=0;i<7;i++){
number[i]=Integer.parseInt(text[i].getText());
}
number=sort(number);
for(int i=0;i<7;i++){
text[i].setText(number[i]+"");
}
}
if(e.getActionCommand()=="resert"){
for(int i=0;i<7;i++){
text[i].setText("0");
}
}
}
}
public int[] sort(int[] num){
for(int i=0;i<7;i++)
for(int j=i+1;j<7;j++)
if(num[i]>num[j]){
int temp=num[i];
num[i]=num[j];
num[j]=temp;
}
return num;
}
private class LabelListener extends MouseAdapter{
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
Label label=(Label) e.getSource();
label.setText(createNum()+"");
}
}
public int createNum(){
int temp=r.nextInt(33)+1;
for(int i:number)
if(temp==i)
createNum();
return temp;
}
}