简体   繁体   English

我不明白为什么我的ActionListeners在按JButton时不工作?

[英]I don't understand why my ActionListeners are not working when pressing JButton?

I use action listener and then call it to a class that relays a function. 我使用动作侦听器,然后将其调用到一个中继函数的类。 I didn't want to use implements ActionListener in the class because it is more complicated to deal with multiple buttons. 我不想在类中使用implements ActionListener ,因为处理多个按钮更复杂。 Can you access a method from an ActionListener ? 你可以从ActionListener访问一个方法吗?

I am also getting a Serializable caution. 我也得到了Serializable的警告。 I'm not sure what that means. 我不确定这意味着什么。 I suppressed the warnings and looked it up online, however, I still do not understand it in context. 我压制了警告并在网上查了一下,但是,我仍然不了解它的情况。 First GUI project, any help is greatly appreciated. 第一个GUI项目,非常感谢任何帮助。

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class GUI extends JFrame{
    public JFrame j;
    public JPanel p;
    public JButton NEW;
    public JButton update;
    public DefaultTableModel model;
    public JTable jt;

    public GUI(){
        setVisible(true);
        setSize(600,400);
        setTitle("Student Record Management System");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        JButton NEW = new JButton("Additional Student");
        NEW.setBounds(10,10,20,20);
        NEW.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                NEWpressed();
            }
        });
        p.add(NEW);
        JButton update = new JButton("Update Exam Scores");
        update.setBounds(50,40,20,20);
        update.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                updatepressed();
            }
        });
        p.add(update);
        String [] col = {"Last Name", "First Name", "HKID", "Student ID", "Final Exam Score"};
        String [][] data = {{"Rollins", "Zachary", "1234", "4321", "88"},{"Preston","John", "4321", "1234", "94"}};
        DefaultTableModel model = new DefaultTableModel(data, col);
        JTable jt = new JTable(model);
        jt.setEnabled(false);
        jt.setPreferredScrollableViewportSize(new Dimension(400,300));
        jt.setFillsViewportHeight(true);
        jt.setBackground(Color.BLUE);
        jt.setAlignmentY(BOTTOM_ALIGNMENT);
        JScrollPane jps = new JScrollPane(jt);
        p.add(jps);
        add(p);
    }
    public void NEWpressed(){
        model.addRow(new Object[]{"col 1", "col 2", "col 3", "col 4", "col 5"});
    }
    public void updatepressed(){
        jt.setEnabled(true);
        p.add(jt);
        add(p);
    }
    public static void main(String args []){
        GUI a = new GUI();
        a.setVisible(true);

    }

}

Yes you can call methods from your onclicklistener but the problem is in your called method, you are calling 是的,您可以从onclicklistener调用方法,但问题出在您调用的方法中

model.addRow()

on DefaultTableModel which is not defined yet. 在DefaultTableModel上尚未定义。 Initialize your "model" variable before calling the pressed method. 在调用press方法之前初始化“model”变量。

Your main problem is you're shadowing your variables... 你的主要问题是你正在掩盖你的变量......

You define model as an instance variable 您将model定义为实例变量

public DefaultTableModel model;

But in the constructor you define a local variable of the same name... 但是在构造函数中,您定义了一个同名的局部变量......

DefaultTableModel model = new DefaultTableModel(data, col);

This means that when NEWpressed is called, model is null 这意味着当NEWpressed时, modelnull

You also define p and jt as a instance variable 您还将pjt定义为实例变量

public JTable jt;
public JPanel p;

But in the constructor, you define a local variable of the same name... 但是在构造函数中,您定义了一个同名的局部变量......

JPanel p = new JPanel();
//...
JTable jt = new JTable(model);

Which means when updatepressed is pressed jt and p are null 当这意味着updatepressed按下jtpnull

Lose the local definitions and simply instantiate the instance variables within the constructor... 丢失本地定义并简单地在构造函数中实例化实例变量......

public GUI() {
    p = new JPanel();
    //...
    model = new DefaultTableModel(data, col);
    //...
    jt = new JTable(model);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM