调试我的按钮

时间:2017-01-01 07:37:25

标签: java actionevent

这是我正在研究的一个小程序的JPanels类。该程序正确启动并显示我的GUI。问题是由于某种原因我的actionEvent没有触发onClick。

我在actionPerformed中放入了一个println,看看它是否真的启动了该事件并发现它不是。你们中的任何人能帮助我弄清楚我在哪里出错吗?

这是有问题的课程。

package MetaMarket.GUI;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.text.NumberFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import static MetaMarket.GUI.Fields.*;


public class UserInput {

private final JPanel userInput;
private final JPanel displayInput;

public UserInput(){

    //set up panel
    this.userInput = new JPanel();
    userInput.setLayout(null);
    userInput.setBounds(16,16,300,500);
    userInput.setBorder(new TitledBorder("Shift Report"));


    userInput.add(Labels.getUserJLabel());
    //set up select user combo box
    userInput.add(getUserSelection());
    // set up callsReceivedJLabel
    userInput.add(Labels.getCallsReceivedJLabel());
    // set up callsReceivedJTextField
    userInput.add(Fields.getCallsReceivedJTextField());
    // set up ticketsOpenedJLabel
    userInput.add(Labels.getTicketsOpenedJLabel());
    // set up ticketsOpenedJTextField
    userInput.add(getTicketsOpenedJTextField());
    // set up ticketsClosedJLabel
    userInput.add(Labels.getTicketsClosedJLabel());
    // set up ticketsClosedJTextField
    userInput.add(getTicketsClosedJTextField());
    // set up vpnRequestJLabel
    userInput.add(Labels.getVpnRequestsJLabel());
    // set up vpnRequestJTextField
    userInput.add(getVpnRequestsJTextField());
    // set up notesJLabel
    userInput.add(Labels.getNotesJLabel());
    // set up notesJTextArea
    userInput.add(Fields.getNotesJTextArea());

    Object[] row = new Object[8];
    JTable table = new JTable();
    Object[] columns = {"Date", "Representative","Calls","Tickets Opened", "Tickets Closed", "VPN Requests", "FCR"
            ,"Comments"};
    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.PINK);
    DefaultTableModel model = new DefaultTableModel();
    model.setColumnIdentifiers(columns);
    table.setModel(model);



    Buttons.createLogButton().addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            System.out.println("action is beginning");

            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
            Date date = new Date();

            int openInt = Integer.parseInt(getTicketsOpenedJTextField().getText());
            int closeInt = Integer.parseInt(getTicketsClosedJTextField().getText());
            int vpn = Integer.parseInt(getVpnRequestsJTextField().getText());

            double trueClose = closeInt - vpn;
            double trueOpen = openInt - vpn;
            double fcr =   trueClose/trueOpen;

            NumberFormat defaultFormat = NumberFormat.getPercentInstance();
            defaultFormat.setMinimumFractionDigits(2);

            row[0] = date;
            row[1] = getUserSelection().getSelectedItem();
            row[2] = getCallsReceivedJTextField().getText();
            row[3] = getTicketsOpenedJTextField().getText();
            row[4] = getTicketsClosedJTextField().getText();
            row[5] = getVpnRequestsJTextField().getText();
            row[6] = defaultFormat.format(fcr);
            row[7] = getNotesJTextArea().getText();

            model.addRow(row);

         }
    });



    userInput.add(Buttons.createLogButton());

    userInput.add(Buttons.createUpdateButton());

    userInput.add(Labels.getCreditsJLabel());

    //--------------------------------------//

    this.displayInput = new JPanel();
    displayInput.setBounds(330,16,825,700);
    displayInput.setBorder(new TitledBorder("Stuff and things"));
    displayInput.setLayout(new BorderLayout());

    displayInput.setLayout(new BorderLayout());
    displayInput.add(table.getTableHeader(), BorderLayout.PAGE_START);
    displayInput.add(table, BorderLayout.CENTER);

    JScrollPane pane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pane.setSize(825,700);

    displayInput.add(pane);


}

public JPanel getUserInput() {
    return this.userInput;
}

public JPanel getDisplayInput(){
    return this.displayInput;
}
}

提前致谢!如果您需要查看任何其他课程或需要更多信息,请告诉我们!我一直试图解决这个问题好几天了,真的需要另外一双眼睛看看它。

1 个答案:

答案 0 :(得分:2)

您创建了两次日志按钮。尝试更改您的代码:

Button btnLog = Buttons.createLogButton();
btnLog.addActionListener(new ActionListener() {
  ...
}
userInput.add(btnLog);