Java 如何在JMenu中激活ActionEvent以在框架上显示新面板?(基本)
我刚开始自学挥杆,我有点困惑为什么我的活动在这里不起作用: 1.如果用户单击菜单栏->加载,我试图删除面板中的所有内容,但它迫使我将面板更改为最终面板,因为我在事件中使用它 2.我已经在我的事件中定义了新面板,并定义了另外两个容器添加到该面板,然后将其添加到主框架,但似乎什么都没有发生 如果你能找出问题所在,请帮助我。 对于混乱的代码,请提前道歉。 谢谢你给我任何提示Java 如何在JMenu中激活ActionEvent以在框架上显示新面板?(基本),java,swing,jframe,jpanel,jmenu,Java,Swing,Jframe,Jpanel,Jmenu,我刚开始自学挥杆,我有点困惑为什么我的活动在这里不起作用: 1.如果用户单击菜单栏->加载,我试图删除面板中的所有内容,但它迫使我将面板更改为最终面板,因为我在事件中使用它 2.我已经在我的事件中定义了新面板,并定义了另外两个容器添加到该面板,然后将其添加到主框架,但似乎什么都没有发生 如果你能找出问题所在,请帮助我。 对于混乱的代码,请提前道歉。 谢谢你给我任何提示 public class SimpleBorder { public static void main(String[
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()
,这样所有组件都由layoutmanager隔开。正如在评论中所说的,不要扩展JFrame类,创建frame的变量并启动frames实例上所需的所有内容,不要将布局设置为null,除非您喜欢艰苦的工作:P
或者,正如mKorbel所提到的,a可能更符合您的需要,它将允许您使用单个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
三个技巧:1)遵守Java编码约定(例如,类名以大写字母开头)2)学习如何使用LayoutManager并远离空布局。该站点上的Swing wiki包含到教程的链接3)不扩展J*类,而是使用可用的API自定义它们。在这种情况下,不需要使用
myFrame
class@mKorbel哦+1,是的,谢谢提醒你,卡片布局会更好。?@trashgood我必须承认我很困惑,所以我应该用什么?挫折?@DavidKroukamp:我不明白为什么CardLayout
实现LayoutManager
以查找容纳容器中最大面板的preferredLayoutSize()。对不起,如果我忽略了什么。