Kevin_K edited 6 年前
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Main{
public static int StringToInt(String s){
if (s.length()==0){
return -1;
}
for (int i=0;i<s.length();i++){
if (!Character.isDigit(s.charAt(i))){
return -1;
}
}
return Integer.valueOf(s);
}
public static void deal(int a,int b){
if (a<1||b<1||a>1000000||b>100000||a<b){
JFrame error=new JFrame("ERROR");
JPanel resu=new JPanel();
JLabel err=new JLabel("Should check something!");
err.setForeground(Color.red);
err.setFont(new Font("黑体",Font.BOLD,24));
resu.add(err);
error.add(resu);
error.setSize(400,80);
error.setVisible(true);
}
else{
ArrayList<Integer> l=new ArrayList<Integer>();
String[] r=new String[b];
JFrame result=new JFrame("Result");
for (int i=1;i<=a;i++){
l.add(i);
}
for (int i=0;i<b;i++){
int x=(int)(l.size()*Math.random());
r[i]=String.valueOf(l.get(x));
l.remove(x);
}
JPanel res=new JPanel();
JList list=new JList(r);
list.setVisibleRowCount(20);
list.setFixedCellWidth(200);
res.add(new JScrollPane(list));
result.getContentPane().add(BorderLayout.CENTER,res);
result.setSize(250,420);
result.setVisible(true);
}
}
public static void main(String[] args){
JFrame input=new JFrame("Input");
JTextField total=new JTextField();
JTextField participation=new JTextField();
JButton button=new JButton("OK");
input.setDefaultCloseOperation(input.EXIT_ON_CLOSE);
input.addWindowListener(new WindowAdapter(){
public void actionPerformed(WindowEvent w){
System.exit(0);
}
});
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int a=StringToInt(total.getText());
int b=StringToInt(participation.getText());
deal(a,b);
}
});
JPanel label=new JPanel();
label.setLayout(new BoxLayout(label,BoxLayout.Y_AXIS));
JPanel text=new JPanel();
text.setLayout(new BoxLayout(text,BoxLayout.Y_AXIS));
label.add(new JLabel("Total:"));
label.add(new JLabel("Participation:"));
text.add(total);
text.add(participation);
input.getContentPane().add(BorderLayout.WEST,label);
input.getContentPane().add(BorderLayout.CENTER,text);
input.getContentPane().add(BorderLayout.SOUTH,button);
input.setSize(300,100);
input.setVisible(true);
}
}
OOP是什么?QAQ我不知道呢!
打包成jar就能好好玩耍了。
说明书密码: 8uxe
我实在太怂了,所以不敢上传jar文件。
本程序效果图如下: