java - 将按钮放置在组合框下方

标签 java swing layout-manager

如何使按钮出现在此处的组合框下方?换句话说,按钮 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;
    }
}

enter image description here

最佳答案

使用 BoxLayout 作为面板,而不是默认布局 FlowLayout

查看此链接:How to Use BoxLayout 。对于您的情况,BoxLayout.Y_AXIT 会有所帮助。

例如:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

关于java - 将按钮放置在组合框下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18806176/

相关文章:

java - 在Android中为不同的功能使用不同的包是正确的编码约定吗?

java - GridLayout不通过按钮点击添加组件?

java - 无法添加到布局约束必须是 gridbagconstraint

java - 在java中突出显示单词

java - 拖动后执行意外操作

java - [Miglayout]调整容器的大小以适合所包含的所有组件的总大小,带有插图和间隙

java - 如何用Java删除Excel工作表的内容?

java - 代码重复 vs 类型转换 vs?

java - Golang 中的动态加载?

java - 使用自定义 TableModel 使 isCellEditable 在按钮单击时对特定行为真