lundi 7 mai 2018

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. 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. 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();
}

Aucun commentaire:

Enregistrer un commentaire