试图将Jcombobox列表添加到Jtextfield区域

发布于 2025-01-22 17:39:47 字数 6667 浏览 2 评论 0原文

我花了很多时间在网上挖掘,似乎找不到直接的答案或方法,可以在我的主面板中添加一个下拉列表,以包含各种文本字段,在这种情况下,我试图在面板,因此可以将颜色用作确定成本的变量。

我到目前为止最接近的是在新窗口中有一个组合盒弹出窗口,但后来无法检索选择。

package paintestimator;
import java.lang.String;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Component;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.PopupMenu;
import javax.swing.JComboBox;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

 public class PaintEstimator extends JFrame
     {





private JTextField wallHeight = new JTextField(3);
private JTextField wallWidth = new JTextField(3);
private JTextField wallArea = new JTextField(3);
private JTextField gallonsNeeded = new JTextField(3);
private JTextField cansNeeded = new JTextField(3);
private JTextField paintTime = new JTextField(3);
private JTextField sColor = new JTextField(3);
private JTextField matCost = new JTextField(3);
private JTextField laborCost = new JTextField(3);

//Josh
public PaintEstimator()

{

    
    JButton CalcChangeBTN = new JButton("Calculate");
    JButton ClearBTN = new JButton("Clear");
    JButton ChoiceA = new JButton("Paint Color");
    //JComboBox vendorBTN = (new JComboBox());
    
    ChoiceA.addActionListener(new ChoiceAListener());
    CalcChangeBTN.addActionListener(new CalcChangeBTNListener());
    ClearBTN.addActionListener(new ClearBTNListener());
    
    wallHeight.setEditable(true);
    wallWidth.setEditable(true);
    wallArea.setEditable(false);
    gallonsNeeded.setEditable(false);
    paintTime.setEditable(false);
    cansNeeded.setEditable(false);
   sColor.setEditable(true); 
    matCost.setEditable(false);
    laborCost.setEditable(true);



    JPanel mainPanel = new JPanel();
    

    mainPanel.setLayout(new GridLayout(16, 3, 0, 0));

    // need cost of paint array to set equations for material cost
    // need combobox arrays for both color and cost
    // vendor selection combo box sets array field for cost/color
    
    

    mainPanel.add(new JLabel("Please enter wall height in feet"));

    mainPanel.add(wallHeight);

    mainPanel.add(new JLabel("please enter wall width in feet"));

    mainPanel.add(wallWidth);

    //box to show chosen color 
    //mainPanel.add(new JLabel("Color Chosen"));
   // mainPanel.add(sColor);

    mainPanel.add(new JLabel("wall area"));

    mainPanel.add(wallArea);

    mainPanel.add(new JLabel("Gallons Needed"));

    mainPanel.add(gallonsNeeded);

    mainPanel.add(new JLabel("Number of cans Needed"));
    mainPanel.add(cansNeeded);

    mainPanel.add(new JLabel("Time to paint in Hours"));

    mainPanel.add(paintTime);

    mainPanel.add(new JLabel("Cost of Labor"));
    mainPanel.add(laborCost);

    mainPanel.add(new JLabel("Total Cost of Material"));
    mainPanel.add(matCost);

    mainPanel.add( new JLabel("Select a Color"));
    mainPanel.add (ChoiceA);
   // mainPanel.add(sColor);
    
    
   // Select<String> select = new Select<>();
//select.setLabel("Sort by");
//select.setItems("Most recent first", "Rating: high to low",
 // "Rating: low to high", "Price: high to low", "Price: low to high");
//select.setValue("Most recent first");
   // mainPanel.add(select);
    
    mainPanel.add(CalcChangeBTN);

    mainPanel.add(ClearBTN);
    
   // mainPanel.add(ChoiceA);
    
    setContentPane(mainPanel);

    pack();

    setTitle("Paint Estimator Tool");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLocationRelativeTo(null);
}




//Josh
class CalcChangeBTNListener implements ActionListener
{

    public void actionPerformed(ActionEvent e)
    {
        try
        {

            final double paintCvrGal = 320.0;

            final double gallonsPerCan = 1.0;

            double h = Integer.parseInt(wallHeight.getText());

            double w = Integer.parseInt(wallWidth.getText());

            double a = h * w;

            double c = ((a / paintCvrGal) * gallonsPerCan);

            double n = (c / gallonsPerCan);

            double p = (int) ((a * 0.76) / 60);

            double l = Integer.parseInt(laborCost.getText());

            double labCost = l * p;

            // double mc = c * CostofPaint
            wallArea.setText(String.valueOf(a));
            String wallArea = String.valueOf(a);

            gallonsNeeded.setText(String.valueOf(c));
            String gallonsNeeded = Double.toString(c);

            cansNeeded.setText(String.valueOf(n));
            String cansNeeded = Double.toString(n); // still need refine decimal point to #.0 
 (one decimal place)

            paintTime.setText(String.valueOf(p));
            String paintTime = String.valueOf(p);

            laborCost.setText(String.valueOf(labCost));
            String laborCost = Double.toString(labCost);
            
            
            


        } catch (NumberFormatException f)
        {
            wallHeight.requestFocus();
            wallHeight.selectAll();
        }
    }

}

class ClearBTNListener implements ActionListener
{

    public void actionPerformed(ActionEvent e)
    {

// clear text fields and set focus
        cansNeeded.setText("");
        gallonsNeeded.setText("");
        wallArea.setText("");
        wallHeight.setText("");
        wallWidth.setText("");
        paintTime.setText("");
        sColor.setText("");
        laborCost.setText("");
        //set focus
        wallHeight.requestFocus();
    }
}

class ChoiceAListener implements ActionListener
{
public void actionPreformed (ActionEvent e)
{ 
    
   

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


    
    PaintEstimator window = new PaintEstimator();
    window.setVisible(true);
    

}

}

以下是我能够创建的组合框:

public static void main(String[] args)
{
    String[] optionsToChoose =
    {
        "White", "Cream", "Sage", "Light Blue", "Eggshell White"
    };

    JFrame jFrame = new JFrame();

    JComboBox<String> jComboBox = new JComboBox<>(optionsToChoose);
    jComboBox.setBounds(80, 50, 140, 20);

    JButton jButton = new JButton("Done");
    jButton.setBounds(100, 100, 90, 20);

    JLabel jLabel = new JLabel();
    jLabel.setBounds(90, 100, 400, 100);

    jFrame.add(jButton);
    jFrame.add(jComboBox);
    jFrame.add(jLabel);

    jFrame.setLayout(null);
    jFrame.setSize(350, 250);
    jFrame.setVisible(true);

    jButton.addActionListener((ActionEvent e) ->
    {
        String selectedColor = "You selected " + 
jComboBox.getItemAt(jComboBox.getSelectedIndex());
        jLabel.setText(selectedColor);
    });

}

I have spent many hours digging around the web and cannot seem to find a straightforward answer or method to add a dropdown selection into my main panel that houses the various text fields, in this case, I am trying to add a paint color selection to the panel so that color can then be used as a variable in determining cost.

The closest I have come so far is having a combo box popup in a new window but then was unable to retrieve the selection.

package paintestimator;
import java.lang.String;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Component;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.PopupMenu;
import javax.swing.JComboBox;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

 public class PaintEstimator extends JFrame
     {





private JTextField wallHeight = new JTextField(3);
private JTextField wallWidth = new JTextField(3);
private JTextField wallArea = new JTextField(3);
private JTextField gallonsNeeded = new JTextField(3);
private JTextField cansNeeded = new JTextField(3);
private JTextField paintTime = new JTextField(3);
private JTextField sColor = new JTextField(3);
private JTextField matCost = new JTextField(3);
private JTextField laborCost = new JTextField(3);

//Josh
public PaintEstimator()

{

    
    JButton CalcChangeBTN = new JButton("Calculate");
    JButton ClearBTN = new JButton("Clear");
    JButton ChoiceA = new JButton("Paint Color");
    //JComboBox vendorBTN = (new JComboBox());
    
    ChoiceA.addActionListener(new ChoiceAListener());
    CalcChangeBTN.addActionListener(new CalcChangeBTNListener());
    ClearBTN.addActionListener(new ClearBTNListener());
    
    wallHeight.setEditable(true);
    wallWidth.setEditable(true);
    wallArea.setEditable(false);
    gallonsNeeded.setEditable(false);
    paintTime.setEditable(false);
    cansNeeded.setEditable(false);
   sColor.setEditable(true); 
    matCost.setEditable(false);
    laborCost.setEditable(true);



    JPanel mainPanel = new JPanel();
    

    mainPanel.setLayout(new GridLayout(16, 3, 0, 0));

    // need cost of paint array to set equations for material cost
    // need combobox arrays for both color and cost
    // vendor selection combo box sets array field for cost/color
    
    

    mainPanel.add(new JLabel("Please enter wall height in feet"));

    mainPanel.add(wallHeight);

    mainPanel.add(new JLabel("please enter wall width in feet"));

    mainPanel.add(wallWidth);

    //box to show chosen color 
    //mainPanel.add(new JLabel("Color Chosen"));
   // mainPanel.add(sColor);

    mainPanel.add(new JLabel("wall area"));

    mainPanel.add(wallArea);

    mainPanel.add(new JLabel("Gallons Needed"));

    mainPanel.add(gallonsNeeded);

    mainPanel.add(new JLabel("Number of cans Needed"));
    mainPanel.add(cansNeeded);

    mainPanel.add(new JLabel("Time to paint in Hours"));

    mainPanel.add(paintTime);

    mainPanel.add(new JLabel("Cost of Labor"));
    mainPanel.add(laborCost);

    mainPanel.add(new JLabel("Total Cost of Material"));
    mainPanel.add(matCost);

    mainPanel.add( new JLabel("Select a Color"));
    mainPanel.add (ChoiceA);
   // mainPanel.add(sColor);
    
    
   // Select<String> select = new Select<>();
//select.setLabel("Sort by");
//select.setItems("Most recent first", "Rating: high to low",
 // "Rating: low to high", "Price: high to low", "Price: low to high");
//select.setValue("Most recent first");
   // mainPanel.add(select);
    
    mainPanel.add(CalcChangeBTN);

    mainPanel.add(ClearBTN);
    
   // mainPanel.add(ChoiceA);
    
    setContentPane(mainPanel);

    pack();

    setTitle("Paint Estimator Tool");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLocationRelativeTo(null);
}




//Josh
class CalcChangeBTNListener implements ActionListener
{

    public void actionPerformed(ActionEvent e)
    {
        try
        {

            final double paintCvrGal = 320.0;

            final double gallonsPerCan = 1.0;

            double h = Integer.parseInt(wallHeight.getText());

            double w = Integer.parseInt(wallWidth.getText());

            double a = h * w;

            double c = ((a / paintCvrGal) * gallonsPerCan);

            double n = (c / gallonsPerCan);

            double p = (int) ((a * 0.76) / 60);

            double l = Integer.parseInt(laborCost.getText());

            double labCost = l * p;

            // double mc = c * CostofPaint
            wallArea.setText(String.valueOf(a));
            String wallArea = String.valueOf(a);

            gallonsNeeded.setText(String.valueOf(c));
            String gallonsNeeded = Double.toString(c);

            cansNeeded.setText(String.valueOf(n));
            String cansNeeded = Double.toString(n); // still need refine decimal point to #.0 
 (one decimal place)

            paintTime.setText(String.valueOf(p));
            String paintTime = String.valueOf(p);

            laborCost.setText(String.valueOf(labCost));
            String laborCost = Double.toString(labCost);
            
            
            


        } catch (NumberFormatException f)
        {
            wallHeight.requestFocus();
            wallHeight.selectAll();
        }
    }

}

class ClearBTNListener implements ActionListener
{

    public void actionPerformed(ActionEvent e)
    {

// clear text fields and set focus
        cansNeeded.setText("");
        gallonsNeeded.setText("");
        wallArea.setText("");
        wallHeight.setText("");
        wallWidth.setText("");
        paintTime.setText("");
        sColor.setText("");
        laborCost.setText("");
        //set focus
        wallHeight.requestFocus();
    }
}

class ChoiceAListener implements ActionListener
{
public void actionPreformed (ActionEvent e)
{ 
    
   

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


    
    PaintEstimator window = new PaintEstimator();
    window.setVisible(true);
    

}

}

Below is the combo box I was able to create:

public static void main(String[] args)
{
    String[] optionsToChoose =
    {
        "White", "Cream", "Sage", "Light Blue", "Eggshell White"
    };

    JFrame jFrame = new JFrame();

    JComboBox<String> jComboBox = new JComboBox<>(optionsToChoose);
    jComboBox.setBounds(80, 50, 140, 20);

    JButton jButton = new JButton("Done");
    jButton.setBounds(100, 100, 90, 20);

    JLabel jLabel = new JLabel();
    jLabel.setBounds(90, 100, 400, 100);

    jFrame.add(jButton);
    jFrame.add(jComboBox);
    jFrame.add(jLabel);

    jFrame.setLayout(null);
    jFrame.setSize(350, 250);
    jFrame.setVisible(true);

    jButton.addActionListener((ActionEvent e) ->
    {
        String selectedColor = "You selected " + 
jComboBox.getItemAt(jComboBox.getSelectedIndex());
        jLabel.setText(selectedColor);
    });

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2025-01-29 17:39:47

首先阅读如何使用组合盒 请浏览一些示例不会受到伤害

从创建ComboBox的实例字段开始,

public class PaintEstimator extends JFrame {
    //...
    private JComboBox<String> colorChoice = new JComboBox<>();
    //...

...接下来,创建一个comboboxModel以保持要呈现的值并替换choicea带有ComboBox ...

mainPanel.add(new JLabel("Select a Color"));
//mainPanel.add(ChoiceA);
// mainPanel.add(sColor);

String[] optionsToChoose = {
            "White", "Cream", "Sage", "Light Blue", "Eggshell White"
        };
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(optionsToChoose);
colorChoice.setModel(model);
mainPanel.add(colorChoice);

然后在您的ActionListener的按钮,获取所选值...

public void actionPerformed(ActionEvent e) {
    try {

        String choosenColor = (String) colorChoice.getSelectedItem();
        System.out.println("choosenColor = " + choosenColor);

可运行的示例...

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

public class Main {

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new PaintEstimator();
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PaintEstimator extends JFrame {

        private JTextField wallHeight = new JTextField(3);
        private JTextField wallWidth = new JTextField(3);
        private JTextField wallArea = new JTextField(3);
        private JTextField gallonsNeeded = new JTextField(3);
        private JTextField cansNeeded = new JTextField(3);
        private JTextField paintTime = new JTextField(3);
        private JTextField sColor = new JTextField(3);
        private JTextField matCost = new JTextField(3);
        private JTextField laborCost = new JTextField(3);

        private JComboBox<String> colorChoice = new JComboBox<>();

        public PaintEstimator() {

            JButton CalcChangeBTN = new JButton("Calculate");
            JButton ClearBTN = new JButton("Clear");

            //ChoiceA.addActionListener(new ChoiceAListener());
            CalcChangeBTN.addActionListener(new CalcChangeBTNListener());
            ClearBTN.addActionListener(new ClearBTNListener());

            wallHeight.setEditable(true);
            wallWidth.setEditable(true);
            wallArea.setEditable(false);
            gallonsNeeded.setEditable(false);
            paintTime.setEditable(false);
            cansNeeded.setEditable(false);
            sColor.setEditable(true);
            matCost.setEditable(false);
            laborCost.setEditable(true);

            JPanel mainPanel = new JPanel();

            mainPanel.setLayout(new GridLayout(16, 3, 0, 0));

            // need cost of paint array to set equations for material cost
            // need combobox arrays for both color and cost
            // vendor selection combo box sets array field for cost/color
            mainPanel.add(new JLabel("Please enter wall height in feet"));

            mainPanel.add(wallHeight);

            mainPanel.add(new JLabel("please enter wall width in feet"));

            mainPanel.add(wallWidth);

            mainPanel.add(new JLabel("wall area"));

            mainPanel.add(wallArea);

            mainPanel.add(new JLabel("Gallons Needed"));

            mainPanel.add(gallonsNeeded);

            mainPanel.add(new JLabel("Number of cans Needed"));
            mainPanel.add(cansNeeded);

            mainPanel.add(new JLabel("Time to paint in Hours"));

            mainPanel.add(paintTime);

            mainPanel.add(new JLabel("Cost of Labor"));
            mainPanel.add(laborCost);

            mainPanel.add(new JLabel("Total Cost of Material"));
            mainPanel.add(matCost);

            mainPanel.add(new JLabel("Select a Color"));

            String[] optionsToChoose = {
                "White", "Cream", "Sage", "Light Blue", "Eggshell White"
            };
            DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(optionsToChoose);
            colorChoice.setModel(model);
            mainPanel.add(colorChoice);
            mainPanel.add(CalcChangeBTN);

            mainPanel.add(ClearBTN);

            setContentPane(mainPanel);

            pack();

            setTitle("Paint Estimator Tool");

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            setLocationRelativeTo(null);
        }

        class CalcChangeBTNListener implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                try {

                    String choosenColor = (String) colorChoice.getSelectedItem();
                    System.out.println("choosenColor = " + choosenColor);

                    final double paintCvrGal = 320.0;
                    final double gallonsPerCan = 1.0;
                    double h = Integer.parseInt(wallHeight.getText());
                    double w = Integer.parseInt(wallWidth.getText());
                    double a = h * w;
                    double c = ((a / paintCvrGal) * gallonsPerCan);
                    double n = (c / gallonsPerCan);
                    double p = (int) ((a * 0.76) / 60);
                    double l = Integer.parseInt(laborCost.getText());
                    double labCost = l * p;

                    // double mc = c * CostofPaint
                    wallArea.setText(String.valueOf(a));
                    String wallArea = String.valueOf(a);

                    gallonsNeeded.setText(String.valueOf(c));
                    String gallonsNeeded = Double.toString(c);

                    cansNeeded.setText(String.valueOf(n));
                    String cansNeeded = Double.toString(n);

                    paintTime.setText(String.valueOf(p));
                    String paintTime = String.valueOf(p);

                    laborCost.setText(String.valueOf(labCost));
                    String laborCost = Double.toString(labCost);

                } catch (NumberFormatException f) {
                    wallHeight.requestFocus();
                    wallHeight.selectAll();
                }
            }

        }

        class ClearBTNListener implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                cansNeeded.setText("");
                gallonsNeeded.setText("");
                wallArea.setText("");
                wallHeight.setText("");
                wallWidth.setText("");
                paintTime.setText("");
                sColor.setText("");
                laborCost.setText("");
                //set focus
                wallHeight.requestFocus();
            }
        }
    }
}

Start by reading through How to Use Combo Boxes (and it wouldn't hurt to look over some the examples)

Start by creating an instance field for the combobox...

public class PaintEstimator extends JFrame {
    //...
    private JComboBox<String> colorChoice = new JComboBox<>();
    //...

Next, create a ComboBoxModel to hold the values you want to present and replace the ChoiceA button with the combobox...

mainPanel.add(new JLabel("Select a Color"));
//mainPanel.add(ChoiceA);
// mainPanel.add(sColor);

String[] optionsToChoose = {
            "White", "Cream", "Sage", "Light Blue", "Eggshell White"
        };
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(optionsToChoose);
colorChoice.setModel(model);
mainPanel.add(colorChoice);

And then in your ActionListener, get the selected value...

public void actionPerformed(ActionEvent e) {
    try {

        String choosenColor = (String) colorChoice.getSelectedItem();
        System.out.println("choosenColor = " + choosenColor);

Runnable example...

enter image description here

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

public class Main {

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new PaintEstimator();
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PaintEstimator extends JFrame {

        private JTextField wallHeight = new JTextField(3);
        private JTextField wallWidth = new JTextField(3);
        private JTextField wallArea = new JTextField(3);
        private JTextField gallonsNeeded = new JTextField(3);
        private JTextField cansNeeded = new JTextField(3);
        private JTextField paintTime = new JTextField(3);
        private JTextField sColor = new JTextField(3);
        private JTextField matCost = new JTextField(3);
        private JTextField laborCost = new JTextField(3);

        private JComboBox<String> colorChoice = new JComboBox<>();

        public PaintEstimator() {

            JButton CalcChangeBTN = new JButton("Calculate");
            JButton ClearBTN = new JButton("Clear");

            //ChoiceA.addActionListener(new ChoiceAListener());
            CalcChangeBTN.addActionListener(new CalcChangeBTNListener());
            ClearBTN.addActionListener(new ClearBTNListener());

            wallHeight.setEditable(true);
            wallWidth.setEditable(true);
            wallArea.setEditable(false);
            gallonsNeeded.setEditable(false);
            paintTime.setEditable(false);
            cansNeeded.setEditable(false);
            sColor.setEditable(true);
            matCost.setEditable(false);
            laborCost.setEditable(true);

            JPanel mainPanel = new JPanel();

            mainPanel.setLayout(new GridLayout(16, 3, 0, 0));

            // need cost of paint array to set equations for material cost
            // need combobox arrays for both color and cost
            // vendor selection combo box sets array field for cost/color
            mainPanel.add(new JLabel("Please enter wall height in feet"));

            mainPanel.add(wallHeight);

            mainPanel.add(new JLabel("please enter wall width in feet"));

            mainPanel.add(wallWidth);

            mainPanel.add(new JLabel("wall area"));

            mainPanel.add(wallArea);

            mainPanel.add(new JLabel("Gallons Needed"));

            mainPanel.add(gallonsNeeded);

            mainPanel.add(new JLabel("Number of cans Needed"));
            mainPanel.add(cansNeeded);

            mainPanel.add(new JLabel("Time to paint in Hours"));

            mainPanel.add(paintTime);

            mainPanel.add(new JLabel("Cost of Labor"));
            mainPanel.add(laborCost);

            mainPanel.add(new JLabel("Total Cost of Material"));
            mainPanel.add(matCost);

            mainPanel.add(new JLabel("Select a Color"));

            String[] optionsToChoose = {
                "White", "Cream", "Sage", "Light Blue", "Eggshell White"
            };
            DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(optionsToChoose);
            colorChoice.setModel(model);
            mainPanel.add(colorChoice);
            mainPanel.add(CalcChangeBTN);

            mainPanel.add(ClearBTN);

            setContentPane(mainPanel);

            pack();

            setTitle("Paint Estimator Tool");

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            setLocationRelativeTo(null);
        }

        class CalcChangeBTNListener implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                try {

                    String choosenColor = (String) colorChoice.getSelectedItem();
                    System.out.println("choosenColor = " + choosenColor);

                    final double paintCvrGal = 320.0;
                    final double gallonsPerCan = 1.0;
                    double h = Integer.parseInt(wallHeight.getText());
                    double w = Integer.parseInt(wallWidth.getText());
                    double a = h * w;
                    double c = ((a / paintCvrGal) * gallonsPerCan);
                    double n = (c / gallonsPerCan);
                    double p = (int) ((a * 0.76) / 60);
                    double l = Integer.parseInt(laborCost.getText());
                    double labCost = l * p;

                    // double mc = c * CostofPaint
                    wallArea.setText(String.valueOf(a));
                    String wallArea = String.valueOf(a);

                    gallonsNeeded.setText(String.valueOf(c));
                    String gallonsNeeded = Double.toString(c);

                    cansNeeded.setText(String.valueOf(n));
                    String cansNeeded = Double.toString(n);

                    paintTime.setText(String.valueOf(p));
                    String paintTime = String.valueOf(p);

                    laborCost.setText(String.valueOf(labCost));
                    String laborCost = Double.toString(labCost);

                } catch (NumberFormatException f) {
                    wallHeight.requestFocus();
                    wallHeight.selectAll();
                }
            }

        }

        class ClearBTNListener implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                cansNeeded.setText("");
                gallonsNeeded.setText("");
                wallArea.setText("");
                wallHeight.setText("");
                wallWidth.setText("");
                paintTime.setText("");
                sColor.setText("");
                laborCost.setText("");
                //set focus
                wallHeight.requestFocus();
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文