1

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

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

    }

}
4

1 回答 1

3

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

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
于 2012-09-18T16:43:45.757 回答