【发布时间】:2016-03-18 13:48:45
【问题描述】:
我正在尝试编写一个 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();
}
}
【问题讨论】:
-
您目前拥有的是
JPanel,您需要将此面板添加到框架并显示该框架。 -
如何将它添加到框架中?你能给我指一个网站吗?
-
我的总费用怎么没有出现?
标签: java user-interface checkbox checkboxlist