Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的复选框组件在GridLayout中未正确对齐_Java_Swing_Jpanel_Layout Manager_Jcheckbox - Fatal编程技术网

Java 我的复选框组件在GridLayout中未正确对齐

Java 我的复选框组件在GridLayout中未正确对齐,java,swing,jpanel,layout-manager,jcheckbox,Java,Swing,Jpanel,Layout Manager,Jcheckbox,在JPanel中有laborServicesCB(复选框)和laborServicesTF(文本字段),并且在GridLayout中添加了面板。现在,我添加的面板与GridLayout中的其他组件不对齐 /******************************************************************************** * Program Name: JoeSAutomotive.java * Created Date: 3/3/2016 *

在JPanel中有laborServicesCB(复选框)和laborServicesTF(文本字段),并且在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);
    }

}
以下是我得到的:

以下是我想要的:

您可以使用EmptyBorder将每个复选框推到中间。请尝试下面的方法,并检查是否有帮助

oilChangeCB=新的JCheckBox(“换油($26.00)”//换油
oilChangeCB.setboorder(BorderFactory.createEmptyBorder(0,175,0,0));
lubeJobCB=新的JCheckBox(“润滑工作($18.00)”)//润滑油工作
lubeJobCB.setboorder(BorderFactory.createEmptyByOrder(0,175,0,0));
散热器冲洗CB=新的JCheckBox(“散热器冲洗($30.00)”)//散热器冲洗
散热器冲洗cb.setBorder(BorderFactory.createEmptyByOrder(0,175,0,0));
transmissionFlushCB=新的JCheckBox(“变速器刷新($80.00”);//变速器刷新
transmissionFlushCB.setBorder(BorderFactory.CreateEmptyByOrder(0,175,0,0));
inspectionCB=新的JCheckBox(“检查($15.00)”;//inspectionCB
inspectionCB.setboorder(BorderFactory.createEmptyBorder(0,175,0,0));
消声器更换CBCB=新的JCheckBox(“消声器更换($100.00)”;//消声器更换
mufflerReplacementCBCB.setBorder(BorderFactory.CreateEmptyByOrder(0,175,0,0));
TireRotationB=新的JCheckBox(“轮胎旋转($20.00)”;//轮胎旋转

tireRotationCB.setBorder(BorderFactory.createEmptyBorder(0,175,0,0));

您可以使用EmptyBorder将每个复选框推到中间。请尝试下面的方法,并检查是否有帮助

oilChangeCB=newjcheckbox(“换油($26.00)”;//换油
oilChangeCB.setboorder(BorderFactory.createEmptyBorder(0,175,0,0));
lubeJobCB=新的JCheckBox(“润滑作业($18.00)”;//润滑作业
lubeJobCB.setboorder(BorderFactory.createEmptyByOrder(0,175,0,0));
散热器冲洗CB=新的JCheckBox(“散热器冲洗($30.00)”;//散热器冲洗
散热器冲洗cb.setBorder(BorderFactory.createEmptyByOrder(0,175,0,0));
transmissionFlushCB=新的JCheckBox(“变速器刷新($80.00”);//变速器刷新
transmissionFlushCB.setBorder(BorderFactory.CreateEmptyByOrder(0,175,0,0));
inspectionCB=新的JCheckBox(“检查($15.00)”;//inspectionCB
inspectionCB.setboorder(BorderFactory.createEmptyBorder(0,175,0,0));
消声器更换CBCB=新的JCheckBox(“消声器更换($100.00)”;//消声器更换
mufflerReplacementCBCB.setBorder(BorderFactory.CreateEmptyByOrder(0,175,0,0));
TireRotationB=新的JCheckBox(“轮胎旋转($20.00)”;//轮胎旋转

tireRotationCB.setBorderFactory.createEmptyBorder(0,175,0,0));
如果你想做什么,用GridLayout是完全不可能的。在GridLayout下,所有单元格的尺寸都是相等的。实际上,在每个网格中添加一个面板数组是一个技巧

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


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

按照您的意愿,使用GridLayout是不可能做到的。在GridLayout下,所有单元格的尺寸都是相等的。实际上,在每个网格中添加一组面板是一个技巧

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


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

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