java - 单击“清除”按钮后第二次尝试输入数据无法生成输出(Java GUI)

标签 java swing user-interface jpanel

现在,在我在源代码中进行了此处和那里的更正后,程序运行良好。不知何故,如果我想在使用按钮[清除]清除前一个客户的数据后为下一个客户输入,当我单击[收据]按钮再次收据面板上的输出只是显示我在 isSelected() block 之外编写的 toString 语句。我应该做什么来恢复错误(因为我认为它被视为无法显示的错误下一个输出)

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.text.*;
 import java.text.DecimalFormat;
 import javax.swing.border.*;
 import java.awt.GridLayout;
 import java.awt.FlowLayout;
 import java.awt.BorderLayout;
 import javax.swing.BoxLayout;

public class TestP extends JFrame implements ActionListener
{


//var for UPSR
private JTextField txtUh1, txtUh2, txtUh3, txtUh4, txtUh5,txtUt1, txtUt2,   txtUt3, txtUt4, txtUt5,txtTot;
private JLabel lblU1, lblU2, lblU3;
private JCheckBox cbUbm1,cbUbm2, cbUbi,cbUmath,cbUsc;


//var for PT3
 private JTextField txtPhbm,txtPhbi,txtPhm3,txtPhsc,txtPhag,txtPhsj,txtPhkh;
 private JTextField txtPtbm,txtPtbi,txtPtm3,txtPtsc,txtPtag,txtPtsj,txtPtkh;
 private JCheckBox cbPbm,cbPbi,cbPm3,cbPsc,cbPag,cbPsj,cbPkh;
 private JLabel lblP4,lblP5,lblP6;  // lblP1,lblP2,lblP3,lblP7,lblP8,lblP9
 private JTextField txtname,txtage,txtcontact;
 private JLabel lblname,lblage,lblcontact;

//var for SPM

private JTextField txtSprice, txtShbm, txtShbi, txtShsc, txtShag, txtShm3, txtShac, txtShpd, txtShaddm3, txtShcm, txtShbio, txtShfz, txtShsej, txtStbm, txtStbi, txtStsc, txtStag, txtStm3, txtStac, txtStpd, txtStaddm3, txtStcm, txtStbio, txtStfz, txtStsej;
private JLabel lblS1, lblS2, lblS3, lblS4;
private JCheckBox cbSbm, cbSbi, cbSsc, cbSag, cbSm3, cbSac, cbSpd, cbSaddm3, cbScm, cbSbio, cbSfz, cbSsej;
private JRadioButton rbUpsr,rbPt3,rbSpm;

private JButton btnReceipt = new JButton("RECEIPT");
private JButton btnClear = new JButton("CLEAR");
private JButton btnExit = new JButton("EXIT"); 

//button group for level
private ButtonGroup rbGroup = new ButtonGroup();

//Text Area for calculation
private JTextArea txtReceipt = new JTextArea("Super Tuition Center Sdn Bhd\t\t" + "\n"); 
//layout fixed size
private static final int width = 1350;
private static final int height = 730; 
Container pane = getContentPane();
Color c = new Color(217, 161, 63);
public TestP()
{
rbUpsr = new JRadioButton("UPSR");
rbPt3 = new JRadioButton("PT-3");
rbSpm = new JRadioButton("SPM");

   lblU1 = new JLabel("SUBJECT");
   lblU2 = new JLabel("HOUR/WEEK");
   lblU3 = new JLabel("TIME/WEEK");

   txtUh1 = new JTextField(10);
   txtUh2 = new JTextField(10);
   txtUh3 = new JTextField(10);
   txtUh4 = new JTextField(10);
   txtUh5 = new JTextField(10);
   txtUt1 = new JTextField(10);
   txtUt2 = new JTextField(10);
   txtUt3 = new JTextField(10);
   txtUt4 = new JTextField(10);
   txtUt5 = new JTextField(10);
   txtTot = new JTextField(10);

   cbUbm1 = new JCheckBox("BAHASA MELAYU PEMAHAMAN");
   cbUbm2 = new JCheckBox("BAHASA MELAYU PENULISAN");
   cbUbi = new JCheckBox("BAHASA INGGERIS");
   cbUmath = new JCheckBox("MATEMATIK");
   cbUsc = new JCheckBox("SAINS");


   txtPhbm=new JTextField(10);
    txtPhbi=new JTextField(10);
    txtPhm3=new JTextField(10);
    txtPhsc=new JTextField(10);
    txtPhag=new JTextField(10);
    txtPhsj=new JTextField(10);
    txtPhkh=new JTextField(10);
    txtPtbm=new JTextField(10);
    txtPtbi=new JTextField(10);
    txtPtm3=new JTextField(10);
    txtPtsc=new JTextField(10);
    txtPtag=new JTextField(10);
    txtPtsj=new JTextField(10);
    txtPtkh=new JTextField(10);

    lblP4=new JLabel("SUBJECT ");
    lblP5=new JLabel("HOUR/WEEK");
    lblP6=new JLabel("TIME/WEEK");

    cbPbm=new JCheckBox("BAHASA MELAYU :");
    cbPbi=new JCheckBox("BAHASA INGGERIS :");
    cbPm3=new JCheckBox("MATEMATIK :");
    cbPsc=new JCheckBox("SAINS :");
    cbPag=new JCheckBox("PENDIDIKAN ISLAM :");
    cbPsj=new JCheckBox("SEJARAH :");
    cbPkh=new JCheckBox("KEMAHIRAN HIDUP :");

     lblS1 = new JLabel("SUBJECT ");
    lblS2 = new JLabel("HOUR/WEEK");
    lblS3 = new JLabel("TIME/WEEK");


    txtShbm = new JTextField(10);
    txtShbi = new JTextField(10);
    txtShsc = new JTextField(10);
    txtShag = new JTextField(10);
    txtShm3 = new JTextField(10);
    txtShac = new JTextField(10);
    txtShpd = new JTextField(10);
    txtShaddm3 = new JTextField(10);
    txtShcm = new JTextField(10);
    txtShbio = new JTextField(10);
    txtShfz = new JTextField(10);
    txtShsej = new JTextField(10);
    txtSprice = new JTextField(10);
    txtStbm = new JTextField(10);
    txtStbi = new JTextField(10);
    txtStsc = new JTextField(10);
    txtStag = new JTextField(10);
    txtStm3 = new JTextField(10);
    txtStac = new JTextField(10);
    txtStpd = new JTextField(10);
    txtStaddm3 = new JTextField(10);
    txtStcm = new JTextField(10);
    txtStbio = new JTextField(10);
    txtStfz = new JTextField(10);
    txtStsej = new JTextField(10);

    lblname=new JLabel("NAME : ");
    lblage=new JLabel("AGE : ");
    lblcontact=new JLabel("CONTACT NO : ");
    txtname=new JTextField(30);
    txtage=new JTextField(5);
    txtcontact=new JTextField(15);

    cbSbm = new JCheckBox("BAHASA MELAYU");
    cbSbi = new JCheckBox("BAHASA INGGERIS");
    cbSsc = new JCheckBox("SAINS");
    cbSag = new JCheckBox("PENDIDIKAN ISLAM");
    cbSm3 = new JCheckBox("MATEMATIK");
    cbSac = new JCheckBox("AKAUN");
    cbSpd = new JCheckBox("PERDAGANGAN");
    cbSaddm3 = new JCheckBox("MATEMATIK TAMBAHAN");
    cbScm = new JCheckBox("KIMIA");
    cbSbio = new JCheckBox("BIOLOGI");
    cbSfz = new JCheckBox("FIZIK");
    cbSsej = new JCheckBox("SEJARAH");





   //setting for main panel
   setTitle("Super Tuition");
   setSize(width, height);

   setVisible(true);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   JPanel p1 = new JPanel();
   p1.setLayout(new GridLayout(7,3));
   p1.setBorder(new TitledBorder("UPSR"));
   p1.setBackground(Color.pink);
   //p1.setSize(400,400);
   p1.add(lblU1);
   lblU1.setForeground(Color.BLACK);
   p1.add(lblU2);
   lblU2.setForeground(Color.BLACK);
   p1.add(lblU3);
   lblU3.setForeground(Color.BLACK);

   p1.add(cbUbm1);
   cbUbm1.setBackground(Color.YELLOW);
   p1.add(txtUh1);
   p1.add(txtUt1);

   p1.add(cbUbm2);
   cbUbm2.setBackground(Color.YELLOW);
   p1.add(txtUh2);
   p1.add(txtUt2);

   p1.add(cbUbi);
   cbUbi.setBackground(Color.YELLOW);
   p1.add(txtUh3);
   p1.add(txtUt3);

   p1.add(cbUmath);
   cbUmath.setBackground(Color.YELLOW);
   p1.add(txtUh4);
   p1.add(txtUt4);

   p1.add(cbUsc);
   cbUsc.setBackground(Color.YELLOW);
   p1.add(txtUh5);
   p1.add(txtUt5);


   //add components for PT-3
   JPanel p2 = new JPanel();
   p2.setLayout(new GridLayout(11,3));
   p2.setBorder(new TitledBorder("PT-3"));
   p2.setBackground(Color.PINK);
   p2.setSize(400,400);

   p2.add(lblP4);
   lblP4.setForeground(Color.BLACK);
   p2.add(lblP5);
   p2.add(lblP6);

   p2.add(cbPbm);
   cbPbm.setBackground(Color.YELLOW);
   p2.add(txtPhbm);
   p2.add(txtPtbm);

   p2.add(cbPbi);
   cbPbi.setBackground(Color.YELLOW);
   p2.add(txtPhbi);
   p2.add(txtPtbi);

   p2.add(cbPm3);
   cbPm3.setBackground(Color.YELLOW);
   p2.add(txtPhm3);
   p2.add(txtPtm3);

   p2.add(cbPsc);
   cbPsc.setBackground(Color.YELLOW);
   p2.add(txtPhsc);
   p2.add(txtPtsc);

   p2.add(cbPag);
   cbPag.setBackground(Color.YELLOW);
   p2.add(txtPhag);
   p2.add(txtPtag);

   p2.add(cbPsj);
   cbPsj.setBackground(Color.YELLOW);
   p2.add(txtPhsj);
   p2.add(txtPtsj);

   p2.add(cbPkh);
   cbPkh.setBackground(Color.YELLOW);
   p2.add(txtPhkh);
   p2.add(txtPtkh);


   //add components for SPM 
   JPanel p3 = new JPanel();
   p3.setLayout(new GridLayout(15,3));
   p3.setBorder(new TitledBorder("SPM"));
   p3.setBackground(Color.PINK);


   p3.add(lblS1);
   lblS1.setForeground(Color.BLACK);
   p3.add(lblS2);
   lblS1.setForeground(Color.BLACK);
   p3.add(lblS3);
   lblS1.setForeground(Color.BLACK);

   p3.add(cbSbm);
   cbSbm.setBackground(Color.YELLOW);
   p3.add(txtShbm);
   p3.add(txtStbm);

   p3.add(cbSbi);
   cbSbi.setBackground(Color.YELLOW);
   p3.add(txtShbi);
   p3.add(txtStbi);

   p3.add(cbSsej);
   cbSsej.setBackground(Color.YELLOW);
   p3.add(txtShsej);
   p3.add(txtStsej);

   p3.add(cbSag);
   cbSag.setBackground(Color.YELLOW);
   p3.add(txtShag);
   p3.add(txtStag);

   p3.add(cbSm3);
   cbSm3.setBackground(Color.YELLOW);
   p3.add(txtShm3);
   p3.add(txtStm3);

   p3.add(cbSaddm3);
   cbSaddm3.setBackground(Color.YELLOW);
   p3.add(txtShaddm3);
   p3.add(txtStaddm3);

   p3.add(cbSsc);
   cbSsc.setBackground(Color.YELLOW);
   p3.add(txtShsc);
   p3.add(txtStsc);

   p3.add(cbScm);
   cbScm.setBackground(Color.YELLOW);
   p3.add(txtShcm);
   p3.add(txtStcm);

   p3.add(cbSfz);
   cbSfz.setBackground(Color.YELLOW);
   p3.add(txtShfz);
   p3.add(txtStfz);

   p3.add(cbSbio);
   cbSbio.setBackground(Color.YELLOW);
   p3.add(txtShbio);
   p3.add(txtStbio);

   p3.add(cbSac);
   cbSac.setBackground(Color.YELLOW);
   p3.add(txtShac);
   p3.add(txtStac);

   p3.add(cbSpd);
   cbSpd.setBackground(Color.YELLOW);
   p3.add(txtShpd);
   p3.add(txtStpd);


   //add components for Button Panel
   JPanel p4 = new JPanel();
   p4.setLayout(new GridLayout(11,3));
   p4.setBorder(new TitledBorder("RECEIPT"));
   p4.setBackground(Color.red);




   //add components for Main panel

   JPanel p5 = new JPanel();

    setBackground(Color.orange);

   p5.add(lblname);

   p5.add(txtname);

   p5.add(lblage);

   p5.add(txtage);

   p5.add(lblcontact);

   p5.add(txtcontact);

   p5.add(btnReceipt);

   btnReceipt.addActionListener(this);

   p5.add(btnClear);

   btnClear.addActionListener(this);

   p5.add(btnExit);

   btnExit.addActionListener(this);

   p5.setBackground(Color.ORANGE);
   rbGroup.add(rbUpsr);
   p5.add(rbUpsr);
   rbUpsr.setBackground(Color.ORANGE);
   rbUpsr.addActionListener(this);

   rbGroup.add(rbPt3);
   p5.add(rbPt3);
   rbPt3.setBackground(Color.ORANGE);
   rbPt3.addActionListener(this);

   rbGroup.add(rbSpm);
   p5.add(rbSpm);
   rbSpm.setBackground(Color.ORANGE);
   rbSpm.addActionListener(this);



   JPanel receipt = new JPanel();

   receipt.setBorder( new TitledBorder("TOTAL PAYMENT RECEIPT"));
   receipt.setBackground(Color.pink);
   receipt.add(txtReceipt); 

   txtReceipt.setFont(new Font("Serif", Font.BOLD, 13));
   //end panel receipt 

   pane.setLayout(new BorderLayout());
   pane.add(p1,"South");
   pane.add(p2,"Center");
   pane.add(p3,"West");
   pane.add(p5,"North");
   pane.add(receipt,"East"); // end outermost panel
  }
  public void actionPerformed(ActionEvent event)
  {


    if (event.getSource() == btnReceipt)
    {
        double totD=0.0,totalS=0.0,totalU=0.0,totalP=0.0;
        DecimalFormat df = new DecimalFormat("0.00");
        double totP=0.0;
        txtReceipt.append("\n");
        txtReceipt.append("\t\tWELCOME TO SUPER TUITION CENTER SDN BHD" + "\n");
        txtReceipt.append("\n");
        txtReceipt.append("CUSTOMER'S NAME:" +txtname.getText()+"\n");
        txtReceipt.append("AGE\t              :" +txtage.getText()+"\n");
        txtReceipt.append("CONTACT NO             :" +txtcontact.getText()+"\n");
        txtReceipt.append("\n");
        txtReceipt.append("SUBJECTS :         " + "\t\t PRICE"+ "\tHOUR"+ "\tTIME" + "\tTOTAL" + "\n");
        txtReceipt.append("\n");
        if(rbUpsr.isSelected()){
       int countU=0;
       double totbm1=0.0,totbm2=0.0,totbi=0.0,totmath=0.0,totsc=0.0;

        if (cbUbm1.isSelected())
        {
            int hrbm1 = Integer.parseInt(txtUh1.getText());
            int tmbm1 = Integer.parseInt(txtUt1.getText());
            totbm1=20*hrbm1*tmbm1;
            countU++;
            txtReceipt.append("BAHASA MELAYU PEMAHAMAN " + "\tRM 20.00" + "\t" + hrbm1 + "\t"+tmbm1+"\t"+ df.format( totbm1) + "\n"); // set to text area
        }
        if (cbUbm2.isSelected())
        {
            int hrbm2 = Integer.parseInt(txtUh2.getText());
            int tmbm2 = Integer.parseInt(txtUt2.getText());
            totbm2=20*hrbm2*tmbm2;
            countU++;
            txtReceipt.append("BAHASA MELAYU PENULISAN " + "\tRM 20.00" + "\t" + hrbm2 + "\t" +tmbm2+"\t"+ df.format( totbm2) + "\n"); // set to text area
        }
        if (cbUbi.isSelected())
        {
            int hrbi = Integer.parseInt(txtUh3.getText());
            int tmbi = Integer.parseInt(txtUt3.getText());
            totbi=20*hrbi*tmbi;
            countU++;
            txtReceipt.append("BAHASA INGGERIS          " + "\t\tRM 20.00" + "\t" + hrbi + "\t" +tmbi+"\t"+ df.format( totbi) + "\n"); // set to text area
        }
        if (cbUmath.isSelected())
        {
            int hrmath = Integer.parseInt(txtUh4.getText());
            int tmmath= Integer.parseInt(txtUt4.getText());
            totmath=20*hrmath*tmmath;
            countU++;
            txtReceipt.append("MATEMATIK                " + "\t\tRM 20.00" +"\t"+ hrmath + "\t" +tmmath+"\t"+ df.format( totmath) + "\n"); // set to text area
        }
        if (cbUsc.isSelected())
        {
            int hrsc = Integer.parseInt(txtUh5.getText());
            int tmsc = Integer.parseInt(txtUt5.getText());
            totsc=20*hrsc*tmsc;
            txtReceipt.append("SAINS                    " + "\t\tRM 20.00" +"\t" + hrsc + "\t" +tmsc+"\t"+ df.format( totsc) + "\n"); // set to text area
            countU++;
        }
        totalU=totbm1+totbm2+totbi+totmath+totsc;
        if(countU>5){
            totD=totalU*0.1;
           totP=totalU-totD;
        }
        else{
            totP=totalU;
        }
        txtReceipt.append("TOTAL FEES : " + "\t\t" + "RM" +totalU+"0" + "\n");
    }

      if(rbPt3.isSelected()){

     double pbm=0.0,pbi=0.0,pm3=0.0,psc=0.0,pag=0.0,psj=0.0,pkh=0.0;

     int countP=0;

         if(cbPbm.isSelected())
        {   int textPhbm=Integer.parseInt(txtPhbm.getText());
            int textPtbm=Integer.parseInt(txtPtbm.getText());
            pbm=35*textPhbm*textPtbm;
            countP++;
            txtReceipt.append("BAHASA MELAYU            " + "\t\tRM 35.00" + "\t  " + textPhbm + "\t  " +textPtbm+ "\t  "+df.format(pbm) + "\n"); // set to text area
        }
        if(cbPbi.isSelected())
        {   int textPhbi=Integer.parseInt(txtPhbi.getText());
            int textPtbi=Integer.parseInt(txtPtbi.getText());
            pbi=35*textPhbi*textPtbi;
            countP++;
            txtReceipt.append("BAHASA INGGERIS          " + "\t\tRM 35.00" + "\t  " + textPhbi + "\t  " +textPtbi+"\t  "+ df.format(pbi) + "\n"); // set to text area
        }
        if(cbPm3.isSelected())
        {   int textPhm3=Integer.parseInt(txtPhm3.getText());
            int textPtm3=Integer.parseInt(txtPtm3.getText());
            pm3=35*textPhm3*textPtm3;
            countP++;
            txtReceipt.append("MATEMATIK                " + "\t\tRM 35.00" + "\t  " + textPhm3 + "\t  " +textPtm3+"\t  "+ df.format(pm3) + "\n"); // set to text area
        }
        if(cbPsc.isSelected())
        {   int textPhsc=Integer.parseInt(txtPhsc.getText());
            int textPtsc=Integer.parseInt(txtPtsc.getText());
            psc=35*textPhsc*textPtsc;
            countP++;
            txtReceipt.append("SAINS                    " + "\t\tRM 35.00" + "\t  " + textPhsc + "\t  " +textPtsc+ "\t  "+ df.format(psc) + "\n"); // set to text area
        }
        if(cbPag.isSelected())
        {   int textPhag=Integer.parseInt(txtPhag.getText());
            int textPtag=Integer.parseInt(txtPtag.getText());
            pag=35*textPhag*textPtag;
            countP++;
            txtReceipt.append("PENDIDIKAN ISLAM         " + "\t\tRM 35.00" + "\t  " + textPhag + "\t  " +textPtag+ "\t  "+ df.format(pag) + "\n"); // set to text area
        }
        if(cbPsj.isSelected())
        {   int textPhsj=Integer.parseInt(txtPhsj.getText());
            int textPtsj=Integer.parseInt(txtPtsj.getText());
            psj=35*textPhsj*textPtsj;
            countP++;
            txtReceipt.append("SEJARAH                  " + "\t\tRM 35.00" + "\t  " + textPhsj + "\t  " +textPtsj+ "\t  "+ df.format(psj) + "\n"); // set to text area
        }
        if(cbPkh.isSelected())
        {   int textPhkh=Integer.parseInt(txtPhkh.getText());
            int textPtkh=Integer.parseInt(txtPtkh.getText());
            pkh=35*textPhkh*textPtkh;
            countP++;
            txtReceipt.append("KEMAHIRAN HIDUP          " + "\t\tRM 35.00" + "\t  " + textPhkh + "\t  " +textPtkh+ "\t  "+ df.format(pkh) + "\n"); // set to text area
        }

        totalP=pbm+pbi+pm3+psc+pag+psj+pkh;

        if(countP>=6)
        {
            totD=totalP*0.1;
            totP=totalP-totD;
        }
        else{
            totP=totalP;
        }
        txtReceipt.append("TOTAL FEES : " + "\t\t" + "RM" +totalP +"0"+ "\n");

      }

    if(rbSpm.isSelected()){
         double bm=0.0, bi=0.0, ag=0.0, sej=0.0, m3=0.0, addm3=0.0, sc=0.0, cm=0.0, bio=0.0, fz=0.0, ac=0.0, pd=0.0; 
        int countS = 0;

        if(cbSbm.isSelected())
        {
            int hbm = Integer.parseInt(txtShbm.getText());
            int tbm = Integer.parseInt(txtStbm.getText());
            bm = 50*hbm*tbm;
            countS++;
            txtReceipt.append("BAHASA MELAYU         " + "\t\tRM 50.00" + "\t   " + hbm + "\t   " +tbm+ "\t   "+ df.format(bm) + "\n"); // set to text area
        }

        if(cbSbi.isSelected())
        {
            int hbi = Integer.parseInt(txtShbi.getText());
            int tbi = Integer.parseInt(txtStbi.getText());
            bi = 50*hbi*tbi;
            countS++;
            txtReceipt.append("BAHASA INGGERIS         " + "\t\tRM 50.00" + "\t   " + hbi + "\t   " +tbi+"\t   "+ df.format(bi) + "\n"); // set to text area
        }

        if(cbSag.isSelected())
        {
            int hag = Integer.parseInt(txtShag.getText());
            int tag = Integer.parseInt(txtStag.getText());
            ag = 50*hag*tag;
            countS++;
            txtReceipt.append("PENDIDIKAN ISLAM         " + "\t\tRM 50.00" + "\t   " + hag + "\t   " +tag+"\t   "+ df.format(ag) + "\n"); // set to text area
        }

        if(cbSsej.isSelected())
        {
            int hsej = Integer.parseInt(txtShsej.getText());
            int tsej = Integer.parseInt(txtStsej.getText());
            sej = 50*hsej*tsej;
            countS++;
            txtReceipt.append("SEJARAH         " + "\t\tRM 50.00" + "\t   " + hsej + "\t   " +tsej+"\t   "+ df.format(sej) + "\n"); // set to text area
        }

        if(cbSm3.isSelected())
        {
            int hm3 = Integer.parseInt(txtShm3.getText());
            int tm3 = Integer.parseInt(txtStm3.getText());
            m3 = 50*hm3*tm3;
            countS++;
            txtReceipt.append("MATEMATIK         " + "\t\tRM 50.00" + "\t   " + hm3 + "\t   " +tm3+"\t   "+ df.format(m3) + "\n"); // set to text area
        }

        if(cbSaddm3.isSelected())
        {
            int haddm3 = Integer.parseInt(txtShaddm3.getText());
            int taddm3 = Integer.parseInt(txtStaddm3.getText());
            addm3 = 50*haddm3*taddm3;
            countS++;
            txtReceipt.append("MATEMATIK TAMBAHAN " + "\t\tRM 50.00" + "\t   " + haddm3 + "\t   " +taddm3+"\t   "+ df.format(addm3) + "\n"); // set to text area
        }

        if(cbSsc.isSelected())
        {
            int hsc = Integer.parseInt(txtShsc.getText());
            int tsc = Integer.parseInt(txtStsc.getText());
            sc = 50*hsc*tsc;
            countS++;
            txtReceipt.append("SAINS         " + "\t\t\tRM 50.00" + "\t   " + hsc + "\t   " +tsc+ "\t   "+ df.format(sc) + "\n"); // set to text area
        }

        if(cbScm.isSelected())
        {
            int hcm = Integer.parseInt(txtShcm.getText());
            int tcm = Integer.parseInt(txtStcm.getText());
            cm = 50*hcm*tcm;
            countS++;
            txtReceipt.append("KIMIA         " + "\t\t\tRM 50.00" + "\t   " + hcm + "\t   " +tcm+ "\t   "+ df.format( cm) + "\n"); // set to text area
        }

        if(cbSbio.isSelected())
        {
            int hbio = Integer.parseInt(txtShbio.getText());
            int tbio = Integer.parseInt(txtStbio.getText());
            bio = 50*hbio*tbio;
            countS++;
            txtReceipt.append("BIOLOGI" + "\t\t\tRM 50.00" + "\t   " + hbio + "\t   " +tbio+ "\t   "+ df.format( bio) + "\n"); // set to text area
        }

        if(cbSfz.isSelected())
        {
            int hfz = Integer.parseInt(txtShfz.getText());
            int tfz = Integer.parseInt(txtStfz.getText());
            fz = 50*hfz*tfz;
            countS++;
            txtReceipt.append("FIZIK         " + "\t\t\tRM 50.00" + "\t   " + hfz + "\t   " +tfz+ "\t   "+ df.format( fz) + "\n"); // set to text area
        }

        if(cbSac.isSelected())
        {
            int hac = Integer.parseInt(txtShac.getText());
            int tac = Integer.parseInt(txtStac.getText());
            ac = 50*hac*tac;
            countS++;
            txtReceipt.append("AKAUN         " + "\t\t\tRM 50.00" + "\t   " + hac + "\t   " +tac+ "\t   "+ df.format(ac) + "\n"); // set to text area
        }

        if(cbSpd.isSelected())
        {
            int hpd = Integer.parseInt(txtShpd.getText());
            int tpd = Integer.parseInt(txtStpd.getText());
            pd = 50*hpd*tpd;
            countS++;
            txtReceipt.append("PERDAGANGAN" + "\t\tRM 50.00" + "\t   " + hpd + "\t   " +tpd+ "\t   "+ df.format( pd) + "\n"); // set to text area
        }

        totalS = bm + bi + ag + sej + m3 + addm3 + sc + cm + fz + ac + pd;

        if(countS >= 6)
        {
            totD=totalS*0.1;
            totP=totalS-totD;
        }
        else{
            totP=totalS;
        }
        txtReceipt.append("TOTAL FEES : " + "\t\t" + "RM" +totP+"0"+ "\n");
    }



            txtReceipt.append("\n");

            txtReceipt.append("TOTAL DISCOUNT : " + "\t\t" + "RM " +totD +"0"+ "\n");
            txtReceipt.append("TOTAL PAYMENT PER MONTH : " + "\t" + "RM" +(totP*4)+"0" + "\n");//txtTot.getText()
            txtReceipt.append("\n");
            txtReceipt.append("\t  THANK YOU FOR CHOOSING SUPER TUITION SDN BHD" + "\n");

            txtReceipt.append("\n"); // set to text area
        }
    if (event.getSource() == btnClear)
    {
        cbUbm1.setSelected(false);
        cbUbm2.setSelected(false);
        cbUbi.setSelected(false);
        cbUmath.setSelected(false);
        cbUsc.setSelected(false);

        txtUh1.setText(" ");
        txtUh2.setText(" ");
        txtUh3.setText(" ");
        txtUh4.setText(" ");
        txtUh5.setText(" ");

        txtUt1.setText(" ");
        txtUt2.setText(" ");
        txtUt3.setText(" ");
        txtUt4.setText(" ");
        txtUt5.setText(" ");

    txtPhbm.setText(" ");
    txtPhbi.setText(" ");
    txtPhm3.setText(" ");
    txtPhsc.setText(" ");
    txtPhag.setText(" ");
    txtPhsj.setText(" ");
    txtPhkh.setText(" ");
    txtPtbm.setText(" ");
    txtPtbi.setText(" ");
    txtPtm3.setText(" ");
    txtPtsc.setText(" ");
    txtPtag.setText(" ");
    txtPtsj.setText(" ");
    txtPtkh.setText(" ");


    cbPbm.setSelected(false);
    cbPbi.setSelected(false);
    cbPm3.setSelected(false);
    cbPsc.setSelected(false);
    cbPag.setSelected(false);
    cbPsj.setSelected(false);
    cbPkh.setSelected(false);




    txtShbm.setText(" ");
    txtShbi.setText(" ");
    txtShsc.setText(" ");
    txtShag.setText(" ");
    txtShm3.setText(" ");
    txtShac.setText(" ");
    txtShpd.setText(" ");
    txtShaddm3.setText(" ");
    txtShcm.setText(" ");
    txtShbio.setText(" ");
    txtShfz.setText(" ");
    txtShsej.setText(" ");

    txtStbm.setText(" ");
    txtStbi.setText(" ");
    txtStsc.setText(" ");
    txtStag.setText(" ");
    txtStm3.setText(" ");
    txtStac.setText(" ");
    txtStpd.setText(" ");
    txtStaddm3.setText(" ");
    txtStcm.setText(" ");
    txtStbio.setText(" ");
    txtStfz.setText(" ");
    txtStsej.setText(" ");

    cbSbm.setSelected(false);
    cbSbi.setSelected(false);
    cbSsc.setSelected(false);
    cbSag.setSelected(false);
    cbSm3.setSelected(false);
    cbSac.setSelected(false);
    cbSpd.setSelected(false);
    cbSaddm3.setSelected(false);
    cbScm.setSelected(false);
    cbSbio.setSelected(false);
    cbSfz.setSelected(false);
    cbSsej.setSelected(false);
    txtname.setText(" ");
    txtage.setText(" ");
    txtcontact.setText(" ");

        rbUpsr.setSelected(false);
        rbPt3.setSelected(false);
        rbSpm.setSelected(false);
        txtReceipt.setText("\t  WELCOME TO SUPER TUITION SDN BHD\t\t" + "\n"); //clearing all data that are available
    }    


    if (event.getSource() == btnExit)
    {
        System.exit(0); //exit the program
    }
   }
   public static void main(String [] args)
  {
    TestP t = new TestP(); //main application
    t.setVisible(true);
    t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
  }

最佳答案

您将看到从 JTextField 返回的 toString()。切勿尝试打印文本字段,仅打印其文本,这是通过调用 toString() 获得的。

这里:

txtReceipt.append("TOTAL PAYMENT" + "\t\t\t" + "    RM" + txtTot + "\n");

您正在打印 JTextField、txtTot,而不是它包含的文本。不要这样做。而是这样做:

txtReceipt.append("TOTAL PAYMENT" + "\t\t\t" + "    RM" + txtTot.getText() + "\n");

对于您可能尝试打印的所有其他 JTextField 也类似。

还有

  • 虽然 null 布局和 setBounds() 对于 Swing 新手来说似乎是创建复杂 GUI 的最简单、最好的方法,但创建的 Swing GUI 越多,使用它们时遇到的困难就越严重。当 GUI 调整大小时,它们不会调整组件的大小,它们是增强或维护的皇家女巫,当放置在滚动 Pane 中时,它们会完全失败,在所有平台或与原始分辨率不同的屏幕分辨率上查看时,它们看起来非常糟糕.
  • 你似乎有一个巨大的“上帝”类,一个试图做所有事情的类,这不好,因为这会导致代码困惑和复杂。最好创建多个较小的类,每个类都有自己的单一职责,每个类都可以单独测试和调试。

关于java - 单击“清除”按钮后第二次尝试输入数据无法生成输出(Java GUI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33454401/

相关文章:

java - Android:媒体播放器不会在使用 ImageView 的第二个 Activity 中暂停

java - setContentPane() 和 addActionListener 收到 NullPointerException

c++ - 让子窗口继承父背景颜色?

java - 如何在 javafx TreeView 中使某些节点变为粗体?

java - JDialog 上的 .setModal() 方法

java - 从 Activity 中访问方法,该方法将从类中启动另一个 Activity

java - 检查字符串是否存在于 HashSet Java 中的时间复杂度

java - JtextEditorPane - 如何防止复制和粘贴将文本换行到 html 段落标记中?

Java - 如何将 png 文件添加到 JPanel

user-interface - DLGTableLayOut(2, 1, 0) 和 DLGLayout(DLGCreateTableLayout(2, 1, 0)) 有什么区别