如何使用子类中的方法填充GUI文本。

时间:2016-09-16 02:43:53

标签: java user-interface methods jlabel jtextfield

我正在尝试填充我的gui盒子,当我运行命令gui符合时它给了我一个选择所有者的选项。我正在尝试制作一个gui,当选择所有者时,它会进行计算并填充该字段。

PropertyGUI

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.Scanner;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.*;


 public class PropertyGUI extends JFrame
{
    private JLabel taxlabel;
    private JLabel finishButtonLabel;
    private JComboBox<String> jCombo;
    private  String [] comboList = { "None","Jones","Smith","Willis","Traver","Merrit"};
    private JTextField marketValueLabel;
    private JTextField  taxlabelField;
    private JButton     finishButton;
    private ButtonGroup yesNoButton;
    private JPanel  buttonPanel;
    private JLabel ownerLabel;
    private JLabel marketValue;
    private JLabel floodZone;
    private JLabel exemption;
    private JRadioButton yesButton;
    private JRadioButton noButton;
    private JTextField  exemptionField;

    ArrayList <Property> propertyList = new ArrayList<Property>();   

    ArrayList <String> ownerList = new ArrayList<String>();

    double calTax;



    public PropertyGUI()
        {
                super("Property Input Screen");
                setLayout(new GridLayout(6,2));
                readFile();

                ownerLabel          = new JLabel (" Select an Owner");
                jCombo              = new JComboBox<String>(comboList);
                marketValueLabel    = new JTextField(20);
                yesButton           = new JRadioButton(" Yes");
                noButton            = new JRadioButton(" No");
                marketValue         = new JLabel(" Total Market Value");
                floodZone           = new JLabel (" Flood Zone");
                exemption           = new JLabel (" Total Exemptions");
                taxlabel            = new JLabel (" Total Taxes");
                finishButtonLabel   = new JLabel (" Click When Done");
                exemptionField      = new JTextField(20);
                taxlabelField       = new JTextField(20);
                finishButton        = new JButton (" FINISH");
                buttonPanel         = new JPanel ();
                yesNoButton         = new ButtonGroup();

                yesNoButton.add(yesButton);
                yesNoButton.add(noButton);
                buttonPanel.add(yesButton);
                buttonPanel.add(noButton);

                MyHandler actionHandler = new MyHandler(); 
                finishButton.addActionListener(actionHandler);



                add(ownerLabel);
                add(jCombo);
                add(marketValue);
                add(marketValueLabel);
                add(floodZone);
                add(buttonPanel);
                add(exemption);
                add(exemptionField);
                add(taxlabel);
                add(taxlabelField);
                add(finishButtonLabel);
                add(finishButton);

        }


public ArrayList<Property> readFile()

{   

    Address propertyAddress;
    double marketValue;
    int squareFeet;
    boolean floodZone;
    String businessName;
    String stateCode;
    String sub;
    ResidentialProperty.Subdivision subDivision;

try
    {   


        Scanner read= new Scanner(new File("properties.txt"));
        while (read.hasNextLine())
        {
            String text = read.nextLine();
            String[] input = text.split(";");


            if (input[0].equals("CommercialProperty"))
            {   
                ownerList.add(input[1]);    
                propertyAddress = new Address(input[2], input[3], input[4],Integer.parseInt(input[5]));
                marketValue = Double.parseDouble(input[6]);
                squareFeet = Integer.parseInt(input[7]);
                floodZone = Boolean.parseBoolean(input[8]);
                businessName = input[9];
                stateCode = input[10];


                propertyList.add( new CommercialProperty(propertyAddress, marketValue, squareFeet, floodZone, businessName, stateCode));

            }
        else
            {           
                ownerList.add(input[1]);    
                propertyAddress = new Address(input[2], input[3], input[4],Integer.parseInt(input[5]));
                marketValue = Double.parseDouble(input[6]);
                squareFeet = Integer.parseInt(input[7]);
                floodZone = Boolean.parseBoolean(input[8]);
                sub = input[9];

                if (sub.equals("NONE"))
                    subDivision = ResidentialProperty.Subdivision.NONE;
                else if (sub.equals("KINGLY_ESTATES"))
                    subDivision = ResidentialProperty.Subdivision.KINGLY_ESTATES;
                else if (sub.equals("STATELY_ESTATES"))
                    subDivision = ResidentialProperty.Subdivision.STATELY_ESTATES;
                else if (sub.equals("KING_MANOR"))
                    subDivision = ResidentialProperty.Subdivision.KING_MANOR;

                else
                    subDivision = ResidentialProperty.Subdivision.GREEN_GABLES;

                propertyList.add(new ResidentialProperty(propertyAddress, marketValue, squareFeet, floodZone, subDivision));

            }

            }   
    }

    catch (IOException ioe)
            {
                System.out.println(ioe);
            }
    return  

        propertyList;
    }


private class MyHandler implements ActionListener
{


            public void actionPerformed(ActionEvent ae)
            {

                for (String a: ownerList) 

                    for (Property s: propertyList) 
                        {                   
                            marketValue = CommercialProperty.setText(calculateTaxes());

                        }
                System.exit(0);

            }
        }

}

计算税收方法是我试图用来填充GUI字段,它可以填充任何字段,如果有人可以指导我,我可以修复哪一个去哪里。

        public class CommercialProperty extends Property
{

    private String businessName;
    private String stateCode;

public CommercialProperty()
    {
    super();
    setBusinessName("");
    setStateCode("");

        }
public CommercialProperty(Address propertyAddress,double marketValue,int squareFeet, boolean floodZone,String businessName,String stateCode)
{
    super (propertyAddress,marketValue,squareFeet,floodZone);
    setBusinessName(businessName);
    setStateCode(stateCode);
}   

public void setBusinessName(String businessName)
{
    this.businessName = businessName;
}

public void setStateCode(String stateCode)
{
    this.stateCode = stateCode;
}

public String getBusinessName()
{
        return businessName;
}

public String getStateCode()
{
        return stateCode;
}

public String toString()
{
        return(super.toString()+"BusinessName" + businessName + "StateCode" + stateCode);
}

public double calculateTaxes()
{
    double tax;
    if ((stateCode.equals("TX")) && getSquareFeet() > 1500)
        tax = getMarketValue()*0.25;
    else if (getSquareFeet() <= 1500 )
        tax = getMarketValue() * 0.10;
    else 
        tax = getMarketValue() * 0.20;
    return tax;

}


    }

还有几个课程,但如果有人可以帮助我如何填充一个我可以在其余部分工作。

0 个答案:

没有答案