suppose i have a code in java like this
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class GUIExercise {
private static void createAndShowGUI () {
JFrame frame = new JFrame("My Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel center = new JPanel();
center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));
JLabel label = new JLabel("Migz");
label.setHorizontalAlignment(JLabel.CENTER);
label.setFont(label.getFont().deriveFont(Font.ITALIC | Font.BOLD));
center.add(label);
JButton btn = new JButton("Click me");
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed (ActionEvent e) {
JOptionPane.showMessageDialog(GUIExercise.this, "Font.ITALIC is " + Font.ITALIC + " and Font.BOLD is " + Font.BOLD + " finally Font.ITALIC | Font.BOLD is " + (Font.ITALIC | Font.BOLD), "Ni Hao", JOptionPane.INFORMATION_MESSAGE);
}
});
center.add(btn);
frame.getContentPane().add(center, BorderLayout.CENTER);
frame.pack();
frame.setSize(frame.getWidth() + 100, frame.getHeight() + 50);
frame.setVisible(true);
}
public static void main (String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run () {
createAndShowGUI();
}
});
}
}
putting GUIExercise.this in the first parameter of showmessagedialog will result in error: non-static variable this cannot be referenced from a static context. what must be done? or how can i access the EnclosingClass?
EnclosingClassshould extendsComponent. I can't trust this code. Also the error message is sketch becausethiscannot be the name of a variable.