0

For my project, I'm trying to use an ActionListener for a button that when clicked will just go and run another class. I have a constructor that sets up a good amount of my JFrame, and was initially setting up my JButton, one problem though.

I'm using EventQueue.invokeLater(new Runnable(){}) and this doesn't play nice when you try and impliment ActionListener. So I tried creating a separate method outside of the EventQueue to instantiate the button, but now the problem with this is that when I use the line

notebook.addActionListener(this);

this doesn't like to be static, and hence I get hit with that compile-time error in BlueJ: "non-static variable this cannot be referenced from static context".

I tried going back further in an attempt to make as much as a could not static, but it ran down to the main method that has to stay static, or else I end up just running public ui().

Here is one class of my code:

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class ui implements ActionListener
{
    static JFrame frame = new JFrame("Student Tool Dev1.0");
    static JButton notebook = new JButton("Notes");

public static void main(String args[])
{
    new ui();
    buttonSetup();
}

public ui()
{
    EventQueue.invokeLater(new Runnable(){
        @Override
        public void run(){
        try
        {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex)
        {
            ex.printStackTrace();
        }  
        try
        {
                //Load the background image
                BufferedImage img = ImageIO.read(new File("desk.png"));

                //Creates JFrame
                //JFrame frame = new JFrame("Student Tool Dev1.0");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                //Set frame content pane to use JLabel with an 
                //icon property set to use the image buffered above
                frame.setContentPane(new JLabel(new ImageIcon(img)));
                /*
                notebook = new JButton("Notes");
                notebook.setSize(158,175);
                notebook.setLocation(130,160);
                notebook.setOpaque(false);   
                notebook.setContentAreaFilled(false);
                notebook.setBorderPainted(false);
                notebook.setFocusable(false);
                frame.add(notebook);

                notebook.setActionCommand("event");
               // notebook.addActionListener(this);
               */
                //Other JFrame stuph
                frame.pack();
                frame.setResizable(false);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
        } 
        catch(IOException e)
        {
            e.printStackTrace();
        }   
        }
    });
} 

public static void buttonSetup()
{
    // Button instantiation and usage to change text size
    // as well as make it trabnsparent around images
    // Uses absolute positioning
    notebook.setSize(158,175);
    notebook.setLocation(130,160);
    notebook.setOpaque(false);   
    notebook.setContentAreaFilled(false);
    notebook.setBorderPainted(false);
    notebook.setFocusable(false);

    notebook.setActionCommand("event");
    notebook.addActionListener(this);

    frame.add(notebook);

}

public void actionPerformed(ActionEvent event)
{
    Notebook note = new Notebook();
    note.run();
}
}

And yes, I know absolute positioning is not a good practice, but for this application, a layout manager would have been to tedious to work with for what I'm looking for.

Any help is appreciated!

RubioRic
  • 2,442
  • 4
  • 28
  • 35
Connor Bailey
  • 69
  • 1
  • 13

0 Answers0