1 package basicCompoment;
2 import javax.swing.*;
3
4 import java.awt.*;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.WindowAdapter;
8 import java.awt.event.WindowEvent;
9
10 import javax.swing.Timer;
11
12 import java.io.*;
13 public class Aha{
14 static Boolean flag=true;
15 static Boolean flag2=true;
16 String filename;
17 String filename2;
18 int num=0;
19 int num2=0;
20 String[] datalist=new String[100];
21 String[] datalist2=new String[100];
22 public static void main(String[] args) {
23 Aha h=new Aha();
24 h.setUIFont();
25 h.go();
26 }
27 public void setup(){
28
29 }
30 public void go(){
31 JFrame frame=new JFrame("QQ673484761定制");
32 Container a=frame.getContentPane();
33 JPanel pane=new JPanel();
34 a.add(pane);
35 pane.setLayout(null);
36 //获取文件路径
37
38 JButton open=new JButton("导入txt名单");
39 open.setBounds(new Rectangle(10, 5, 280, 30));//参数分别是坐标x,y,宽,高
40 pane.add(open);
41
42 JButton open2=new JButton("导入txt试题");
43 open2.setBounds(new Rectangle(305, 5, 280, 30));
44 pane.add(open2);
45
46 open.addActionListener(new ActionListener(){
47 public void actionPerformed(ActionEvent e){
48 JFileChooser jfc=new JFileChooser();
49 jfc.setFileSelectionMode(JFileChooser.FILES_ONLY );
50 jfc.showDialog(new JLabel(), "选择");
51 File file=jfc.getSelectedFile();
52 System.out.println("文件:"+file.getAbsolutePath());
53 System.out.println(jfc.getSelectedFile().getName());
54 filename=file.getAbsolutePath();
55 //读取文件
56 if(filename != null){
57 File file1 = new File(filename);
58 StringBuffer contents = new StringBuffer();//StringBuffer类 字符串变量
59 BufferedReader reader = null;//BufferedReader字符流 BufferedInputStream字节流
60
61 try {
62 reader = new BufferedReader(new FileReader(file1));
63 String text = null;
64
65 //repeat until all lines is read
66 while ((text = reader.readLine()) != null) {
67 contents.append(text);
68 contents.append(System.getProperty("line.separator"));//读完一行增加一个换行符
69 num++;
70 }
71 } catch (FileNotFoundException e1) {
72 e1.printStackTrace();
73 } catch (IOException e1) {
74 e1.printStackTrace();
75 } finally {
76 try {
77 if (reader != null) {
78 reader.close();
79 }
80 }catch (IOException e1) {
81 e1.printStackTrace();
82 }
83 }
84 //show file contents here
85 System.out.println("文件内容为:");
86 System.out.println(contents.toString());
87 String temp=contents.toString();
88 datalist=temp.split("\n");
89 }
90 }
91 });
92 open2.addActionListener(new ActionListener(){
93 public void actionPerformed(ActionEvent e){
94 JFileChooser jfc=new JFileChooser();
95 jfc.setFileSelectionMode(JFileChooser.FILES_ONLY );
96 jfc.showDialog(new JLabel(), "选择");
97 File file=jfc.getSelectedFile();
98 System.out.println("文件:"+file.getAbsolutePath());
99 System.out.println(jfc.getSelectedFile().getName());
100 filename2=file.getAbsolutePath();
101 //读取文件
102 if(filename2 != null){
103 File file1 = new File(filename2);
104 StringBuffer contents = new StringBuffer();//StringBuffer类 字符串变量
105 BufferedReader reader = null;//BufferedReader字符流 BufferedInputStream字节流
106
107 try {
108 reader = new BufferedReader(new FileReader(file1));
109 String text = null;
110
111 //repeat until all lines is read
112 while ((text = reader.readLine()) != null) {
113 contents.append(text);
114 contents.append(System.getProperty("line.separator"));//读完一行增加一个换行符
115 num2++;
116 }
117 } catch (FileNotFoundException e1) {
118 e1.printStackTrace();
119 } catch (IOException e1) {
120 e1.printStackTrace();
121 } finally {
122 try {
123 if (reader != null) {
124 reader.close();
125 }
126 }catch (IOException e1) {
127 e1.printStackTrace();
128 }
129 }
130 //show file contents here
131 System.out.println("文件内容为:");
132 System.out.println(contents.toString());
133 String temp=contents.toString();
134 datalist2=temp.split("\n");
135 }
136 }
137 });
138
139 //添加主体
140 final JButton button=new JButton("请先选择文本文件");
141 button.setBounds(new Rectangle(10, 40, 280, 210));
142 pane.add(button);
143
144 ActionListener listener=new ActionListener(){
145 public void actionPerformed(ActionEvent arg){
146 button.setText(datalist[(int)(Math.random()*num)]);
147 }
148 };
149 final Timer timer = new Timer(200,listener);// 实例化Timer类
150
151 button.addActionListener(new ActionListener(){
152 public void actionPerformed(ActionEvent e){
153 if(flag){
154 timer.start();
155 flag=false;
156 }else{
157 timer.stop();
158 flag=true;
159 }
160 }
161 });
162 //添加主体2
163 final JButton buttont1=new JButton("请先选择文本文件");
164 buttont1.setBounds(new Rectangle(305, 40, 280, 210));
165 pane.add(buttont1);
166 ActionListener listener1=new ActionListener(){
167 public void actionPerformed(ActionEvent arg){
168 buttont1.setText(datalist2[(int)(Math.random()*num2)]);
169 }
170 };
171 final Timer timer1 = new Timer(200,listener1);// 实例化Timer类
172
173 buttont1.addActionListener(new ActionListener(){
174 public void actionPerformed(ActionEvent e){
175 if(flag2){
176 timer1.start();
177 flag2=false;
178 }else{
179 timer1.stop();
180 flag2=true;
181 }
182 }
183 });
184 frame.addWindowListener(new WindowAdapter(){
185 public void windowClosing(WindowEvent e){
186 System.exit(0);
187 }
188 });
189 pane.setBounds(0, 0, 600, 300);
190 pane.setVisible(true);
191 frame.setVisible(true);
192 frame.setSize(600, 300);
193 }
194 public void setUIFont()
195 {
196 Font f = new Font("宋体",Font.PLAIN,20);
197 String names[]={ "Label", "Button", "TextArea"};
198 for (String item : names) {
199 UIManager.put(item+ ".font",f);
200 }
201 }
202 }