Okay,
So this code I have is giving me an error: non-static variable this cannot be referenced from a static context.
Here is the code:
package macey;
/**
* @author WolfShield
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DemoConv implements ActionListener {
private JLabel answerL;
public static void Conv() {
// Content
JPanel content = new JPanel();
content.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Actual Content
JTextField userInputNum, userInputUnit, userOutputUnit;
JButton convCalculateB;
JLabel leftBarL, rightBarL, multSymbolL, left1L, right1L, equalsL, answNumL, answUnitL;
// Variable Labels
JLabel rightBottomConvUnitL, rightTopConvNumL;
//Assign all above content
leftBarL = new JLabel("__________");
c.gridy=2;
c.gridx=0;
c.gridwidth=2;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(leftBarL, c);
rightBarL = new JLabel("__________");
c.gridy=2;
c.gridx=3;
c.gridwidth=2;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(rightBarL, c);
multSymbolL = new JLabel("X");
c.gridy=2;
c.gridx=2;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(multSymbolL, c);
left1L = new JLabel("1");
c.gridy=3;
c.gridx=0;
c.gridwidth=2;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(left1L, c);
right1L = new JLabel("1");
c.gridy=3;
c.gridx=3;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(right1L, c);
equalsL = new JLabel("=");
c.gridy=2;
c.gridx=5;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 10, 0, 10);
content.add(equalsL, c);
convCalculateB = new JButton("Calculate");
c.gridy=5;
c.gridx=2;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(10, 0, 0, 0);
convCalculateB.addActionListener(this);
content.add(convCalculateB, c);
userInputNum = new JTextField();
c.gridy=1;
c.gridx=0;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=39;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(userInputNum, c);
userInputUnit = new JTextField();
c.gridy=1;
c.gridx=1;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=25;
c.ipady=0;
c.insets=new Insets(0, 3, 0, 0);
content.add(userInputUnit, c);
userOutputUnit = new JTextField();
c.gridy=1;
c.gridx=4;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=25;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(userOutputUnit, c);
rightTopConvNumL = new JLabel("0.08"); // Changes
c.gridy=1;
c.gridx=3;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(rightTopConvNumL, c);
rightBottomConvUnitL = new JLabel("in.");
c.gridy=3;
c.gridx=4;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(rightBottomConvUnitL, c);
answNumL = new JLabel("0.4");
c.gridy=2;
c.gridx=6;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(answNumL, c);
answUnitL = new JLabel("ft.");
c.gridy=2;
c.gridx=7;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 5, 0, 0);
content.add(answUnitL, c);
//Menubar
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem fileQuit = new JMenuItem("Quit");
fileMenu.add(fileQuit);
menuBar.add(fileMenu);
JMenu helpMenu = new JMenu("Help");
JMenuItem helpHowToUse = new JMenuItem("How To Use The Converter");
helpMenu.add(helpHowToUse);
menuBar.add(helpMenu);
// Converter Window
JFrame convWin = new JFrame("Converter Demo");
convWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
convWin.setResizable(false);
convWin.setSize(new Dimension(340, 160));
convWin.setLocation(450, 150);
convWin.getRootPane().setDefaultButton(convCalculateB);
convWin.setJMenuBar(menuBar);
convWin.setContentPane(content);
convWin.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Conv();
}
});
}
public void actionPerformed(ActionEvent e) {
String f;
f = e.getActionCommand();
}
}
The line the error is on is line 89:
convCalculateB.addActionListener(this);
Thanks in advance,
- WolfShield