【问题标题】:GUI program terminates without displayGUI 程序终止而不显示
【发布时间】: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


【解决方案1】:

@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
    }
}

【讨论】:

    【解决方案2】:

    到目前为止,您拥有的是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
    }
    

    【讨论】:

    • @sam 它应该在类的主体内。你应该用这个替换你已经拥有的main(...) 方法。
    • 我这样做了,但它仍然没有运行
    • @sam 我刚刚测试过它并且它有效。您能否编辑您的问题以反映您所做的更改?
    • 以下是我目前所拥有的
    • @sam 没有计算serviceCost,因为您从未调用过getServiceCost() 方法,您应该在此面板中添加一个按钮并在按下按钮时调用该方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 2013-03-02
    • 2022-11-25
    相关资源
    最近更新 更多