简体   繁体   中英

Window displayed but not the buttons

I wrote this code to display a window with three buttons. However, the window appears but not the buttons. Here is the code:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.*;



public class Test3 extends JFrame{


public Test3() {

    JFrame frame = new JFrame("Exemple");
    JButton button1 = new JButton("Règles");
    button1.setBounds(100, 60, 100, 30);
    JButton button2 = new JButton("Jouer");
    JButton button3 = new JButton("Scores");
    JPanel pane1 = new JPanel(new GridLayout(0, 1));
    this.setVisible(true);

    pane1.add(button1);
    pane1.add(button2);
    pane1.add(button3);
    frame.getContentPane().add(pane1, BorderLayout.EAST);
    this.setVisible(true);
    setSize(800, 600);



}

public static void main(String[] args) {
    Test3 test3 = new Test3();
}

}

Does anyone have an idea on how to fix this ?

Thanks for your help !

Agnès

You have two windows...

public class Test3 extends JFrame{
    public Test3() {
        JFrame frame = new JFrame("Exemple");

You're adding content to frame , but showing Test3 ...

As a general rule of thumb, you should avoid extending from top level containers like JFrame and instead, create an instance when you need it. This unlocks you from a given implementation and allows you flexibility in deciding how you might re-use given components, as an example.

public class Test3 {
    public Test3() {
        JFrame frame = new JFrame("Exemple");
        JButton button1 = new JButton("Règles");
        JButton button2 = new JButton("Jouer");
        JButton button3 = new JButton("Scores");
        JPanel pane1 = new JPanel(new GridLayout(0, 1));

        pane1.add(button1);
        pane1.add(button2);
        pane1.add(button3);
        frame.getContentPane().add(pane1, BorderLayout.EAST);
        frame.pack();
        frame.setVisible(true);
    }

Runnable example...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test3 {

    public Test3() {

        JFrame frame = new JFrame("Exemple");
        JButton button1 = new JButton("Règles");
        button1.setBounds(100, 60, 100, 30);
        JButton button2 = new JButton("Jouer");
        JButton button3 = new JButton("Scores");
        JPanel pane1 = new JPanel(new GridLayout(0, 1));

        pane1.add(button1);
        pane1.add(button2);
        pane1.add(button3);
        frame.getContentPane().add(pane1, BorderLayout.EAST);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                Test3 test3 = new Test3();
            }
        });
    }

}

Please try like this, I have corrected

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.*;



public class Test3 extends JFrame{


public Test3() {

   super("Exemple");
    JButton button1 = new JButton("Règles");
    button1.setBounds(100, 60, 100, 30);
    JButton button2 = new JButton("Jouer");
    JButton button3 = new JButton("Scores");
    JPanel pane1 = new JPanel(new GridLayout(0, 1));
    this.setVisible(true);

    pane1.add(button1);
    pane1.add(button2);
    pane1.add(button3);
    getContentPane().add(pane1, BorderLayout.EAST);
    this.setVisible(true);
    setSize(800, 600);



}

public static void main(String[] args) {
    Test3 test3 = new Test3();
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM