0

如何使按钮出现在此处的组合框下方?换句话说,在组合框下方有按钮 1 和按钮 2?

public class GUI extends JFrame implements ListSelectionListener, ActionListener {

    private JPanel myPanelA;
    private JSplitPane itemPane;

    public static void startWindowsGui ( ) { 

        SwingUtilities.invokeLater ( new Runnable ( ) {
            public void run ( ) {

                GUI gui = new GUI ( );


                gui.setVisible ( true );
            }
        } );
    }

    public GUI() {

        // Set the layout to a grid
        setLayout ( new BorderLayout ( 5, 5 ) );


        setTitle ( "UI " );
        setSize ( 800, 600 );
        setDefaultCloseOperation ( EXIT_ON_CLOSE );
        setBackground ( new Color ( 15, 255, 10 ) );


        addComponents ( );
    }

    private void addComponents ( ) {

        JSplitPane mainPane = new JSplitPane ( JSplitPane.HORIZONTAL_SPLIT );
        itemPane = new JSplitPane ( JSplitPane.VERTICAL_SPLIT );

        mainPane.add ( PanelA ( ), JSplitPane.LEFT );
        mainPane.add ( itemPane, JSplitPane.RIGHT );
        mainPane.setOneTouchExpandable ( true );

        itemPane.setOpaque(true);
        itemPane.setBackground(new Color(0xffffffc0));
        BufferedImage myPicture = null;
        try {
            myPicture = ImageIO.read(new File("C:/Users/Desktop/image.jpg"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JLabel picLabel = new JLabel(new ImageIcon(myPicture));
        //add(picLabel);
        itemPane.add(picLabel);

        add ( mainPane, BorderLayout.CENTER );
    }


    private JPanel PanelA ( ) {

        myPanelA = new JPanel ( );


        myPanelA.setLayout ( new BorderLayout ( 0, 0 ) );


        myPanelA.add ( buttonPanel ( ), BorderLayout.NORTH );

        myPanelA.setBorder ( new EmptyBorder ( 0, 0, 0, 0 ) );
        myPanelA.setBackground(new Color(0,0,0));

        return myPanelA;
    }

    @Override
    public void actionPerformed(ActionEvent e) {


    }

    @Override
    public void valueChanged(ListSelectionEvent arg0) {


    }


    private JPanel buttonPanel ( ) {
        // Create the panel 
        JPanel addButton = new JPanel ( );

        JPanel cards; //a panel that uses CardLayout
         String BUTTONPANEL = "Card with JButtons";
         String TEXTPANEL = "Card with JTextField";

        JPanel comboBoxPane = new JPanel(); //use FlowLayout
        String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
        JComboBox cb = new JComboBox(comboBoxItems);
        cb.setEditable(false);
            //cb.addItemListener(this);
            comboBoxPane.add(cb);

            //Create the "cards".
            JPanel card1 = new JPanel();
            card1.add(new JButton("Button 1"));
            card1.add(new JButton("Button 2"));

            JPanel card2 = new JPanel();
            card2.add(new JTextField("TextField", 10));

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

            addButton.add(comboBoxPane, BorderLayout.PAGE_START);
            addButton.add(cards, BorderLayout.CENTER);

        return addButton;
    }
}

在此处输入图像描述

4

2 回答 2

2

用于BoxLayout面板而不是默认布局,即FlowLayout.

请参阅此链接:如何使用 BoxLayout。在你的情况下,BoxLayout.Y_AXIT会有所帮助。

例如:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
于 2013-09-14T20:42:20.443 回答
1

你需要安排你的布局,GridLayout非常好。

你需要先GridLayout(2, 1)

第一行将分配给组合框,

第二行是带有GridLayout(1, 2)两个按钮的面板。

setLayout(new GridLayout(2, 1));
add(comboBox);
JPanel inner = new JPanel();
inner.setLayout(new GridLayout(1, 2));
add(inner);
inner.add(button1);
inner.add(button2);
于 2013-09-14T20:43:13.223 回答