GUI程序终止而不显示

时间:2015-12-12 19:28:56

标签: java user-interface checkbox checkboxlist

我正在尝试编写一个GUI应用程序,显示客户访问Joe汽车中心的总数。我使用复选框点击服务,然后添加到客户的总数中。我的程序运行并快速终止,没有任何弹出。

import javax.swing.*;
import javax.swing.border.TitledBorder;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;



public class JoesAuto extends JPanel{

    private JTextField tOilchangeBox;
    private JTextField tLubeJob;
    private JTextField tRadiatorFlush;
    private JTextField tTransmissionFlush;
    private JTextField tInspection;
    private JTextField tMufflerReplacement;
    private JTextField tTireRotation; 

    private JCheckBox oilchangeBox;
    private JCheckBox lubeJob;
    private JCheckBox radiatorFlush;
    private JCheckBox transmissionFlush;
    private JCheckBox inspection;
    private JCheckBox mufflerReplacement;
    private JCheckBox tireRotation;





    public final double OIL_CHANGE_BOX = 26;
    public final double LUBE_JOB = 18;
    public final double RADIATOR_FLUSH = 30;
    public final double TRANSMISSION_FLUSH = 80;
    public final double INSPECTION = 15;
    public final double MUFFLER_REPLACEMENT = 100;
    public final double TIRE_ROTATION = 20;



    //CONSTRUCTOR

    public JoesAuto()
    {

        setLayout(new GridLayout(7,1));

        oilchangeBox = new JCheckBox ("Oil Change Box");
        lubeJob = new JCheckBox ("Lube Job");
        radiatorFlush= new JCheckBox ("Radiator Flush");
        transmissionFlush = new JCheckBox ("Transmission Flush");
        inspection = new JCheckBox ("Inspection");
        mufflerReplacement = new JCheckBox ("Muffler Replacement");
        tireRotation = new JCheckBox ("Tire Rotation");

        //add a border around the panel 
        setBorder(BorderFactory.createTitledBorder("Services"));

        //add checkboxes to the panel 

        add(oilchangeBox);
        add(lubeJob);
        add(radiatorFlush);
        add(transmissionFlush);
        add(inspection);
        add(mufflerReplacement);
        add(tireRotation);
    }

        //get method returns the cost of selected services 

        public double getServiceCost()
        {
        double serviceCost = 0.0;

        if(oilchangeBox.isSelected())
            serviceCost += OIL_CHANGE_BOX;
        if(lubeJob.isSelected())
            serviceCost += LUBE_JOB;
        if(radiatorFlush.isSelected())
            serviceCost += RADIATOR_FLUSH;
        if(transmissionFlush.isSelected())
            serviceCost += TRANSMISSION_FLUSH;
        if(inspection.isSelected())
            serviceCost += INSPECTION;
        if(mufflerReplacement.isSelected())
            serviceCost += MUFFLER_REPLACEMENT;
        if(tireRotation.isSelected())
            serviceCost += TIRE_ROTATION;

        return serviceCost;

        }

        public static void main(String args[])
        {
            new JoesAuto();
        }
    }

2 个答案:

答案 0 :(得分:0)

到目前为止您所拥有的是JPanel,您需要将此面板添加到框架并显示该框架。

以下是一个例子:

public static void main(String args[]){
    JFrame frame = new JFrame(); // create a new frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //terminate the program when this frame is closed
    frame.add(new JoesAuto()); // add the panel to the frame
    frame.pack(); // resize the frame to fit all the components
    frame.setVisible(true); // show the frame
}

答案 1 :(得分:0)

@titus这是我到目前为止所做的,它确实有效,为什么它不计算我选择的复选框

import javax.swing.*;
import javax.swing.border.TitledBorder;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;



public class JoesAuto extends JPanel{

private JTextField tOilchangeBox;
private JTextField tLubeJob;
private JTextField tRadiatorFlush;
private JTextField tTransmissionFlush;
private JTextField tInspection;
private JTextField tMufflerReplacement;
private JTextField tTireRotation; 

private JCheckBox oilchangeBox;
private JCheckBox lubeJob;
private JCheckBox radiatorFlush;
private JCheckBox transmissionFlush;
private JCheckBox inspection;
private JCheckBox mufflerReplacement;
private JCheckBox tireRotation;





public final double OIL_CHANGE_BOX = 26;
public final double LUBE_JOB = 18;
public final double RADIATOR_FLUSH = 30;
public final double TRANSMISSION_FLUSH = 80;
public final double INSPECTION = 15;
public final double MUFFLER_REPLACEMENT = 100;
public final double TIRE_ROTATION = 20;



//CONSTRUCTOR

public JoesAuto()
{

    setLayout(new GridLayout(7,1));

    oilchangeBox = new JCheckBox ("Oil Change Box");
    lubeJob = new JCheckBox ("Lube Job");
    radiatorFlush= new JCheckBox ("Radiator Flush");
    transmissionFlush = new JCheckBox ("Transmission Flush");
    inspection = new JCheckBox ("Inspection");
    mufflerReplacement = new JCheckBox ("Muffler Replacement");
    tireRotation = new JCheckBox ("Tire Rotation");

    //add a border around the panel 
    setBorder(BorderFactory.createTitledBorder("Services"));

    //add checkboxes to the panel 

    add(oilchangeBox);
    add(lubeJob);
    add(radiatorFlush);
    add(transmissionFlush);
    add(inspection);
    add(mufflerReplacement);
    add(tireRotation);
}

    //get method returns the cost of selected services 

    public double getServiceCost()
    {
    double serviceCost = 0.0;

    if(oilchangeBox.isSelected())
        serviceCost += OIL_CHANGE_BOX;
    if(lubeJob.isSelected())
        serviceCost += LUBE_JOB;
    if(radiatorFlush.isSelected())
        serviceCost += RADIATOR_FLUSH;
    if(transmissionFlush.isSelected())
        serviceCost += TRANSMISSION_FLUSH;
    if(inspection.isSelected())
        serviceCost += INSPECTION;
    if(mufflerReplacement.isSelected())
        serviceCost += MUFFLER_REPLACEMENT;
    if(tireRotation.isSelected())
        serviceCost += TIRE_ROTATION;

    return serviceCost;

    }

    public static void main(String args[]){
        JFrame frame = new JFrame(); // create a new frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //terminate the program when this frame is closed
        frame.add(new JoesAuto()); // add the panel to the frame
        frame.pack(); // resize the frame to fit all the components
        frame.setVisible(true); // show the frame
    }
}