import java.awt.BorderLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Demo extends JFrame {
static HashMap<Integer, Shu> map = new HashMap<>();
static Iterator<?> it;
private static String name;
private static String sentence;
static {
map.put(1, new Shu("阿大", "我有我的好!!"));
map.put(2, new Shu("小二", "上菜了"));
map.put(3, new Shu("张三", "又到我了"));
map.put(4, new Shu("李四", "哎,咋是我啊"));
map.put(5, new Shu("乌龟", "千年之光阴,就这样过了"));
}
JButton sendBt;
JTextArea inputField;
JTextArea chatContent;
public static void main(String[] args) {
new Demo();
}
public Dem0_() {
this.setLayout(new BorderLayout());
JPanel inputPanel = new JPanel();
inputField = new JTextArea(1, 7);
inputField.setEditable(false);
sendBt = new JButton("点点点");
chatContent = new JTextArea(12, 34);
JScrollPane showPanel = new JScrollPane(chatContent);
chatContent.setEditable(false);
sendBt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getRandom();
String content = name + ":" + sentence;
if (content != null && !content.trim().equals("")) {
inputField.setText("");
chatContent.append(content + "\n");
inputField.append(name);
} else {
chatContent.append("竟然没有他!!!\n");
}
}
});
Label lable = new Label("抽中者:");
inputPanel.add(lable);
inputPanel.add(inputField);
inputPanel.add(sendBt);
this.add(inputPanel, BorderLayout.CENTER);
this.add(showPanel, BorderLayout.SOUTH);
this.setSize(400, 300);
this.setTitle("无聊而已");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void getRandom() {
Random r = new Random();
int temp = r.nextInt(5) + 1;
Shu shu = map.get(temp);
name = shu.getName();
sentence = shu.getSentence();
}
}
class Shu {
private int id;
private String name;
private String sentence;
public Shu() {
super();
}
public Shu(int id) {
super();
this.id = id;
}
public Shu(String name, String sentence) {
super();
this.name = name;
this.sentence = sentence;
}
public String getSentence() {
return sentence;
}
public void setSentence(String sentence) {
this.sentence = sentence;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
|
|