【问题标题】:How to activate ActionEvent inside JMenu to show new panel on the frame?(Basic)如何在 JMenu 中激活 ActionEvent 以在框架上显示新面板?(基本)
【发布时间】:2012-09-18 16:20:10
【问题描述】:

我刚开始自学摇摆,我有点困惑为什么我的活动在这里不起作用:

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);

    }

}

【问题讨论】:

  • 三个提示:1) 尊重 Java 编码约定(例如类名以大写字母开头) 2) 学习如何使用 LayoutManagers 并远离 null 布局。此站点上的 Swing wiki 包含指向教程的链接 3) 不要扩展 J* 类,而是使用可用的 API 来定制它们。在这种情况下,不需要myFrame

标签: java swing jframe jpanel jmenu


【解决方案1】:

添加新面板后,在您的 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();

同时调用pack() JFrame 实例,以便所有组件都由布局管理器隔开。正如评论中所说,不要扩展 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

【讨论】:

  • @mKorbel 哦 +1,是的,谢谢你提醒卡片布局会更好。
  • @trashgod 我必须承认我很困惑,所以我应该使用什么?设置边界?
  • @DavidKroukamp:我不明白为什么; CardLayout 实现LayoutManager 以找到容纳容器中最大面板的preferredLayoutSize(),因此pack() 应该足够了。抱歉,如果我忽略了什么。
猜你喜欢
  • 1970-01-01
  • 2012-04-06
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
相关资源
最近更新 更多