Jbutton的位置

时间:2014-05-21 03:33:47

标签: java jbutton

public class someThing {
    public static void main(String[] arg){
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        final int WID = screenSize.width;
        final int HEI = screenSize.height;
        JFrame frame = new JFrame("Menu");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(600,500));
        panel.setLayout(new GridLayout(4,4,100,100));

        JButton order1 = new JButton("Order");
        JButton order2 = new JButton("Order");
        JButton order3 = new JButton("Order");
        JButton order4 = new JButton("Order");
        JButton order5 = new JButton("Order");
        JButton order6 = new JButton("Order");
        JButton order7 = new JButton("Order");
        JButton order8 = new JButton("Order");


        panel.add(order1);
        panel.add(order2);
        panel.add(order3);
        panel.add(order4);
        panel.add(order5);
        panel.add(order6);
        panel.add(order7);
        panel.add(order8);

        frame.setLocation(WID/3, HEI/3);
        frame.setResizable(false);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

我得到的是

|-------------------------|
|[botton]         [botton]|
|                         |
|[botton]         [botton]|
|                         |
|[botton]         [botton]|
|                         |
|[botton]         [botton]|
---------------------------

我想要的是

|-------------------------|
|                         |
| [botton]      [botton]  |
|                         |
| [button]      [botton]  |
|                         |
| [button]      [botton]  |
|                         |
| [botton]      [botton]  |
|                         |
---------------------------

有谁能告诉我如何更改按钮大小并将其调整到位置? 我在GrindLayout上多次看过,但是setBound设置为北,南,对我来说不起作用。

1 个答案:

答案 0 :(得分:0)

你有几个选择。您可以使用EmptyBorder在按钮外部周围提供额外的间距,或者使用GridBagLayout可以让您更好地控制布局,例如

Layout

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout {

    public static void main(String[] args) {
        new TestLayout();
    }

    public TestLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            JButton order1 = new JButton("Order");
            JButton order2 = new JButton("Order");
            JButton order3 = new JButton("Order");
            JButton order4 = new JButton("Order");
            JButton order5 = new JButton("Order");
            JButton order6 = new JButton("Order");
            JButton order7 = new JButton("Order");
            JButton order8 = new JButton("Order");

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(8, 8, 8, 8);

            add(order1, gbc);
            gbc.gridx++;
            add(order2, gbc);
            gbc.gridx = 0;
            gbc.gridy++;
            add(order3, gbc);
            gbc.gridx++;
            add(order4, gbc);
            gbc.gridx = 0;
            gbc.gridy++;
            add(order5, gbc);
            gbc.gridx++;
            add(order6, gbc);
            gbc.gridx = 0;
            gbc.gridy++;
            add(order7, gbc);
            gbc.gridx++;
            add(order8, gbc);
        }

    }

}