我一直在尝试将事件处理程序附加到抽象列表模型。没有工作,所以我切换到默认列表模型。现在我需要帮助调试。为什么我有这么多错误?我正在使用windowbuilder,所以我应该重新考虑它不应该这么难。
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.LayoutManager;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
public class Window {
private JFrame frame;
/**
* Launch the application. The main method is the entry point to a Java application.
* For this assessment, you shouldn't have to add anything to this.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application. This is the constructor for this Window class.
* All of the code here will be executed as soon as a Window object is made.
*/
public Window() {
initialize();
}
/**
* Initialize the contents of the frame. This is where Window Builder
* will generate its code.
* @return
*/
public JPanel initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu File = new JMenu("File");
menuBar.add(File);
JMenuItem mntmSave = new JMenuItem("Save");
File.add(mntmSave);
JMenuItem mntmLoad = new JMenuItem("Load");
File.add(mntmLoad);
JMenuItem mntmExit = new JMenuItem("Exit");
File.add(mntmExit);
frame.getContentPane().setLayout(null);
JList Left_list = new JList();
Left_list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
}
});
Left_list.setFont(new Font("Tahoma", Font.PLAIN, 14));
Left_list.setModel(new AbstractListModel() {
String[] values = new String[] {"Case", "Motherboard", "CPU", "RAM", "GPU", "HDD", "PSU"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
Left_list.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
Left_list.setBounds(10, 11, 146, 218);
frame.getContentPane().add(Left_list);
JList Right_list = new JList();
Right_list.setModel(new AbstractListModel() {
String[] values = new String[] {};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
Right_list.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
Right_list.setBounds(278, 16, 146, 213);
frame.getContentPane().add(Right_list);
JButton btnNewButton = new JButton("Add>>");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton.setBounds(166, 91, 102, 23);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("<<Remove");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton_1.setBounds(166, 125, 102, 23);
frame.getContentPane().add(btnNewButton_1);
}
}
}