我的Checkbox组件未在GridLayout内正确对齐

时间:2016-03-04 02:59:11

标签: java swing jpanel layout-manager jcheckbox

我在JPanel中有laborServicesCB(CheckBox)和laborServicesTF(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);
    }

}

这是我得到的: enter image description here

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

2 个答案:

答案 0 :(得分:0)

您可以使用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));

答案 1 :(得分:0)

根据您的想法,使用 GridLayout 进行操作是不可能的。在GridLayout下,所有单元格的尺寸相同。通过在每个网格中添加一组面板实际上是一种黑客攻击。

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

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