简体   繁体   English

即使在上面的类中也找不到变量

[英]Variable not found even though it's in the class above

So I have a GUI class which contains the JButtons, and then I have an actionPerformed class below which shows what will happen if I press the button. 因此,我有一个包含JButtons的GUI类,然后在下面有一个actionPerformed类,该类显示了按按钮时将发生的情况。 Now, when I do the if(e.getSource()==search) the search comes up red and says the variable cannot be found in the class GUI. 现在,当我执行if(e.getSource()==search) ,搜索变成红色,并说在类GUI中找不到该变量。 Even though it's in that very class. 即使在那个类别中。

I need some help with this. 我需要一些帮助。

package crimedata;

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


class GUI extends JFrame implements ActionListener {

    Connection con = null;

    GUI() {
        JPanel p = new JPanel();
        JFrame f = new JFrame("Crime Data");

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(600, 600);

        f.setResizable(true);
        f.setLocationRelativeTo(null);
        GridBagConstraints gbc = new GridBagConstraints();
        getContentPane().setLayout(new GridBagLayout());

        gbc.insets = new Insets(5, 5, 5, 5);

        JLabel LongLabel = new JLabel("Enter Longitude here");
        LongLabel.setForeground(Color.gray);
        gbc.gridx = 0;
        gbc.gridy = 1;
        getContentPane().add(LongLabel, gbc);
        JTextField LongText = new JTextField(20);
        getContentPane().add(LongText, gbc);

        JLabel LatLabel = new JLabel("Enter Latitude here");
        LatLabel.setForeground(Color.gray);
        gbc.gridx = 0;
        gbc.gridy = 2;
        getContentPane().add(LatLabel, gbc);
        JTextField LatText = new JTextField(20);
        getContentPane().add(LatText, gbc);

        JLabel LSOANameLabel = new JLabel("Enter LSOA Name here");
        LSOANameLabel.setForeground(Color.gray);
        gbc.gridx = 0;
        gbc.gridy = 3;
        getContentPane().add(LSOANameLabel, gbc);
        JTextField LSOANameText = new JTextField(20);
        getContentPane().add(LSOANameText, gbc);

        JButton search = new JButton("Search");
        gbc.gridx = 0;
        gbc.gridy = -1;
        getContentPane().add(search, gbc);
        search.addActionListener(this);

        JButton exportnoid = new JButton("Export No Crime ID");
        gbc.gridx = 0;
        gbc.gridy = -1;
        getContentPane().add(exportnoid, gbc);

        JButton exportdup = new JButton("Export Duplicate ID");
        gbc.gridx = 0;
        gbc.gridy = -1;
        getContentPane().add(exportdup, gbc);

        f.add(getContentPane());
        pack();
        f.setVisible(true);
    }

   @Override
   public void actionPerformed(ActionEvent e) {
      if(e.getSource()==search){
          System.out.println("");
      } else {

      }
   }

}

class GUIHandler {
    public static void main(String[] args) {
        GUI a = new GUI();
    }
}

No, there is no variable search in the scope of your class GUI . 不,在您的类GUI的范围内没有变量search

There is a local variable search in the constructor of your class GUI . 在类GUI的构造函数中有一个局部变量search

The only instance variable I see so far, is the variable con of type Connection . 到目前为止,我看到的唯一实例变量是Connection类型的变量con

You should declare search as an instance variable of GUI , just like con , if you want to use it in another method. 如果要在其他方法中使用search ,则应像con一样将search声明为GUI的实例变量。 My suggestion is, you make it private . 我的建议是,将其private

Alternatively you could use setActionCommand(String cmd) to set an action command name for your JButton. 或者,您可以使用setActionCommand(String cmd)设置JButton的操作命令名称。 Then you can use getActionCommand() of your ActionEvent to retrieve the name of the action and react accordingly. 然后,您可以使用ActionEvent getActionCommand()来检索动作的名称并做出相应的反应。 But I do not like this approach, since in this case the compiler cannot help you much to detect name clashes. 但是我不喜欢这种方法,因为在这种情况下,编译器无法帮助您检测名称冲突。

search variable is not an instance variable, its scope is limited to the constructor, so you cannot use it outside of it. search变量不是实例变量,其范围仅限于构造函数,因此您不能在其外部使用它。

Declare JButton search variable after Connection con = null; Connection con = null;之后声明JButton search变量Connection con = null; .

Also note that have empty 'else' statement in actionPerformed method. 还要注意,在actionPerformed方法中有空的“ else”语句。

class GUI extends JFrame implements ActionListener { 类GUI扩展JFrame实现ActionListener {

Connection con = null;
JButton search;

GUI() {
    JPanel p = new JPanel();
    JFrame f = new JFrame("Crime Data");

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(600, 600);

    f.setResizable(true);
    f.setLocationRelativeTo(null);
    GridBagConstraints gbc = new GridBagConstraints();
    getContentPane().setLayout(new GridBagLayout());

    gbc.insets = new Insets(5, 5, 5, 5);

    JLabel LongLabel = new JLabel("Enter Longitude here");
    LongLabel.setForeground(Color.gray);
    gbc.gridx = 0;
    gbc.gridy = 1;
    getContentPane().add(LongLabel, gbc);
    JTextField LongText = new JTextField(20);
    getContentPane().add(LongText, gbc);

    JLabel LatLabel = new JLabel("Enter Latitude here");
    LatLabel.setForeground(Color.gray);
    gbc.gridx = 0;
    gbc.gridy = 2;
    getContentPane().add(LatLabel, gbc);
    JTextField LatText = new JTextField(20);
    getContentPane().add(LatText, gbc);

    JLabel LSOANameLabel = new JLabel("Enter LSOA Name here");
    LSOANameLabel.setForeground(Color.gray);
    gbc.gridx = 0;
    gbc.gridy = 3;
    getContentPane().add(LSOANameLabel, gbc);
    JTextField LSOANameText = new JTextField(20);
    getContentPane().add(LSOANameText, gbc);
    search = new JButton("Search");
    gbc.gridx = 0;
    gbc.gridy = -1;
    getContentPane().add(search, gbc);
    search.addActionListener(this);

    JButton exportnoid = new JButton("Export No Crime ID");
    gbc.gridx = 0;
    gbc.gridy = -1;
    getContentPane().add(exportnoid, gbc);

    JButton exportdup = new JButton("Export Duplicate ID");
    gbc.gridx = 0;
    gbc.gridy = -1;
    getContentPane().add(exportdup, gbc);

    f.add(getContentPane());
    pack();
    f.setVisible(true);
}

} }

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

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