如何处理ActionListeners和静态方法

时间:2016-05-10 18:09:55

标签: java swing jframe actionlistener

对于我的项目,我尝试使用ActionListener作为一个按钮,单击该按钮将运行另一个类。我有一个构造函数,它设置了很多我的JFrame,并且最初设置我的JButton,但是有一个问题。

我正在使用EventQueue.invokeLater(new Runnable(){}),当您尝试恭维ActionListener时,这并不好玩。所以我尝试在EventQueue之外创建一个单独的方法来实例化按钮,但现在问题是当我使用该行时

notebook.addActionListener(this);

这个不喜欢是静态的,因此我遇到了BlueJ中的编译时错误:"非静态变量,这不能从静态上下文引用#34;

我试图进一步尝试尽可能多地制作静态,但它跑到了必须保持静态的主要方法,否则我最终只是运行 public ui()< / em>的

这是我的一类代码:

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

是的,我知道绝对定位不是一个好的做法,但对于这个应用程序,布局管理员可能会因为我正在寻找的东西而单调乏味。

感谢任何帮助!

0 个答案:

没有答案