1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| package gui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.UIManager;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class PourForum extends JFrame {
private JPanel contentPane;
private JButton button;
private static PourForum frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new PourForum();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PourForum() {
setTitle("Titre 1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 621, 428);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
button = new JButton("Rendre invisible");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
button_ferme();
}
});
GridBagConstraints gbc_button = new GridBagConstraints();
gbc_button.gridx = 1;
gbc_button.gridy = 1;
contentPane.add(button, gbc_button);
init();
}
static void button_ferme() {
frame.setVisible(false);
frame.setVisible(true); // sert pour test uniquement
}
static void init() {
setTitle("Titre 2"); // Réponse Eclipse: Cannot make a static reference to the non-static method setTitle(String) from the type Frame
frame.setTitle("Titre 2"); // Pas d'erreur à la compilation mais à l'éexécution:
/* java.lang.NullPointerException
at gui.PourForum.init(PourForum.java:87)
at gui.PourForum.<init>(PourForum.java:74) */
this.setTitle("Titre 2"); // Réponse Eclipse: Cannot use this in a static context
}
} |
Partager