我正在为需要创建JComboBox的类进行分配,每个选项都会打开一个新窗口,您可以在这些新窗口中执行任何操作。请记住我对图形用户界面非常陌生,一般来说对于Java来说都是新手,以防我的问题很愚蠢。从JFrame中清除JPanel
我有一个问题,一个问题...
我的问题:
当用户选择“黑客帝国”选项在新窗口中报价和两个按钮弹出。现在我有两个JPanel(panel和panel2)面板将报价添加到NORTH位置,然后panel2使用BorderLayout将两个按钮添加到CENTER位置。我的问题是我正确地做到了这一点...我可以使用面板来添加报价和按钮,还是有必要为单独的项目添加到JFrame中创建单独的面板?当我将它们添加到同一个面板时,当我运行该程序时,报价不在窗口中。
panel.add(matrixQuote);
newFrame.add(panel, BorderLayout.NORTH);
这就是我怎么过的,当它没有显示出来^^^
我得到这个问题具有清热固定的JFrame
我想一个ActionListener添加到bluePill按钮,而不是打开另一个新窗口,我想我可以在按下按钮时清除现有窗口中的所有内容,然后在窗口上显示新的内容。我能找到的唯一信息就是我如何在下面的actionPerformed方法中找到它。我会在下面直接发布我正在谈论的内容,然后在下面列出所有我的代码以防万一。
所有我的代码...
public class MultiForm extends JFrame{
private JComboBox menu;
private JButton bluePill;
private JButton redPill;
private JLabel matrixQuote;
private int matrixSelection;
private JFrame newFrame;
private JPanel panel;
private JPanel panel2;
private static String[] fileName = {"", "The Matrix", "Another Option"};
public MultiForm() {
super("Multi Form Program");
setLayout(new FlowLayout());
menu = new JComboBox(fileName);
add(menu);
TheHandler handler = new TheHandler();
menu.addItemListener(handler);
}
public void matrixPanel() {
TheHandler handler = new TheHandler();
//Create a new window when "The Matrix" is clicked in the JCB
newFrame = new JFrame();
panel = new JPanel();
panel2 = new JPanel();
newFrame.setLayout(new FlowLayout());
newFrame.setSize(500, 300);
newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);
matrixQuote = new JLabel("<html>After this, there is no turning back. "
+ "<br>You take the blue pill—the story ends, you wake up "
+ "<br>in your bed and believe whatever you want to believe."
+ "<br>You take the red pill—you stay in Wonderland, and I show"
+ "<br>you how deep the rabbit hole goes. Remember: all I'm "
+ "<br>offering is the truth. Nothing more.</html>");
panel2.add(matrixQuote);
newFrame.add(panel2, BorderLayout.NORTH);
//Blue pill button and picture.
Icon bp = new ImageIcon(getClass().getResource("Blue Pill.png"));
bluePill = new JButton("Blue Pill", bp);
panel2.add(bluePill);
bluePill.addActionListener(handler);
//Red pill button and picture
Icon rp = new ImageIcon(getClass().getResource("Red Pill.png"));
redPill = new JButton("Red Pill", rp);
panel2.add(redPill);
newFrame.add(panel2, BorderLayout.CENTER);
newFrame.setVisible(true);
}
private class TheHandler implements ItemListener, ActionListener{
public void itemStateChanged(ItemEvent IE) {
//listen for an item to be selected.
if(IE.getStateChange() == ItemEvent.SELECTED) {
Object selection = menu.getSelectedItem();
if("The Matrix".equals(selection)) {
matrixPanel();
}
else if("Another Option".equals(selection)) {
}
}
}
public void actionPerformed(ActionEvent AE) {
if(AE.getSource() == bluePill) {
newFrame.remove(panel);
newFrame.remove(panel2);
newFrame.repaint();
}
}
}
//MAIN
public static void main(String[] args) {
MultiForm go = new MultiForm();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(400, 200);
go.setVisible(true);
}
}
你是对的,我会修复它 – roeygol
嗯,我有点在我的代码中,我从newFrame移除面板相反... newFrame.remove(panel);它没有做任何事情 – NoobCoderChick
这就是我的代码,但点击按钮时没有任何东西被删除。或者,问题出在我如何编写Action? – NoobCoderChick