调试我的按钮

Debugging my Button

这是我的 JPanels class,用于我正在开发的一个小程序。该程序启动并正确显示我的 GUI。问题是出于某种原因我的 actionEvent 没有触发 onClick。

我在 actionPerformed 中输入了一个 println 以查看它是否真的启动了事件,但发现它没有。你们中的任何人都可以帮我弄清楚我哪里出了问题吗?

这是有问题的 class。

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;
}
}

提前致谢!如果您需要查看任何其他 classes 或需要更多信息,请告诉我!几天来我一直在努力解决这个问题,真的需要另一双眼睛来仔细检查。

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

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