Я делаю JFrame, который имеет две страницы (P07 и P07ProcessData). Я не могу получить кнопки для записи правильной информации в текстовое поле

P07ProcessData считывает информацию о заработной плате из файла DAT и производит расчеты. P07 - это JFrame. Я хочу, чтобы P07 записывал информацию о заработной плате в текстовое поле при нажатии двух отдельных кнопок. Один, Show Pay, чтобы показать имена и информацию о заработной плате... два, Show Totals, чтобы показать итоги информации. Я не могу заставить это сделать это. Любая помощь будет оценена.

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class P07 extends JFrame implements ActionListener
{
    private static P07 MyFrame;
private static String Author, Company, Heading, Totals;

private JPanel       pnlHeading, pnlDisplay, pnlButtons;
private JButton      btnShowPay, btnShowTotals, btnReset, btnExit;
private JLabel       lblAuthor, lblCompany, lblHeading;
private JTextArea    txtArea;

public P07()
{
     super(Author);

    pnlHeading = new JPanel(new GridLayout(0, 1, 4, 4) );
    pnlHeading.setBackground(Color.blue);
    pnlDisplay = new JPanel(new GridLayout(0, 1, 2, 2) );
    pnlButtons = new JPanel(new GridLayout(1, 4, 4, 5) );
    pnlButtons.setBackground(Color.blue);

    Container Pane = getContentPane();
    Pane.setLayout(new BorderLayout(2, 2) );
    Pane.setBackground(Color.blue);
    Pane.add(pnlHeading, "North");
    Pane.add(pnlDisplay, "Center");
    Pane.add(pnlButtons, "South");

    lblAuthor = new JLabel(Author);
    lblAuthor.setOpaque(true);
    lblAuthor.setBackground(Color.MAGENTA);
    lblAuthor.setForeground(Color.BLACK);
    lblAuthor.setFont(new Font("Times New Roman", Font.PLAIN, 20) );
    pnlHeading.add(lblAuthor);

    lblCompany = new JLabel(Company);
    lblCompany.setOpaque(true);
    lblCompany.setBackground(Color.CYAN);
    lblCompany.setForeground(Color.BLACK);
    lblCompany.setFont(new Font("Times New Roman", Font.PLAIN, 20) );
    pnlHeading.add(lblCompany);

    lblHeading = new JLabel(Heading);
    lblHeading.setOpaque(true);
    lblHeading.setBackground(Color.GREEN);
    lblHeading.setForeground(Color.BLACK);
    lblHeading.setFont(new Font("Times New Roman", Font.PLAIN, 20) );
    pnlHeading.add(lblHeading);

    txtArea = new JTextArea();
    txtArea.setEditable(false);
    txtArea.setBackground(Color.white);
    txtArea.setFont(new Font("Monospaced", Font.PLAIN, 16) );
    JScrollPane Scroller = new JScrollPane(txtArea);
    pnlDisplay.add(Scroller);

    btnShowPay = new JButton("Show Pay");
    btnShowPay.setBackground(Color.GRAY);
    btnShowPay.setForeground(Color.black);
    btnShowPay.setFont(new Font("Times New Roman", Font.PLAIN, 18) );
    pnlButtons.add(btnShowPay);
    btnShowPay.addActionListener(this);

    btnShowTotals = new JButton("Show Totals");
    btnShowTotals.setBackground(Color.GRAY);
    btnShowTotals.setForeground(Color.black);
    btnShowTotals.setFont(new Font("Times New Roman", Font.PLAIN, 18) );
    pnlButtons.add(btnShowTotals);
    btnShowTotals.addActionListener(this);

    btnReset = new JButton("Reset");
    btnReset.setBackground(Color.GRAY);
    btnReset.setForeground(Color.black);
    btnReset.setFont(new Font("Times New Roman", Font.PLAIN, 18) );
    pnlButtons.add(btnReset);
    btnReset.addActionListener(this);

    btnExit = new JButton("Exit");
    btnExit.setBackground(Color.GRAY);
    btnExit.setForeground(Color.black);
    btnExit.setFont(new Font("Times New Roman", Font.PLAIN, 18) );
    pnlButtons.add(btnExit);
    btnExit.addActionListener(this);

    addWindowListener(new WinListener() );
}

private void Reset()
{
    txtArea.setText("");
    btnShowPay.setEnabled(true);
    btnReset.setEnabled(false);
}

public static void main(String args[]) throws IOException
{
    Author  =     "";
    Company =     "Go Broke and Lay Off Manufacturing Company";
    Heading =     "Employee Name     Hours Worked     Pay Rate     Gross  
                           Pay     Pension     Net Pay";

    MyFrame = new P07();
    MyFrame.setSize(Toolkit.getDefaultToolkit().getScreenSize() );
    MyFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    MyFrame.setVisible(true);

}

private void ExitProgram()
{
    MyFrame.setVisible(false);
    MyFrame.dispose();
    System.exit(99);
}

public void actionPerformed(ActionEvent Ae)
{
    if (Ae.getActionCommand() == "Show Pay")
    {
        txtArea.setText("");
        try
        {
            P07ProcessData.ProcessData(txtArea); }
            catch (IOException Error)
            {
                System.err.print("IOException error..." +    
                                     Error.getMessage() );
                txtArea.append("IOException error..." + 
                                     Error.getMessage() );
            }
                btnShowPay.setEnabled(false);
                btnReset.setEnabled(true);
    }

    else if (Ae.getActionCommand() == "Show Totals")
    {

        try
        {
            P07ProcessData.ProcessData(txtArea); }
            catch (IOException Error)
            {
                System.err.print("IOException error..." + 
                                     Error.getMessage() );
                txtArea.append("IOException error..." + 
                                     Error.getMessage() );
            }
                btnShowTotals.setEnabled(false);
                btnReset.setEnabled(true);
    }


    else if (Ae.getActionCommand() == "Reset") Reset();
    else ExitProgram();

}

class WinListener extends WindowAdapter
{
    @Override public void windowClosing(WindowEvent We)
    {
        ExitProgram();
    }
}

}

import java.io.*;
import java.util.*;
import javax.swing.*;

public class P07ProcessData
{

private static String Name, outText, outText2;
private static double hoursWorked, payRate, grossPay, pension, netPay, 
                   totalHours, totalGross, totalPension, totalNet;


public static void ProcessData(JTextArea txtArea) throws IOException
{

    RandomAccessFile Infile = new RandomAccessFile("P07.DAT", "r");
    StringTokenizer Tokens;
    long FileLength = Infile.length();

    while(Infile.getFilePointer() < FileLength)
    {
        Tokens = new StringTokenizer(Infile.readLine() );
        Name = Tokens.nextToken() + ',' + ' ' + Tokens.nextToken();
        hoursWorked = Double.parseDouble(Tokens.nextToken() );
        payRate = Double.parseDouble(Tokens.nextToken() );

        CalculatePay();
        SetOutputTextPay();
        txtArea.append(outText + outText2);

    } //while

    Infile.close();
}

    private static void CalculatePay()
    {
        if (hoursWorked > 40) grossPay = (40 + (hoursWorked - 40) * 
                    1.5) * payRate;
        else grossPay = hoursWorked * payRate;
            if (hoursWorked < 30) pension = grossPay * 0.061;
            else pension = payRate * 30 * 0.061;
            netPay = grossPay - pension;
    }

    private static void CalculateTotals()
    {
        totalHours = totalGross = totalPension = totalNet = 0;

        totalHours = totalHours + hoursWorked;
        totalGross = totalGross + grossPay;
        totalPension = totalPension + pension;
        totalNet = totalNet + netPay;
    }

    private static void SetOutputTextPay()
    {
        outText = String.format("%17s", Name) + String.format("%12f", 
                              hoursWorked) + String.format("%12f", payRate) + 
                              String.format("%12f", grossPay) + String.format("%12f", 
                              pension) + String.format("%12f", netPay) + '\n';
    }

    private static void SetOutputTextTotals()
    {
        outText2 = String.format("TOTALS:" + String.format("%21f", 
                               totalHours) + String.format("%24f", totalGross) + 
                               String.format("%12f", totalPension) + 
                               String.format("%12f", totalNet) );
    }

 }  //Class: P07ProcessData 

0 ответов

Другие вопросы по тегам