如何在JButtons之间创建间距?

时间:2015-04-23 04:16:57

标签: java whitespace gridbaglayout

我正在尝试为一个简单的调度程序创建一个小主菜单,但我在按钮之间有一些空间有点困难。他们只是紧挨着彼此,我希望每个按钮之间有一个间隙。

我曾尝试使用weightx,weighty命令,但似乎没有任何改变。 我希望在GUI的边缘和按钮的两侧之间以及每个按钮之间留出一些空白。

这是截图

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class Scheduler {

JButton VDay, VWeek, Task, Exit;
JFrame wframe, dframe, tframe;

JLabel head;




public void CreateFrame() {
    JFrame frame = new JFrame("Main Menu");     

    ButtonListener btnlst = new ButtonListener();
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    c.weightx = 1;
    c.weighty = .25;
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    head = new JLabel("The Plain Scheduler");
    panel.add(head, c);
    c.weightx = 1;
    c.weighty = .25;
    c.gridx = 0;
    c.gridy = 1;
    c.gridheight = 2;
    c.gridwidth = 3;
    VDay = new JButton("View Day");
    panel.add(VDay, c);
    c.weightx = 1;
    c.weighty = .25;
    c.gridx = 0;
    c.gridy = 3;
    c.gridheight = 2;
    c.gridwidth = 3;
    VWeek = new JButton("View Week");
    panel.add(VWeek,c);
    c.weightx = 1;
    c.weighty = .25;
    c.gridx = 0;
    c.gridy = 5;
    c.gridheight = 2;
    c.gridwidth = 3;
    Task = new JButton("Assign/Edit Tasks");
    panel.add(Task, c);
    c.weightx = 1;
    c.weighty = .25;
    c.gridx = 0;
    c.gridy = 7;
    c.gridheight = 1;
    c.gridwidth = 2;
    Exit = new JButton("Exit");
    panel.add(Exit, c);

    VDay.addActionListener(btnlst);
    VWeek.addActionListener(btnlst);
    Task.addActionListener(btnlst);
    Exit.addActionListener(btnlst);

    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
    Scheduler scheduler = new Scheduler();
    scheduler.CreateFrame();
    }
}

1 个答案:

答案 0 :(得分:2)

您可以使用GridBagConstraints#insets来定义添加到单元格的间距/填充量,例如......

Buttons

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.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Scheduler {

    JButton VDay, VWeek, Task, Exit;
    JFrame wframe, dframe, tframe;

    JLabel head;

    public void CreateFrame() {
        JFrame frame = new JFrame("Main Menu");

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.weightx = 1;
        c.weighty = .25;
        c.insets = new Insets(5, 0, 5, 0);
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.BOTH;
        head = new JLabel("The Plain Scheduler");
        panel.add(head, c);
        VDay = new JButton("View Day");
        panel.add(VDay, c);
        VWeek = new JButton("View Week");
        panel.add(VWeek, c);
        Task = new JButton("Assign/Edit Tasks");
        panel.add(Task, c);
        Exit = new JButton("Exit");
        panel.add(Exit, c);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                Scheduler scheduler = new Scheduler();
                scheduler.CreateFrame();
            }
        });
    }
}