```java
//导入
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
//创建主类
public class Election {
//主方法
public void main(String[] args) throws InterruptedException {
//设置编码
//创建初始变量
System.setProperty("file.encoding", "GBK");
String[] m = ReadFile("name.txt");
ArrayList<String> list = new ArrayList<>();
for(int x = 0;x < m.length;x++) {
list.add(m[x]);
}
list.remove(null);
final String[] l = list.toArray(new String[list.size()]);
//开始创建窗口
JFrame.setDefaultLookAndFeelDecorated(false);
JFrame k = new JFrame();
//初始化颜色
Color b = new Color(245,245,245);
Color bg = new Color(255,255,255);
k.getContentPane().setBackground(bg);
k.setLayout(null);
k.setSize(1000,800);
k.setResizable(false);
k.setTitle("Election");
k.setLocationRelativeTo(null);
k.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
k.setVisible(true);
//结果输出
JLabel Result = new JLabel();
Result.setBounds(430, 0, 450, 200);
Font c = new Font("楷体",Font.PLAIN,50);
Result.setFont(c);
Result.setVisible(true);
k.add(Result);
ActionListener bt = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Random r = new Random();
//int index = r.nextInt(m.length);
int i = r.nextInt(l.length);
Result.setText(l[i]);
}
};
Timer timer = new Timer(100,bt);
Thread.sleep(500);
// Thread thread = new Thread(
// new Runnable() {
// @Override
// public void run() {
// try {
// Thread.sleep(1000);
// Random r = new Random();
// int index = r.nextInt(m.length);
// Result.setText(m[index]);
// } catch(Exception e) {
// e.printStackTrace();
// }
// }
// }
// );
//创建按钮
JButton bt1 = new JButton("start");
k.add(bt1);
bt1.setBounds(300,600,100,50);
bt1.setBackground(b);
bt1.setContentAreaFilled(true);
bt1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if("start".equals(e.getActionCommand())) {
timer.start();
}
}
});
bt1.repaint();
bt1.setVisible(true);
JButton bt2 = new JButton("stop");
k.add(bt2);
bt2.setBounds(600,600,100,50);
bt2.setBackground(b);
bt2.setContentAreaFilled(true);
bt2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if("stop".equals(e.getActionCommand())) {
timer.stop();
}
}
});
bt2.repaint();
bt2.setVisible(true);
}
//创建读取文件方法
public String[] ReadFile(String i) {
StringBuilder c = new StringBuilder();
try {
FileInputStream f = new FileInputStream(i);
BufferedReader bf;
String s;
try (InputStreamReader n = new InputStreamReader(f,"GBK")) {
bf = new BufferedReader(n);
while((s = bf.readLine()) != null) {
s = bf.readLine();
c.append(s).append(System.lineSeparator());
}
bf.close();
f.close();
}
} catch (IOException e) {
e.printStackTrace();
}
String[] Result = c.toString().split("\n");
return Result;
}
}
请问为什么m这个string数组里面的null值删不掉啊,每一次输出都有null,而且这个null值好像被识别成了一个字符串而不是单纯的null,望各位博主帮忙看看,感谢!