如何在java swing中垂直对齐按钮

时间:2013-02-05 09:22:29

标签: java swing layout-manager

我希望垂直对齐JButton个实例。你能建议一个这样做的方法吗?

Jbutton jb= new Jbutton();
Jbutton jb1= new Jbutton();
JButton jb3= new JBUtton();

1 个答案:

答案 0 :(得分:2)

请参阅以下代码:

private void prepareUI() {
        this.setUndecorated(true);
        //Preparing control buttons
        JButton jb = new MyButton("-");
        jb.setActionCommand("Min");
        jb.addActionListener(this);
        jb1 = new MyButton("[]");//delete this line
        jb1.setActionCommand("Max");
        jb1.addActionListener(this);
        JButton jb2 = new MyButton("X");
        jb2.setActionCommand("Close");
        jb2.addActionListener(this);
        //Preparing panel
        JPanel buttonPanel = new JPanel();
        BoxLayout boxLayout1 = new BoxLayout(buttonPanel, BoxLayout.Y_AXIS);
        buttonPanel.setLayout(boxLayout1);
        buttonPanel.add(Box.createVerticalGlue());
        buttonPanel.add(jb);
        buttonPanel.add(jb1);
        buttonPanel.add(jb2);
        jb.setFocusable(false);
        jb1.setFocusable(false);

        jb2.setFocusable(false);

        JPanel panel=new JPanel();
        BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.LINE_AXIS);
        panel.setLayout(boxLayout);
        panel.add(Box.createHorizontalGlue())