您好 ,我正在尝试制作一个程序,在即将发生的事件或项目到期时提醒您。我在使用JList时遇到了一些麻烦。当我尝试删除一个事件/项目时它没有刷新我已经尝试list.updateUI()
并使列表不可见然后再次显示它以尝试刷新它但是如果你可以帮助 提前谢谢
这是java代码
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();
}
}
}
}
答案 0 :(得分:3)
你的ListModel在哪里?您需要使用ListModel(例如DefaultListModel<String>
)才能轻松地在显示的JList<String>
中添加和删除元素。您可以通过构造函数或其setModel(...)
方法将模型传递给JList。阅读JList tutorial,因为它在那里都有很好的解释。
其他问题:
setBounds()
似乎是Swing新手,比如创建复杂GUI的最简单和最好的方法,但是你创建的Swing GUI越多,你在使用它们时会遇到更严重的困难。 。当GUI调整大小时,他们不会调整组件的大小,他们是增强或维护的皇室女巫,当他们放置在滚动窗格中时,他们完全失败,当他们在所有平台或屏幕分辨率不同时看起来很糟糕原来的。