gpt4 book ai didi

java - 如何在 JMenu 中激活 ActionEvent 以在框架上显示新面板?(基本)

转载 作者:行者123 更新时间:2023-11-29 05:55:07 26 4
gpt4 key购买 nike

我刚开始自己​​学习 swing,我有点困惑为什么我的事件在这里不起作用:

1.如果用户单击菜单栏 -> 加载,我试图从我的面板中删除所有内容,但这迫使我将面板更改为最终面板,因为我在事件中使用它!

2.我在我的事件中定义了新的面板,并定义了另外两个容器添加到那个面板,然后将它添加到主框架,但似乎没有任何反应!

如果你能找出问题所在,请帮助我。提前为困惑的代码道歉。我很感激任何提示。

public class SimpleBorder {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable()
{
public void run()
{
myFrame frame = new myFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}

class MyFrame extends JFrame {

public MyFrame()
{

setSize(500,500);
JPanel panel = new JPanel();

panel.setLayout(null);
JLabel label = new JLabel("my name is bernard...");
Color myColor = new Color(10, 150, 80);
panel.setBackground(myColor);
label.setFont(new Font("Serif", Font.PLAIN, 25));
Dimension size = label.getPreferredSize();
Insets insets = label.getInsets();
label.setBounds(85+insets.left, 120+insets.top , size.width, size.height);

panel.add(label);

JMenuBar menu = new JMenuBar();
setJMenuBar(menu);


JMenu col = new JMenu("Collection");
menu.add(col);

JMenu help = new JMenu("Help");
menu.add(help);

Action loadAction = new AbstractAction("Load")//menu item exit goes here
{
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent event)
{
JTextArea text = new JTextArea(10, 40);
JScrollPane scrol1 = new JScrollPane(text);
String[] items = {"A", "B", "C", "D"};
JList list = new JList(items);
JScrollPane scrol2 = new JScrollPane(list);
JPanel panel2 = new JPanel(new BorderLayout());
panel2 = new JPanel(new GridLayout(1, 2 ));
panel2.add(scrol1,BorderLayout.WEST);
panel2.add(scrol2,BorderLayout.EAST);
add(panel2);
}
};
JMenuItem load = new JMenuItem(loadAction);
col.add(load);
add(panel);

}

}

最佳答案

添加新面板后,在您的 JFrame 实例上调用 revalidate()/repaint():

JPanel panel2 = new JPanel(new BorderLayout());
// panel2 = new JPanel(new GridLayout(1, 2 ));//why this it will overwrite the above layout
panel2.add(scrol1,BorderLayout.WEST);
panel2.add(scrol2,BorderLayout.EAST);
add(panel2);
revalidate();
repaint();

同时在您的 JFrame 实例上调用 pack(),以便所有组件都由布局管理器隔开。正如评论中所说,不要扩展 JFrame 类,创建框架变量并在框架实例上启动您需要的所有内容,并且不要将布局设置为 null,除非您喜欢努力工作:P

或者如 mKorbel 所述,一个 CardLayout可能更多你想要的,它将允许你使用单个 JPanel 并在其他/新的之间切换:

JPanel cards;
final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";

//Where the components controlled by the CardLayout are initialized:
//Create the "cards".
JPanel card1 = new JPanel();
...
JPanel card2 = new JPanel();
...

//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);

//add card panel to frame
frame.add(cards);

//swap cards
CardLayout cl = (CardLayout)(cards.getLayout());//get layout of cards from card panel
cl.show(cards, TEXTPANEL);//show another card

关于java - 如何在 JMenu 中激活 ActionEvent 以在框架上显示新面板?(基本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12481048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com