嗨,我正在尝试更改JScrollPane
内容,例如
scrollPanel = new JScrollPane(new Welcome().display());
for(final JMenuItem i : items) {
i.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String txt = i.getText();
if(txt.equals("Open")) {
scrollPanel = new JScrollPane(new Open().display());
} else if(txt.equals("Save")) {
scrollPanel = new JScrollPane(new Save().display());
} else if(txt.equals("Save as")) {
scrollPanel = new JScrollPane(new SaveAs().display());
} else if(txt.equals("Close")) {
new Close().ask();
scrollPanel = new JScrollPane();
}
scrollPanel.revalidate();
scrollPanel.repaint();
}
});
}
scrollPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
menuBar.add(menu);
frame.setJMenuBar(menuBar);
frame.add(scrollPanel);
frame.pack();
frame.setSize(640, 480);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
但它不起作用。每个类 Open、Save、SaveAs 都返回JPane
内部JLabel
。前任。
public class Open {
public JPanel display() {
JPanel panel = new JPanel();
panel.add(new JLabel("Open"));
return panel;
}
}