项目侦听器不影响代码中的值

时间:2021-02-12 02:00:02

标签: java

package guidemop6;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;

/**
*
* AWT Swing JavaFx
*/
public class GUIDemop6 extends JFrame implements ItemListener,ActionListener
{
//Labels


double propertyValueHolder;
double totalTax;
double oldTax;
double propertyTax;
double tax;


JLabel propValue = new JLabel("Enter your property value here:");
JLabel SchDistrict = new JLabel("Which school district do you live in:");
JLabel areaLive = new JLabel("Which type of building do you live in:");


JPanel pnl1 = new JPanel();
JPanel grid = new JPanel(new GridLayout(9,1));
JPanel popValue = new JPanel();
JPanel chk1_1 = new JPanel();
JPanel chk2_1 = new JPanel();
JPanel chk3_1 = new JPanel();
JTextField propertyValue = new JTextField(20);



//data - selections
String[] howOld = {"Are you older than 65?(leave blank if answer is no)"};
String[] whichSchoolDistrict = {"NEISD","NSISD","SSISD"};
String[] whereLive = {"Residential","Commercial","Multi-Family"};
//create checkboxes
JCheckBox oldChk = new JCheckBox(howOld[0]);
JRadioButton schChk1 = new JRadioButton(whichSchoolDistrict[0]);
JRadioButton schChk2 = new JRadioButton(whichSchoolDistrict[1]);
JRadioButton schChk3 = new JRadioButton(whichSchoolDistrict[2]);
JRadioButton livChk1 = new JRadioButton(whereLive[0]);
JRadioButton livChk2 = new JRadioButton(whereLive[1]);
JRadioButton livChk3 = new JRadioButton(whereLive[2]);



ButtonGroup drinkGroup = new ButtonGroup();

//JTextField

JTextField money = new JTextField(20);
// // JTextArea handle mulitple rows of data
JTextArea output = new JTextArea(10, 30);
JScrollPane pane1 = new JScrollPane(output);
JButton GoButton = new JButton("Calulate Tax");



//constructor
public GUIDemop6(){
super("Property Tax Calculator"); // sets the title in the title bar of your program
setDefaultCloseOperation(EXIT_ON_CLOSE); // determines what happens when the X or red button (mac) are clicked on
setSize(811, 300); //sets the size of our window width , height

output.setLineWrap(true);
output.setWrapStyleWord(true);
pane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);


oldChk.addItemListener(this);
schChk1.addItemListener(this);
schChk2.addItemListener(this);
schChk3.addItemListener(this);
livChk1.addItemListener(this);
livChk2.addItemListener(this);
livChk3.addItemListener(this);

GoButton.addActionListener(this);


ButtonGroup radGroup1 = new ButtonGroup();
radGroup1.add(schChk1);
radGroup1.add(schChk2);
radGroup1.add(schChk3);
ButtonGroup radGroup2 = new ButtonGroup();
radGroup2.add(livChk1);
radGroup2.add(livChk2);
radGroup2.add(livChk3);


chk1_1.add(oldChk);
popValue.add(propValue);
popValue.add(propertyValue);
grid.add(popValue);

chk2_1.add(SchDistrict);
chk2_1.add(schChk1);
chk2_1.add(schChk2);
chk2_1.add(schChk3);

chk3_1.add(areaLive);
chk3_1.add(livChk1);
chk3_1.add(livChk2);
chk3_1.add(livChk3);

grid.add(chk2_1);
grid.add(chk3_1);
grid.add(chk1_1);


grid.add(GoButton);
grid.add(money);
add(grid);


setVisible(true);
}


/* create a GUIDemo6 and call it myWindow */

public static void main(String[] args) {
GUIDemop6 myWindow = new GUIDemop6();
}
    @Override
    public void itemStateChanged(ItemEvent ie) {
        
        totalTax = Double.parseDouble(propertyValue.getText());
        
        tax = 0.06;
        totalTax = totalTax * tax;
        if((ie.getItemSelectable() == schChk1) && (ie.getStateChange() == ItemEvent.SELECTED))
        {
            totalTax = totalTax * 1.0055;
        }
        if((ie.getItemSelectable() == schChk2) && (ie.getStateChange() == ItemEvent.SELECTED))
        {
            totalTax = totalTax * 1.005;
        }
        if((ie.getItemSelectable() == schChk3) && (ie.getStateChange() == ItemEvent.SELECTED))
        {
            totalTax = totalTax * 1.0025;
        }
        if((ie.getItemSelectable() == schChk1) && (ie.getStateChange() == ItemEvent.SELECTED))
        {
            totalTax = totalTax * 1.0005;
        }
        if((ie.getItemSelectable() == schChk2) && (ie.getStateChange() == ItemEvent.SELECTED))
        {
            totalTax = totalTax * 1.0007;
        }
        if((ie.getItemSelectable() == schChk3) && (ie.getStateChange() == ItemEvent.SELECTED))
        {
            totalTax = totalTax * 1.0006;
        }
        if((ie.getItemSelectable() == oldChk) && (ie.getStateChange() == ItemEvent.SELECTED))
        {
            totalTax = totalTax/2;
        }
        
        
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(totalTax);
    }

我试图让项目侦听器影响输出,但它们什么也没做。只是对这个问题有一点心理障碍。例如,我会将值设置为 160000,每次无论我选择哪个单选按钮,它都会输出 9600 或 4800,具体取决于我是否选择了复选框。我需要一些帮助,因为我已尝试解决问题,但似乎没有任何效果。我正在使用 netbeans,但在这个问题上有点卡住了,我想在今晚之前完成。

0 个答案:

没有答案
相关问题