Java可以使用GridBagLayout来组织CardLayout中使用的JPanel

时间:2012-07-16 20:15:22

标签: java swing awt gridbaglayout cardlayout

我是布局的新手,现在我正在使用的是带有两张不同卡的CardLayout,这些卡目前有一个背景图像和一个按钮。如下图所示,按钮位于屏幕顶部,我想将它放在靠近底部的位置。我的理解是GridBagLayout将是实现这一目标的最佳方式。所以我的第一个问题是,这是真的吗?而且,如果我想要放置GridBagLayout的JPanel是CardLayout中的卡,是否可以这样做。我希望将更多其他对象放入GridBagLayout(如果这是最好的方法,如果可能的话),当我进一步进入项目时,所以任何关于正确方向去哪里的建议都将受到高度赞赏。< / p>

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Cards implements ActionListener {    
private JPanel cards;
private JButton button1;
private JButton button2;
private Image backgroundImage;

public void addComponentToPane(Container pane) throws IOException {
    try {           
        backgroundImage = ImageIO.read(getClass().getResource("resources/background.jpg"));         
    } catch (IOException ex) {
        ex.printStackTrace(System.err);
    }

    //create cards
    JPanel card1 = new JPanel()
    {
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawImage(backgroundImage, 0, 0, null);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(
                    backgroundImage.getWidth(null),
                    backgroundImage.getHeight(null));
        }
    };
    JPanel card2 = new JPanel()
    {
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawImage(backgroundImage, 0, 0, null);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(
                    backgroundImage.getWidth(null),
                    backgroundImage.getHeight(null));
        }
    };
    //create buttons
    button1 = new JButton("Button 1");
    button2 = new JButton("Button 2");
    button1.addActionListener(this);
    button2.addActionListener(this);
    //add buttons to cards
    card1.add(button1);        
    card2.add(button2);
    //create panel that contains cards
    cards = new JPanel(new CardLayout());
    cards.add(card1, "Card 1");
    cards.add(card2, "Card 2");
    pane.add(cards, BorderLayout.SOUTH);        
}

public void itemStateChanged(ItemEvent evt) {
    CardLayout cl = (CardLayout)(cards.getLayout());
    cl.show(cards, (String)evt.getItem());
}

public static void createAndShowGUI() throws IOException  {
    //create and setup window
    JFrame frame = new JFrame("Frame"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    //create and setup content pane
    Cards main = new Cards();
    main.addComponentToPane(frame.getContentPane());        
    //display window
    frame.pack();       
    //frame.setResizable(false);
    frame.setVisible(true);
}

public void actionPerformed(ActionEvent ae) {
    if (ae.getSource() == button1) {
        CardLayout cl = (CardLayout) (cards.getLayout());
        cl.show(cards, "Card 2");     
    } else if (ae.getSource() == button2) {
        CardLayout cl = (CardLayout) (cards.getLayout());
        cl.show(cards, "Card 1");
    }        
}            

public static void main(String[] args) {
    //set look and feel
    try {
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    } catch (InstantiationException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }                

    //schedule job for the event dispatch thread creating and showing GUI        
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {                   
                    try {
                        createAndShowGUI();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                
        }
    });     
}   
}

2 个答案:

答案 0 :(得分:3)

GridBagLayout会做你想做的事,是的。它是最灵活的(也是最复杂的)LayoutManager之一。JPanel可用。它是否是“最佳”方式取决于您想要添加到面板的其他控件,是否要让它们调整大小,以及在调整父JPanel大小时要发生的事情,和其他因素。

re:使用不同的布局嵌套JPanels,是的。在GridBagLayoutCardLayout JPanel BorderLayout内部{{1}} {{1}}并{{1}}完全没问题。

答案 1 :(得分:1)

结帐BorderLayout。它有5个不同的区域,您可以放置​​其他容器:左,右,顶部,中心和底部。

http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html