我一直在开发一个应用程序,您可以在其中设置包含项目的类别列表,然后将其制成文本文件。但是我为它制作的 GUI 并不能完全正常工作。我认为错误位于名为 Cat(或“添加类别”)的 JMenuItem 中,或者位于我显示类别的方式中(Update() 方法)。它应该要求一个名称,创建一个以此命名的类别并将其显示在 JScrollPane 中,但什么也没有出现。这是代码:
public class GUIBuilder {
public JFrame frame;
public JPanel LeftPanel;
public JPanel RightPanel;
public JScrollPane scroll;
public JMenuBar bar;
public JMenu File;
public JMenu Add;
Inventory inv;
public void go() {
frame = new JFrame();
scroll = new JScrollPane();
bar = new JMenuBar();
File = new JMenu("File");
Add = new JMenu("Add...");
bar.add(File);
bar.add(Add);
JMenuItem Save = new JMenuItem(new AbstractAction("Save") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent arg0) {
}
});
JMenuItem Load = new JMenuItem(new AbstractAction("Load") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent arg0) {
}
});
JMenuItem Generate = new JMenuItem(new AbstractAction("Generate Text File") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent arg0) {
}
});
File.add(Save);
File.add(Load);
File.add(Generate);
JMenuItem Cat = new JMenuItem(new AbstractAction("Add Category") {
private static final long serialVersionUID = 1L;
JFrame Cat;
public void actionPerformed(ActionEvent arg0) {
Cat = new JFrame("Add Category");
final JTextField name = new JTextField(15);
JButton Submit = new JButton(new AbstractAction("Submit") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent arg0) {
String n = name .getText();
if (n != null) {
inv.addCategory(new Category(n));
pullThePlug();
GUIBuilder.this.Update();
}
}
});
Cat.setLayout(new BorderLayout());
Cat.add(name, BorderLayout.CENTER);
Cat.add(Submit, BorderLayout.SOUTH);
Cat.setSize(250, 150);
Cat.setVisible(true);
}
public void pullThePlug() {
WindowEvent wev = new WindowEvent(Cat, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
}
});
JMenuItem item = new JMenuItem(new AbstractAction("Add Item") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent arg0) {
}
});
Add.add(Cat);
Add.add(item);
frame.setJMenuBar(bar);
frame.setSize(500, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(scroll);
inv = new Inventory();
}
public void Update() {
for (int i = 0; i < inv.categories.size(); i++) {
Category cat = inv.categories.get(i);
JPanel p = new JPanel();
JTextPane name = new JTextPane();
name.setText(cat.getName());
scroll.add(p);
}
}
}
提前致谢 ;)