java - 我的复选框组件在 GridLayout 内未正确对齐

标签 java swing jpanel layout-manager jcheckbox

我在 JPanel 内有 LaborServicesCB (CheckBox)和 LaborServicesTF (TextField),并且我在 GridLayout 内添加了面板。现在,我添加的面板与 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);
    }

}

这是我得到的: enter image description here

这就是我想要的: enter image description here

最佳答案

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

oilChangeCB = new JCheckBox("机油更换 ($26.00)");//换油 oilChangeCB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0)); lubeJobCB = new JCheckBox("润滑油作业 ($18.00)");//润滑工作 lubeJobCB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0)); RadiatorFlushCB = new JCheckBox("散热器冲洗 ($30.00)");//散热器冲洗 散热器FlushCB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0)); TransmissionFlushCB = new JCheckBox("Transmission Flush ($80.00");//传输刷新 TransmissionFlushCB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0)); InspectionCB = new JCheckBox("检查 ($15.00)");//检查CB 检查CB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0)); mufflerReplacementCBCB = new JCheckBox("消声器更换 ($100.00)");//消声器更换 mufflerReplacementCBCB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0)); tireRotationCB = new JCheckBox("轮胎旋转 ($20.00)");//轮胎换位 tireRotationCB.setBorder(BorderFactory.createEmptyBorder(0, 175, 0, 0));

关于java - 我的复选框组件在 GridLayout 内未正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35787169/

相关文章:

java - 在 JAVA 中将列表对象添加到另一个列表中

java - 我的 JSlider 和下面的文本之间有巨大的空间

java - 通过事件添加带有 JTabbedPane 的新选项卡

java - 使用 MVC 绘制同心/旋转图形

java - 在单个 JFrame 上通过不同方法显示多个 JPanel

java - 从 Activity 调用 Fragment 类时出错

java - 如何使 kotlin 中的主要 java 方法更简单?

java - JAVA 中 TabbedPane 的问题

java - 如何在发生 mouseReleased 事件时将图像添加到 JPanel

java - <identifier> 创建对象列表时出现预期错误