当我触发按钮的 actionlistener 时,JList 无法刷新。我唯一可以让它刷新的方法是调用 GUI 方法,这不是那么有效,因为它实际上只是打开了第二个窗口。有没有人有任何建议让添加和删除按钮自动更新 JList?
package movieinfo;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.commons.io.FileUtils;
public class Swinggui {
public static void main(String[] args) throws IOException {
new Swinggui();
}
public Swinggui() throws IOException {
yourMovies();
gui();
Buttons();
}
JFrame maingui;
JButton enter;
JButton remove;
public JTextField movietext;
JList listofmovies;
File textfilemovie;
JScrollPane listscroll;
ListSelectionListener setSearch;
JButton add;
JTextArea movieinfo;
DefaultListModel lmodel;
public void gui() throws IOException {
maingui = new JFrame("Gui");
maingui.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL;
lmodel = new DefaultListModel();
enter = new JButton("Get Info");
c.gridx = 2;
c.gridy = 1;
maingui.add(enter, c);
add = new JButton("add");
c.gridx = 5;
c.gridy = 6;
maingui.add(add, c);
remove = new JButton("del");
c.gridx = 6;
c.gridy = 6;
maingui.add(remove, c);
movieinfo = new JTextArea(5, 20);
movieinfo.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2,
Color.red));
movietext = new JTextField(18);
c.gridx = 1;
c.gridy = 1;
maingui.add(movietext, c);
JScrollPane scrolll = new JScrollPane(movieinfo);
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 2;
maingui.add(scrolll, c);
final JLabel titlee = new JLabel("Enter movie name below!");
c.gridx = 1;
c.gridy = 0;
maingui.add(titlee, c);
c.gridx = 1;
c.gridy = 3;
maingui.add(titlee, c);
final JLabel watchlist = new JLabel("Watchlist");
c.gridx = 5;
c.gridy = 1;
maingui.add(watchlist, c);
maingui.setResizable(false);
maingui.setVisible(true);
listofmovies = new JList(FileUtils.readLines(textfilemovie).toArray());
listscroll = new JScrollPane(listofmovies);
c.gridx = 4;
c.gridy = 3;
maingui.add(listscroll, c);
movieinfo.setLineWrap(true);
movieinfo.setWrapStyleWord(true);
movieinfo.setEditable(false);
scrolll.getPreferredSize();
listofmovies.addListSelectionListener(setSearch);
maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
maingui.pack();
}
public void Buttons() {
enter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(apicall.getMovieInfo(movietext.getText()
.replaceAll(" ", "%20")));
movieinfo.setText(apicall.getMovieInfo(movietext.getText()
.replaceAll(" ", "%20")));
}
});
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
FileUtils.writeStringToFile(textfilemovie,
"\n" + movietext.getText(), true);
maingui.validate();
maingui.repaint();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
remove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Scanner filescan = null;
File textfiletemp = new File(org.apache.commons.io.FileUtils
.getUserDirectory() + "/yourmoviestemp.txt");
try {
textfiletemp.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
filescan = new Scanner(textfilemovie);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (filescan.hasNextLine()) {
String line = filescan.nextLine();
if (line != (String) listofmovies.getSelectedValue()) {
try {
FileUtils.writeStringToFile(textfiletemp, "\n"
+ line, true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
textfiletemp.renameTo(textfilemovie);
textfiletemp.delete();
}
});
}
public void yourMovies() throws IOException {
textfilemovie = new File(
org.apache.commons.io.FileUtils.getUserDirectory()
+ "/yourmovies.txt");
textfilemovie.createNewFile();
setSearch = new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
movieinfo.setText(apicall.getMovieInfo(((String) listofmovies
.getSelectedValue()).replaceAll(" ", "%20")));
}
};
}
}