【问题标题】:My Checkbox components are not aligned correctly inside a GridLayout我的 Checkbox 组件在 GridLayout 内未正确对齐
【发布时间】:2016-03-04 02:59:11
【问题描述】:

我在 JPanel 中有 labourServicesCB (CheckBox) 和 labourServicesTF (TextField),我在 GridLayout 中添加了 Panel。现在,我添加的面板与该 GridLayout 内的其他组件不对齐。

/********************************************************************************
 * Program Name: JoeSAutomotive.java
 * Created Date: 3/3/2016 
 * Created By: Tommy Saechao 
 * Purpose: A GUI Application that displays total cost for Joe's services
*******************************************************************************/

package pkg12.pkg6.joe.s.automotive;

import java.awt.*; //layout managers
import java.awt.event.*; //event listeners
import javax.swing.*; //jframe


public class JoeSAutomotive extends JFrame implements ActionListener, ItemListener
{

    //Window size
    private final int WINDOW_WIDTH = 600;
    private final int WINDOW_HEIGHT = 800;


    //Services Panel
    JPanel servicesPanel;

    //Buttons Panel
    JPanel buttonsPanel;

    //hourly services panel, will be implemented into servicesPanel to show a checkbox and a textfield
    JPanel hourlyServicesPanel;

    //servicesPanel checkboxes
    JCheckBox oilChangeCB; //oil change
    JCheckBox lubeJobCB; //lube job 
    JCheckBox radiatorFlushCB; //radiator flush
    JCheckBox transmissionFlushCB; //transmission flush
    JCheckBox inspectionCB; //inspectionCB
    JCheckBox mufflerReplacementCBCB; //muffler replacement
    JCheckBox tireRotationCB; //tire rotation
    JCheckBox laborServicesCB; //hourly labor checkbox
    JTextField laborServicesTF; //hourly labor textfield

    //buttonsPanel buttons
    JButton calcButton; //calculates total cost

    public JoeSAutomotive()
    {
        //Sets title
        setTitle("Joe's Automotive Services");

        //Sets window size
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        setLocationRelativeTo(null); //centers frame

        //Exits JFrame when closing window
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set JFrame's default layout
        setLayout(new GridLayout(2,1));

        //Builds servicesPanel, which holds all of the checkboxes and add it to JFrame at the top of the frame
        buildServicesPanel();
        add(servicesPanel);

        //Build and add buttonPanel to JFrame at the center of the frame
        buildButtonPanel();
        add(buttonsPanel);



        //Display window
        setVisible(true);
    }

    public static void main(String[] args)
    {
        JoeSAutomotive joeSAutomotive = new JoeSAutomotive();
    }

    //ActionPerformed method
    @Override
    public void actionPerformed(ActionEvent ae)
    {

    }

    //Itemlistener Method
    @Override
    public void itemStateChanged(ItemEvent ie)
    {

    }

    private void buildServicesPanel()
    {
        //instantiates servicesPanel
        servicesPanel = new JPanel();

        //instantiates hourly servicesPanel
        hourlyServicesPanel = new JPanel();

        //set layout for servicesPanel
        servicesPanel.setLayout(new GridLayout(8,1));

        //Instantiates checkboxes and give them texts
        oilChangeCB = new JCheckBox("Oil Change ($26.00)"); //oil change
        lubeJobCB = new JCheckBox("Lube Job ($18.00)"); //lube job 
        radiatorFlushCB = new JCheckBox("Radiator Flush ($30.00)"); //radiator flush
        transmissionFlushCB = new JCheckBox("Transmission Flush ($80.00"); //transmission flush
        inspectionCB = new JCheckBox("Inspection ($15.00)"); //inspectionCB
        mufflerReplacementCBCB = new JCheckBox("Muffler Replacement ($100.00)"); //muffler replacement
        tireRotationCB = new JCheckBox("Tire rotation ($20.00)"); //tire rotation
        laborServicesCB = new JCheckBox("Hourly Labor Services ($20/hr)");
        laborServicesTF = new JTextField("0", 3);

        //add checkbox components to servicesPanel
        servicesPanel.add(oilChangeCB);
        servicesPanel.add(lubeJobCB);
        servicesPanel.add(radiatorFlushCB);
        servicesPanel.add(transmissionFlushCB);
        servicesPanel.add(inspectionCB);
        servicesPanel.add(mufflerReplacementCBCB);
        servicesPanel.add(tireRotationCB);

        //Hourly Services checkbox and textfield
        hourlyServicesPanel.add(laborServicesCB);
        hourlyServicesPanel.add(laborServicesTF);

        //adds hourlyServicesPanel to servicesPanel
        servicesPanel.add(hourlyServicesPanel);

    }

    private void buildButtonPanel()
    {
        //instantiates buttonPanel
        buttonsPanel = new JPanel();

        //instantiates calcButton
        calcButton = new JButton("Calculate Total");

        //add calcButton to buttonsPanel
        buttonsPanel.add(calcButton);
    }

}

这是我得到的:

这就是我想要的:

【问题讨论】:

  • GUI 图像会有所帮助,包括您正在获得的图像以及您想要获得的图像。
  • 我已经添加了图片。 :)
  • @TommySaechao 看看下面我的解决方案。

标签: java swing jpanel layout-manager jcheckbox


【解决方案1】:

您可以使用 EmptyBorder 将每个复选框推到中心。尝试以下操作并检查是否有帮助。

oilChangeCB = new JCheckBox("Oil Change ($26.00)"); //oil change oilChangeCB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0)); lubeJobCB = new JCheckBox("Lube Job ($18.00)"); //lube job lubeJobCB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0)); radiatorFlushCB = new JCheckBox("Radiator Flush ($30.00)"); //radiator flush radiatorFlushCB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0)); transmissionFlushCB = new JCheckBox("Transmission Flush ($80.00"); //transmission flush transmissionFlushCB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0)); inspectionCB = new JCheckBox("Inspection ($15.00)"); //inspectionCB inspectionCB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0)); mufflerReplacementCBCB = new JCheckBox("Muffler Replacement ($100.00)"); //muffler replacement mufflerReplacementCBCB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0)); tireRotationCB = new JCheckBox("Tire rotation ($20.00)"); //tire rotation tireRotationCB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0));

【讨论】:

    【解决方案2】:

    你想做的事,用GridLayout是完全不可能的。在 GridLayout 下,所有单元格的尺寸都相同。在每个网格中添加一组面板实际上是一种技巧。

    将网格中每个面板的对齐方式设置为FlowLayout.LEFT。这样您就可以集中所有 7 个复选框。 但是,第 8 个复选框带有一个文本字段。在这种情况下,我们需要将网格的中心列调整为更大的宽度,以便它可以连续容纳 2 个组件。

    为此,您需要不同的布局。 GridBagLayout 将能够做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 2012-04-09
      • 2017-07-03
      相关资源
      最近更新 更多