简体   繁体   中英

WindowBuilder - open a JFrame from Main

I have a Main.java , here i would only start ( set visible true ) my JFrame window. But it only opens a little window without anything. Then I would press the Highscorebutten, then a other JFrame (highscore) should be shown.

What should I do?

(this isn't the full code)

Main

public class Main {
    private static MyWindow window = null;

    public static void main(String[] args) {
         window = new MyWindow();
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          // Frame sichtbar machen
          window.setVisible(true);
    }

FWindow

public class FWindow extends JFrame{    

private JFrame Menue;

public FWindow() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    Menue = new JFrame();
    Menue.setBounds(100, 100, 469, 741);
    Menue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Menue.getContentPane().setLayout(null);

    JPanel pMenue = new JPanel();

    pMenue.setLayout(null);

    JLabel lblHighscore = new JLabel();
    lblHighscore.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
             Highscore highscore = new Highscore();         
                getContentPane().add(highscore);
                System.out.println();
                highscore.setVisible(true);

        }
    });
//........

Highscore

public class Highscore extends JFrame {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private JTable table;
TableModel model;

public Highscore() {


    table = new JTable(model);
    table.setShowGrid(false);
    table.setShowHorizontalLines(false);
    table.setShowVerticalLines(false);
    table.setForeground(Color.WHITE);
    table.setBackground(new Color(113,197,208));
    table.setFont(new Font("Comic Sans MS", Font.PLAIN, 30));
    table.setRowHeight(40);
    table.setAutoCreateRowSorter(true);



    table.setModel(new DefaultTableModel(
        new Object[][] {
            //.....
        },
        new String[] {
            "1", "2", "3"
        }

    ){

    });

.......

FWindow extends JFrame , but it is creating a second instance of JFrame ( Menue ), meaning you're only showing FWindow , which has nothing on it.

Remove the extends JFrame from FWindow , it's not adding any useful functionality

Add a method to FWindow which will prepare and show the Menue instance

public class FWindow {

    private JFrame Menue;

    public FWindow() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        Menue = new JFrame();
        Menue.setBounds(100, 100, 469, 741);
        Menue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Menue.getContentPane().setLayout(null);

        JPanel pMenue = new JPanel();

        pMenue.setLayout(null);

        JLabel lblHighscore = new JLabel();
        lblHighscore.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Highscore highscore = new Highscore();
                getContentPane().add(highscore);
                System.out.println();
                highscore.setVisible(true);

            }
        });
        //........
    }

    public void prepareAndShow() {
        //...
        Menue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Menue.setVisible(true);
    }

Then, from your main method, make sure you call it

public class Main {
    private static MyWindow window = null;

    public static void main(String[] args) {
        window = new MyWindow();
        // Frame sichtbar machen
        window.prepareAndShow();
    }

As an example

Rant warning

Normally, I would say avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify...

But that would lead into a rant about the pros and cons of WindowBuilder ...

Basically, you are better off learning to hand code your UI's, they will give you a better appreciation for how the layout managers work and how you can achieve complex and decoupled UI's as well as a UI which will look the way you expect it to on different platforms

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