在我的源代码中出现“this”语句的问题

时间:2013-11-30 07:38:50

标签: java button static action actionlistener

所以这是当前的代码,到目前为止,除了背景颜色以外,任何有“//”之前的东西都是导致其他东西之间错误的原因

package com.Luminate.luminatemedia;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Window extends JFrame implements ActionListener{

static JFrame mediaform = new JFrame();
public static void main(String[] args) {
}

public static void load() {

    ImageIcon fileIcon = new ImageIcon ("assets/icon.png");
    ImageIcon exitIcon = new ImageIcon ("assets/exit.png");
    JButton exitButton = new JButton();
    JLabel text = new JLabel("Luminate Media");
    JPanel panel = new JPanel();

    mediaform.add(panel);
    panel.add(exitButton);
    panel.add(text);

    mediaform.setTitle("Luminate");
    mediaform.setIconImage(fileIcon.getImage());
    mediaform.setBounds(0, 688, 1366, 40);
    //mediaform.setLayout(null);
    mediaform.setUndecorated(true);
    mediaform.setResizable(false);
    mediaform.setVisible(true);
    mediaform.setAlwaysOnTop(true);
    //mediaform.getContentPane().setBackground(Color.DARK_GRAY);
    mediaform.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    text.setFont(new Font("dandelion in the spring", Font.PLAIN, 32));
    text.setBounds(0, 0, 0, 0);
    text.setHorizontalAlignment(SwingConstants.CENTER);
    text.setSize(0, 0);
    text.setForeground(Color.black);
    text.setHorizontalAlignment(0);

    exitButton.setBorder(null);
    exitButton.setBounds(1326, 0, 40, 40);
    exitButton.addActionListener(null);
    exitButton.setIcon(exitIcon);

    //BELOW IS MY ISSUE
    exitButton.addActionListener(this);

    panel.setLayout(null);
    panel.setBackground(DARK_GRAY);
}
@Override
public void actionPerformed(ActionEvent e) {
    //code to close the whole program

}

}

因此强调文本中出现错误,其中显示“this”

在点击按钮时关闭主类的代码是什么?我是否正确地执行此操作?

1 个答案:

答案 0 :(得分:0)

试试这个

public class Window extends JFrame implements ActionListener{
    public static void main(String[] args) {
       new Window();
    }

   public Window() {

    ImageIcon fileIcon = new ImageIcon ("assets/icon.png");
    ImageIcon exitIcon = new ImageIcon ("assets/exit.png");
    JButton exitButton = new JButton("Label");
    JLabel text = new JLabel("Luminate Media");
    JPanel panel = new JPanel();



    setTitle("Luminate");
    setIconImage(fileIcon.getImage());
    setBounds(0, 688, 1366, 40);
    setLayout(null);
    setUndecorated(true);
    setResizable(false);
    setVisible(true);
    setAlwaysOnTop(true);
    getContentPane().setBackground(Color.DARK_GRAY);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    text.setFont(new Font("dandelion in the spring", Font.PLAIN, 32));
    text.setBounds(0, 0, 0, 0);
    text.setHorizontalAlignment(SwingConstants.CENTER);
    text.setSize(0, 0);
    text.setForeground(Color.black);
    text.setHorizontalAlignment(0);

    exitButton.setBorder(null);
    exitButton.setBounds(1326, 0, 40, 40);
    exitButton.setIcon(exitIcon);
    exitButton.addActionListener(this);

    panel.setLayout(null);
    panel.setBackground(DARK_GRAY);
    panel.add(exitButton);
    panel.add(text);
    add(panel);
}
@Override
public void actionPerformed(ActionEvent e) {
    //code to close the whole program

}

}