【问题标题】:How to align fields in GridbagLayout如何对齐 GridbagLayout 中的字段
【发布时间】:2014-09-11 20:22:02
【问题描述】:

我需要调整时间,使它看起来像这样:

到目前为止,我已经在这段代码中实现了它:(这是我的代码的简短版本)

package cardlayoutalignment;

import java.awt.EventQueue;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class gridbaglayoutdemo {

    JFrame Card = new JFrame();

FlowLayout flow = new FlowLayout(FlowLayout.RIGHT,2,2);
Border etch = BorderFactory.createEtchedBorder(Color.white,Color.gray);
Border margin = new EmptyBorder(10,10,10,10);

public static GridBagLayout grid = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
final static boolean shouldFill = true;

JPanel container = new JPanel();
JPanel divider = new JPanel();
JPanel bodypanel = new JPanel();
final JPanel buttonpanel = new JPanel();
JPanel panel_1 = new JPanel();
    JPanel panel_4 = new JPanel();
CardLayout cl = new CardLayout();

public gridbaglayoutdemo(){
        Card.setVisible(true);
    Card.setSize(605,333);
    Card.setTitle("Tank Delivery");
    //Card.setResizable(false);

    final Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();      
    int x=(int)((dimension.getWidth() - Card.getWidth())/2);
    int y=(int)((dimension.getHeight() - Card.getHeight())/2);

    Card.setLocation(x, y);
    Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


    bodypanel.setLayout(new BorderLayout());

    divider.setLayout(new BorderLayout());

    container.setLayout(cl);
    cl.show(container, "1");


    //JOptionPane.showMessageDialog(null, "Memory is full. Finish 1 transaction in order to use Memory again");


    panel_1.setLayout(grid);

    JLabel label_1 = new JLabel("Enter Tank:");
    label_1.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(10,10,0,0);
    panel_1.add(label_1, c);

    JComboBox box_1 = new JComboBox();
    box_1.setPreferredSize(new Dimension(200,30));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 20;
    c.insets = new Insets(10,10,0,0);
    panel_1.add(box_1,c);

    JLabel label = new JLabel("");
    label.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 1;
    c.gridx = 0;
    c.gridy = 30;
    c.insets = new Insets(10,0,0,0);
    panel_1.add(label, c);


    panel_4.setLayout(grid);

    JLabel label_2 = new JLabel("Enter Date:");
    label_2.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(10,10,0,0);
    panel_4.add(label_2,c);

    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    df.setLenient(false);

    JFormattedTextField text_2 = new JFormattedTextField(df);
    text_2.setValue(new Date());
    text_2.setPreferredSize(new Dimension(200,30));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 5;
    c.insets = new Insets(10,10,0,0);
    panel_4.add(text_2,c);

    JLabel label_3 = new JLabel("Enter Date:");
    label_3.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 10;
    c.insets = new Insets(10,10,0,10);
    panel_4.add(label_3,c);

    JLabel text_3 = new JLabel("");
    text_3.setPreferredSize(new Dimension(0,30));
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 15;
    c.insets = new Insets(0,0,0,0);
    panel_4.add(text_3,c);

    String time3 = new java.text.SimpleDateFormat("HH").format(new java.util.Date() );
    JFormattedTextField text_33 = new JFormattedTextField();
    text_33.setValue(time3);
    //text_33.setPreferredSize(new Dimension(30,30));
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 2;
    c.gridy = 15;
    c.insets = new Insets(0,0,0,0);
    panel_4.add(text_33,c);

    JLabel label_4 = new JLabel(":");
    label_4.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 3;
    c.gridy = 15;
    c.insets = new Insets(0,0,0,0);
    panel_4.add(label_4,c);

    String time2 = new java.text.SimpleDateFormat("mm").format(new java.util.Date() );
    JFormattedTextField text_4 = new JFormattedTextField();
    text_4.setValue(time2);
    //text_4.setPreferredSize(new Dimension(30,30));
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 5;
    c.gridy = 15;
    c.insets = new Insets(0,0,0,0);
    panel_4.add(text_4,c);

    JButton btnup = new JButton("UP");
    btnup.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 10;
    c.gridy = 15;
    c.insets = new Insets(0,0,0,0);
    panel_4.add(btnup, c);

    JButton btndown = new JButton("DOWN");
    btndown.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.weighty = 0;
    c.gridx = 10;
    c.gridy = 20;
    c.insets = new Insets(0,0,0,0);
    panel_4.add(btndown, c);


    JLabel label14 = new JLabel("");
    label14.setFont(new Font("Arial", Font.PLAIN, 18));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0;
    c.weighty = 1;
    c.gridx = 0;
    c.gridy = 30;
    c.insets = new Insets(10,0,0,0);
    panel_4.add(label14, c);


    buttonpanel.setLayout(new FlowLayout(SwingConstants.RIGHT));
    buttonpanel.setBorder(new EmptyBorder(0,10,0,0));

    JButton btnBack = new JButton("< Back");
    btnBack.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        cl.previous(container);
        buttonpanel.repaint();
        }   
    });
    btnBack.setFont(new Font("Arial", Font.PLAIN, 20));
    btnBack.setFocusable(false);
    btnBack.setFocusTraversalKeysEnabled(false);
    btnBack.setFocusPainted(false);
    btnBack.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    btnBack.setPreferredSize(new Dimension(110, 40));
    btnBack.setBackground(new Color(224,223,227));
    buttonpanel.add(btnBack);

    JButton btnNext = new JButton("Next >");
    btnNext.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cl.next(container);
            buttonpanel.repaint();
        }   
    });
    btnNext.setFont(new Font("Arial", Font.PLAIN, 20));
    btnNext.setFocusable(false);
    btnNext.setFocusTraversalKeysEnabled(false);
    btnNext.setFocusPainted(false);
    btnNext.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    btnNext.setPreferredSize(new Dimension(110, 40));
    btnNext.setBackground(new Color(224,223,227));
    buttonpanel.add(btnNext);

    JButton btnCancel = new JButton("Cancel");
    btnCancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    btnCancel.setFont(new Font("Arial", Font.PLAIN, 20));
    btnCancel.setFocusable(false);
    btnCancel.setFocusTraversalKeysEnabled(false);
    btnCancel.setFocusPainted(false);
    btnCancel.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    btnCancel.setPreferredSize(new Dimension(110, 40));
    btnCancel.setBackground(new Color(224,223,227));
    buttonpanel.add(btnCancel);



    divider.add(container, BorderLayout.NORTH);     
        container.add(panel_1, "1");
        //container.add(p2.panel_2, "2");
        //container.add(p3.panel_3, "3");
        container.add(panel_4, "4");
    divider.add(buttonpanel, BorderLayout.SOUTH);



    JPanel numberpanel = new JPanel();
    numberpanel.setPreferredSize(new Dimension(221,0));
    numberpanel.setBorder(new EmptyBorder(10,0,0,10));
    numberpanel.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.gray));
    numberpanel.setLayout(flow);

    JButton button_7 = new JButton("7");
    button_7.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_7.setFont(new Font("Arial", Font.PLAIN, 30));
    button_7.setFocusable(false);
    button_7.setFocusTraversalKeysEnabled(false);
    button_7.setFocusPainted(false);
    button_7.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_7.setPreferredSize(new Dimension(70, 70));
    button_7.setBackground(new Color(224,223,227));
    numberpanel.add(button_7);

    JButton button_8 = new JButton("8");
    button_8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_8.setFont(new Font("Arial", Font.PLAIN, 30));
    button_8.setFocusable(false);
    button_8.setFocusTraversalKeysEnabled(false);
    button_8.setFocusPainted(false);
    button_8.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_8.setPreferredSize(new Dimension(70, 70));
    button_8.setBackground(new Color(224,223,227));
    numberpanel.add(button_8);

    JButton button_9 = new JButton("9");
    button_9.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_9.setFont(new Font("Arial", Font.PLAIN, 30));
    button_9.setFocusable(false);
    button_9.setFocusTraversalKeysEnabled(false);
    button_9.setFocusPainted(false);
    button_9.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_9.setPreferredSize(new Dimension(70, 70));
    button_9.setBackground(new Color(224,223,227));
    numberpanel.add(button_9);

    JButton button_4 = new JButton("4");
    button_4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_4.setFont(new Font("Arial", Font.PLAIN, 30));
    button_4.setFocusable(false);
    button_4.setFocusTraversalKeysEnabled(false);
    button_4.setFocusPainted(false);
    button_4.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_4.setPreferredSize(new Dimension(70, 70));
    button_4.setBackground(new Color(224,223,227));
    numberpanel.add(button_4);

    JButton button_5 = new JButton("5");
    button_5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_5.setFont(new Font("Arial", Font.PLAIN, 30));
    button_5.setFocusable(false);
    button_5.setFocusTraversalKeysEnabled(false);
    button_5.setFocusPainted(false);
    button_5.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_5.setPreferredSize(new Dimension(70, 70));
    button_5.setBackground(new Color(224,223,227));
    numberpanel.add(button_5);

    JButton button_6 = new JButton("6");
    button_6.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_6.setFont(new Font("Arial", Font.PLAIN, 30));
    button_6.setFocusable(false);
    button_6.setFocusTraversalKeysEnabled(false);
    button_6.setFocusPainted(false);
    button_6.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_6.setPreferredSize(new Dimension(70, 70));
    button_6.setBackground(new Color(224,223,227));
    numberpanel.add(button_6);

    JButton button_1 = new JButton("1");
    button_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_1.setFont(new Font("Arial", Font.PLAIN, 30));
    button_1.setFocusable(false);
    button_1.setFocusTraversalKeysEnabled(false);
    button_1.setFocusPainted(false);
    button_1.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_1.setPreferredSize(new Dimension(70, 70));
    button_1.setBackground(new Color(224,223,227));
    numberpanel.add(button_1);

    JButton button_2 = new JButton("2");
    button_2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_2.setFont(new Font("Arial", Font.PLAIN, 30));
    button_2.setFocusable(false);
    button_2.setFocusTraversalKeysEnabled(false);
    button_2.setFocusPainted(false);
    button_2.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_2.setPreferredSize(new Dimension(70, 70));
    button_2.setBackground(new Color(224,223,227));
    numberpanel.add(button_2);

    JButton button_3 = new JButton("3");
    button_3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_3.setFont(new Font("Arial", Font.PLAIN, 30));
    button_3.setFocusable(false);
    button_3.setFocusTraversalKeysEnabled(false);
    button_3.setFocusPainted(false);
    button_3.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_3.setPreferredSize(new Dimension(70, 70));
    button_3.setBackground(new Color(224,223,227));
    numberpanel.add(button_3);

    JButton button_0 = new JButton("0");
    button_0.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_0.setFont(new Font("Arial", Font.PLAIN, 30));
    button_0.setFocusable(false);
    button_0.setFocusTraversalKeysEnabled(false);
    button_0.setFocusPainted(false);
    button_0.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_0.setPreferredSize(new Dimension(70, 70));
    button_0.setBackground(new Color(224,223,227));
    numberpanel.add(button_0);

    JButton button_left = new JButton("");
    button_left.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_left.setIcon(new ImageIcon("more_buttons\\left.png"));
    button_left.setFont(new Font("Arial", Font.PLAIN, 30));
    button_left.setFocusable(false);
    button_left.setFocusTraversalKeysEnabled(false);
    button_left.setFocusPainted(false);
    button_left.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_left.setPreferredSize(new Dimension(70, 70));
    button_left.setBackground(new Color(224,223,227));
    numberpanel.add(button_left);

    JButton button_right = new JButton("");
    button_right.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }   
    });
    button_right.setIcon(new ImageIcon("more_buttons\\right.png"));
    button_right.setFont(new Font("Arial", Font.PLAIN, 30));
    button_right.setFocusable(false);
    button_right.setFocusTraversalKeysEnabled(false);
    button_right.setFocusPainted(false);
    button_right.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
    button_right.setPreferredSize(new Dimension(70, 70));
    button_right.setBackground(new Color(224,223,227));
    numberpanel.add(button_right);


    Card.add(bodypanel);
    bodypanel.add(divider, BorderLayout.WEST);
    bodypanel.add(numberpanel, BorderLayout.EAST);



}

   public static void main(String[] args){
     //Use the event dispatch thread for Swing components
 EventQueue.invokeLater(new Runnable()
 {
    @Override
     public void run()
     {
     new gridbaglayoutdemo();         
     }
     });

     }
 }

我的问题是在“输入时间:”的标签下方我放了一个空白标签。当我把JTextField 放在那里时,我无法像上图那样对齐它,因为它的单元格与上面的JLabel 对齐。我尝试了gridwidthgridheight(- 用于向上和向下按钮)、weightxinsets,并添加了一个空白JLabel(- 以占用行尾的空间)。而这一切都失败了。我不知道我做错了什么或什么。我也知道我不必打电话给setPreferredSize,但我必须打电话,因为我希望时间字段的宽度与上图一样。我不认为向上和向下按钮的对齐方式会像图片中那样,因为在GridBagLayout 中没有中间。好吧,我尝试将时间字段的gridheight 设置为2,结果仍然是这样。有没有办法像图片中一样对齐这些字段?

【问题讨论】:

  • "(这是我的代码的简短版本)" 短?在 489 LOC,您对“短”有一个有趣的定义。
  • c.gridy = 30; 你为什么用这样的号码?还要尝试更好地组织你的代码,你的 GUI 有一个清晰的结构,2 张卡片,一个数字面板......所以把所有这些放在不同的方法中。它会让您的生活更轻松。
  • @AndrewThompson 是的。如果不是那么短,我很抱歉。 xD
  • @DSquare 很好,它是该字段的位置。我跳过了 10 或 5。卡片和数字面板没有问题,因为在我的代码中它们都在单独的类中。我的原始代码也是有组织的。感谢您的关心。

标签: java swing alignment layout-manager gridbaglayout


【解决方案1】:

我认为您不知道 GridBagLayout 与 Grid NxM 一起使用,这意味着同一列中的所有组件具有相同宽度的单元格,并且同一行中的所有组件位于相同高度的单元格中。这只是一个网格。

问题很简单,你在第0列有一些组件:“输入日期:”标签,输入日期TextField......所以最宽的组件决定了该列有多大。现在你想放置一堆文本字段、标签和按钮来代表 [14:00 Up/down]。你现在这样做的方式是首先放置一个实际上不做任何事情的空标签(除了使用它的单元格),然后将所述组件放在第 2、3、4 列中......

但是,正如我所说,第 0 列非常宽,因此第 1+ 列从已经放在那里的所有内容的右侧开始,这就是为什么所有这些组件都出现在“右侧”的原因,因为第 0 列非常宽.

现在要将它们放在正确的位置,有 2 个选项。要么使以上所有这些组件跨越多个列,然后您就可以使用这些列将您需要的所有内容放在这些组件下,或者创建一个包含组件的面板,然后将此面板放在“输入时间:”下的第 0 列中:

在我看来,第二种方法要好得多,所以我去定义了一个timeInputPanel,用我们想要的组件填充它,然后把它放在panel_4 的第 0 列中。现在因为 timeInputPanel 只是 1 个组件,它可以驻留在第 0 列内而不会出现任何问题,即使它包含其他几个组件。

相关代码就是这样:

JPanel timeInputPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
timeInputPanel.add(text_33);
timeInputPanel.add(label_4);
timeInputPanel.add(text_4);
timeInputPanel.add(upDownPanel);

c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 15;
c.insets = new Insets(0, 10, 0, 0);
panel_4.add(timeInputPanel, c);

当然,您可能想根据自己的喜好更改 GridBagConstraints,唯一重要的是 c.gridx = 0; 和正确的 gridy

看起来像这样:

完整代码:

package cardlayoutalignment;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class gridbaglayoutdemo
{

    JFrame Card = new JFrame();

    FlowLayout flow = new FlowLayout(FlowLayout.RIGHT, 2, 2);
    Border etch = BorderFactory.createEtchedBorder(Color.white, Color.gray);
    Border margin = new EmptyBorder(10, 10, 10, 10);

    public static GridBagLayout grid = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    final static boolean shouldFill = true;

    JPanel container = new JPanel();
    JPanel divider = new JPanel();
    JPanel bodypanel = new JPanel();
    final JPanel buttonpanel = new JPanel();
    JPanel panel_1 = new JPanel();
    JPanel panel_4 = new JPanel();
    CardLayout cl = new CardLayout();

    public gridbaglayoutdemo()
    {
        Card.setVisible(true);
        Card.setSize(605, 333);
        Card.setTitle("Tank Delivery");
        // Card.setResizable(false);

        final Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - Card.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - Card.getHeight()) / 2);

        Card.setLocation(x, y);
        Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        bodypanel.setLayout(new BorderLayout());

        divider.setLayout(new BorderLayout());

        container.setLayout(cl);
        cl.show(container, "1");

        // JOptionPane.showMessageDialog(null,
        // "Memory is full. Finish 1 transaction in order to use Memory again");

        panel_1.setLayout(grid);

        JLabel label_1 = new JLabel("Enter Tank:");
        label_1.setFont(new Font("Arial", Font.PLAIN, 18));
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.weighty = 0;
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(10, 10, 0, 0);
        panel_1.add(label_1, c);

        JComboBox box_1 = new JComboBox();
        box_1.setPreferredSize(new Dimension(200, 30));
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.weighty = 0;
        c.gridx = 0;
        c.gridy = 20;
        c.insets = new Insets(10, 10, 0, 0);
        panel_1.add(box_1, c);

        JLabel label = new JLabel("");
        label.setFont(new Font("Arial", Font.PLAIN, 18));
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.weighty = 1;
        c.gridx = 0;
        c.gridy = 30;
        c.insets = new Insets(10, 0, 0, 0);
        panel_1.add(label, c);

        panel_4.setLayout(grid);

        JLabel label_2 = new JLabel("Enter Date:");
        label_2.setFont(new Font("Arial", Font.PLAIN, 18));
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.weighty = 0;
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(10, 10, 0, 0);
        panel_4.add(label_2, c);

        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        df.setLenient(false);

        JFormattedTextField text_2 = new JFormattedTextField(df);
        text_2.setValue(new Date());
        text_2.setPreferredSize(new Dimension(200, 30));
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.weighty = 0;
        c.gridx = 0;
        c.gridy = 5;
        c.insets = new Insets(10, 10, 0, 0);
        panel_4.add(text_2, c);

        JLabel label_3 = new JLabel("Enter Time:");
        label_3.setFont(new Font("Arial", Font.PLAIN, 18));
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 0.5;
        c.weighty = 0;
        c.gridx = 0;
        c.gridy = 10;
        c.insets = new Insets(10, 10, 0, 10);
        panel_4.add(label_3, c);


        String time3 = new java.text.SimpleDateFormat("HH").format(new java.util.Date());
        JFormattedTextField text_33 = new JFormattedTextField();
        text_33.setValue(time3);

        JLabel label_4 = new JLabel(":");
        label_4.setFont(new Font("Arial", Font.PLAIN, 18));

        String time2 = new java.text.SimpleDateFormat("mm").format(new java.util.Date());
        JFormattedTextField text_4 = new JFormattedTextField();
        text_4.setValue(time2);

        JButton btnup = new JButton("UP");
        btnup.setFont(new Font("Arial", Font.PLAIN, 18));

        JButton btndown = new JButton("DOWN");
        btndown.setFont(new Font("Arial", Font.PLAIN, 18));

        JPanel upDownPanel = new JPanel(new BorderLayout());
        upDownPanel.add(btnup, BorderLayout.PAGE_START);
        upDownPanel.add(btndown, BorderLayout.PAGE_END);

        JPanel timeInputPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        timeInputPanel.add(text_33);
        timeInputPanel.add(label_4);
        timeInputPanel.add(text_4);
        timeInputPanel.add(upDownPanel);

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridx = 0;
        c.gridy = 15;
        c.insets = new Insets(0, 10, 0, 0);
        panel_4.add(timeInputPanel, c);

        JLabel label14 = new JLabel("");
        label14.setFont(new Font("Arial", Font.PLAIN, 18));
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0;
        c.weighty = 1;
        c.gridx = 0;
        c.gridy = 30;
        c.insets = new Insets(10, 0, 0, 0);
        panel_4.add(label14, c);

        buttonpanel.setLayout(new FlowLayout(SwingConstants.RIGHT));
        buttonpanel.setBorder(new EmptyBorder(0, 10, 0, 0));

        JButton btnBack = new JButton("< Back");
        btnBack.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

                cl.previous(container);
                buttonpanel.repaint();
            }
        });
        btnBack.setFont(new Font("Arial", Font.PLAIN, 20));
        btnBack.setFocusable(false);
        btnBack.setFocusTraversalKeysEnabled(false);
        btnBack.setFocusPainted(false);
        btnBack.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        btnBack.setPreferredSize(new Dimension(110, 40));
        btnBack.setBackground(new Color(224, 223, 227));
        buttonpanel.add(btnBack);

        JButton btnNext = new JButton("Next >");
        btnNext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                cl.next(container);
                buttonpanel.repaint();
            }
        });
        btnNext.setFont(new Font("Arial", Font.PLAIN, 20));
        btnNext.setFocusable(false);
        btnNext.setFocusTraversalKeysEnabled(false);
        btnNext.setFocusPainted(false);
        btnNext.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        btnNext.setPreferredSize(new Dimension(110, 40));
        btnNext.setBackground(new Color(224, 223, 227));
        buttonpanel.add(btnNext);

        JButton btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        btnCancel.setFont(new Font("Arial", Font.PLAIN, 20));
        btnCancel.setFocusable(false);
        btnCancel.setFocusTraversalKeysEnabled(false);
        btnCancel.setFocusPainted(false);
        btnCancel.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        btnCancel.setPreferredSize(new Dimension(110, 40));
        btnCancel.setBackground(new Color(224, 223, 227));
        buttonpanel.add(btnCancel);

        divider.add(container, BorderLayout.NORTH);
        container.add(panel_1, "1");
        // container.add(p2.panel_2, "2");
        // container.add(p3.panel_3, "3");
        container.add(panel_4, "4");
        divider.add(buttonpanel, BorderLayout.SOUTH);

        JPanel numberpanel = new JPanel();
        numberpanel.setPreferredSize(new Dimension(221, 0));
        numberpanel.setBorder(new EmptyBorder(10, 0, 0, 10));
        numberpanel.setBorder(BorderFactory.createEtchedBorder(Color.white, Color.gray));
        numberpanel.setLayout(flow);

        JButton button_7 = new JButton("7");
        button_7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        button_7.setFont(new Font("Arial", Font.PLAIN, 30));
        button_7.setFocusable(false);
        button_7.setFocusTraversalKeysEnabled(false);
        button_7.setFocusPainted(false);
        button_7.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        button_7.setPreferredSize(new Dimension(70, 70));
        button_7.setBackground(new Color(224, 223, 227));
        numberpanel.add(button_7);

        JButton button_8 = new JButton("8");
        button_8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        button_8.setFont(new Font("Arial", Font.PLAIN, 30));
        button_8.setFocusable(false);
        button_8.setFocusTraversalKeysEnabled(false);
        button_8.setFocusPainted(false);
        button_8.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        button_8.setPreferredSize(new Dimension(70, 70));
        button_8.setBackground(new Color(224, 223, 227));
        numberpanel.add(button_8);

        JButton button_9 = new JButton("9");
        button_9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        button_9.setFont(new Font("Arial", Font.PLAIN, 30));
        button_9.setFocusable(false);
        button_9.setFocusTraversalKeysEnabled(false);
        button_9.setFocusPainted(false);
        button_9.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        button_9.setPreferredSize(new Dimension(70, 70));
        button_9.setBackground(new Color(224, 223, 227));
        numberpanel.add(button_9);

        JButton button_4 = new JButton("4");
        button_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        button_4.setFont(new Font("Arial", Font.PLAIN, 30));
        button_4.setFocusable(false);
        button_4.setFocusTraversalKeysEnabled(false);
        button_4.setFocusPainted(false);
        button_4.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        button_4.setPreferredSize(new Dimension(70, 70));
        button_4.setBackground(new Color(224, 223, 227));
        numberpanel.add(button_4);

        JButton button_5 = new JButton("5");
        button_5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        button_5.setFont(new Font("Arial", Font.PLAIN, 30));
        button_5.setFocusable(false);
        button_5.setFocusTraversalKeysEnabled(false);
        button_5.setFocusPainted(false);
        button_5.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        button_5.setPreferredSize(new Dimension(70, 70));
        button_5.setBackground(new Color(224, 223, 227));
        numberpanel.add(button_5);

        JButton button_6 = new JButton("6");
        button_6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        button_6.setFont(new Font("Arial", Font.PLAIN, 30));
        button_6.setFocusable(false);
        button_6.setFocusTraversalKeysEnabled(false);
        button_6.setFocusPainted(false);
        button_6.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        button_6.setPreferredSize(new Dimension(70, 70));
        button_6.setBackground(new Color(224, 223, 227));
        numberpanel.add(button_6);

        JButton button_1 = new JButton("1");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        button_1.setFont(new Font("Arial", Font.PLAIN, 30));
        button_1.setFocusable(false);
        button_1.setFocusTraversalKeysEnabled(false);
        button_1.setFocusPainted(false);
        button_1.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        button_1.setPreferredSize(new Dimension(70, 70));
        button_1.setBackground(new Color(224, 223, 227));
        numberpanel.add(button_1);

        JButton button_2 = new JButton("2");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        button_2.setFont(new Font("Arial", Font.PLAIN, 30));
        button_2.setFocusable(false);
        button_2.setFocusTraversalKeysEnabled(false);
        button_2.setFocusPainted(false);
        button_2.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        button_2.setPreferredSize(new Dimension(70, 70));
        button_2.setBackground(new Color(224, 223, 227));
        numberpanel.add(button_2);

        JButton button_3 = new JButton("3");
        button_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        button_3.setFont(new Font("Arial", Font.PLAIN, 30));
        button_3.setFocusable(false);
        button_3.setFocusTraversalKeysEnabled(false);
        button_3.setFocusPainted(false);
        button_3.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        button_3.setPreferredSize(new Dimension(70, 70));
        button_3.setBackground(new Color(224, 223, 227));
        numberpanel.add(button_3);

        JButton button_0 = new JButton("0");
        button_0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        button_0.setFont(new Font("Arial", Font.PLAIN, 30));
        button_0.setFocusable(false);
        button_0.setFocusTraversalKeysEnabled(false);
        button_0.setFocusPainted(false);
        button_0.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        button_0.setPreferredSize(new Dimension(70, 70));
        button_0.setBackground(new Color(224, 223, 227));
        numberpanel.add(button_0);

        JButton button_left = new JButton("");
        button_left.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        button_left.setIcon(new ImageIcon("more_buttons\\left.png"));
        button_left.setFont(new Font("Arial", Font.PLAIN, 30));
        button_left.setFocusable(false);
        button_left.setFocusTraversalKeysEnabled(false);
        button_left.setFocusPainted(false);
        button_left.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        button_left.setPreferredSize(new Dimension(70, 70));
        button_left.setBackground(new Color(224, 223, 227));
        numberpanel.add(button_left);

        JButton button_right = new JButton("");
        button_right.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        button_right.setIcon(new ImageIcon("more_buttons\\right.png"));
        button_right.setFont(new Font("Arial", Font.PLAIN, 30));
        button_right.setFocusable(false);
        button_right.setFocusTraversalKeysEnabled(false);
        button_right.setFocusPainted(false);
        button_right.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
        button_right.setPreferredSize(new Dimension(70, 70));
        button_right.setBackground(new Color(224, 223, 227));
        numberpanel.add(button_right);

        Card.add(bodypanel);
        bodypanel.add(divider, BorderLayout.WEST);
        bodypanel.add(numberpanel, BorderLayout.EAST);

    }

    public static void main(String[] args)
    {
        // Use the event dispatch thread for Swing components
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run()
            {
                new gridbaglayoutdemo();
            }
        });

    }
}

【讨论】:

  • 这太棒了!非常感谢!现在我学到了一些东西。我实际上知道一列将具有相同的宽度和高度,但我唯一不知道的是我可以将我的其他字段放在面板中,我可以将它插入到一个单元格中。我只会将我的gridy 更改为 1。我习惯于输入跳过 10 秒或 5 秒的值。我很抱歉。
猜你喜欢
  • 2013-07-08
  • 2015-04-05
  • 2014-06-04
  • 2012-07-09
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 2013-01-23
相关资源
最近更新 更多