-1
你好,我正在嘗試做一個程序,當即將發生的事件是關閉或項目到期時提醒你。我在JList上遇到了一些麻煩。當我嘗試和刪除事件/項目不刷新我已經嘗試list.updateUI()
,使無形的列表,然後使其可見再次嘗試刷新,但沒有奏效,如果你能幫助謝謝提前 這裏是java代碼試圖提出提醒程序幫助更新J列表
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import javax.swing.*;
public class alarm implements ActionListener {
public static JFrame window;
public static JPanel p;
@SuppressWarnings("rawtypes")
public static JList list;
public RandomAccessFile rw;
public int[] numberArray;
public String[] name;
public String[] desc;
public String[] date;
public JButton remove;
String[] listData = new String[1000000];
@SuppressWarnings({ "unchecked", "rawtypes" })
public void windowList() {
list = new JList(name);
JScrollPane pane = new JScrollPane(list);
pane.setBounds(100, 10, 400, 400);
p.add(pane);
pane.setVisible(true);
}
public void dataLoad() throws IOException {
try {
rw = new RandomAccessFile("db.txt", "rw");
int loop = 0;
int insertNumber = 0;
numberArray = new int[1000000];
name = new String[1000000];
desc = new String[1000000];
date = new String[1000000];
String row = "";
while(loop == 0) {
row = rw.readLine();
if(rw.getFilePointer() == rw.length()) {
insertNumber = 0;
loop = 1;
}else if(row.equals("/")) {
insertNumber = insertNumber + 1;
numberArray[insertNumber] = insertNumber;
name[insertNumber] = rw.readLine();
desc[insertNumber] = rw.readLine();
date[insertNumber] = rw.readLine();
}
}
while(loop == 1) {
insertNumber = insertNumber + 1;
if(name[insertNumber] == null) {
loop = 2;
}else {
System.out.println(name[insertNumber]);
}
}
System.out.println("Arrays Loaded");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void dataRemove() throws IOException {
rw = new RandomAccessFile("db.txt", "rw");
int remove = 0;
int count = 0;
int countR = 0;
String tempData = "";
String value = list.getSelectedValue().toString();
rw.seek(0);
while(remove == 0) {
tempData = rw.readLine();
System.out.println(tempData);
if(rw.getFilePointer() == rw.length()) {
System.out.println("Not found...");
remove = 1;
}else if(tempData.equals(value)) {
countR = count;
count = 0;
remove = 1;
}
else if(tempData.equals("/")) {
count = count + 1;
}
}
rw.seek(0);
System.out.println("Starting loop 2");
while(remove == 1) {
tempData = rw.readLine();
if(tempData.equals("/")) {
count = count + 1;
}
if(count == countR) {
rw.seek(rw.getFilePointer()-2);
rw.writeBytes("/");
remove = 2;
}
}
numberArray = new int[1000000];
name = new String[1000000];
desc = new String[1000000];
date = new String[1000000];
dataLoad();
}
alarm() throws IOException {
dataLoad();
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.setSize(600, 600);
p = new JPanel();
p.setLayout(null);
window.add(p);
remove = new JButton("Remove");
remove.addActionListener(this);
remove.setBounds(100, 415, 100, 30);
p.add(remove);
remove.setVisible(true);
windowList();
}
public static void main(String[] args) throws IOException {
new alarm();
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == remove) {
try {
dataRemove();
list.setVisible(false);
windowList();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
開始由具有看看[如何使用列表](http://docs.oracle.com/javase/tutorial/uiswing/components/list.html)。然後看看['DefaultListModel'](http://docs.oracle.com/javase/7/docs/api/javax/swing/DefaultListModel.html),它有一個很好用的'remove'方法 – MadProgrammer