在 sfml 中打印块

Print blocks in sfml

我正在 java 中编写一个程序,当我单击按钮时我想通过新的 window 但是当我 运行 程序时它会自动打开我如果我不按 button.What 也有 2 个视图,我可以做些什么来解决这个问题吗?

主要:

package com.SimplyGeometry.src.tiles;

import com.SimplyGeometry.src.windows.SelectWindow;
import com.SimplyGeometry.src.windows.StartWindow;

public class Main {

    public static void main(String[] args) {
        String title = "SimplyGeometry";
        int WIDTH = 1320, HEIGHT = 840;
        StartWindow stw = new StartWindow(/*WIDTH, HEIGHT, title*/);
    }

}

Vew1:

package com.SimplyGeometry.src.windows;


import javax.swing.*;

import Actions.Actions;


public class StartWindow implements ActionListener {

    public static JFrame window;
    SelectWindow sw;

    public StartWindow(/*int WIDTH, int HEIGHT, String title*/) {

        window = new JFrame("SimplyGeometry");
        window.setPreferredSize(new Dimension(1320, 840));
        window.setMaximumSize(new Dimension(1320, 840));
        window.setMinimumSize(new Dimension(1320, 840));
        window.setLocationRelativeTo(null);
        window.setResizable(false);

        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(0, 0, 1320, 840);
        panel.setBackground(Color.CYAN);

        JLabel label = new JLabel();
        label.setIcon(new ImageIcon("/Users/gaetanodonnarumma/Documents/workspace/SimplyGeometry(Complete)/src/Images/titolo.png"));
        label.setSize(650, 250);
        label.setLocation(320, 100);

        JButton button = new JButton("Start");
        button.setBackground(Color.white);
        button.setForeground(Color.black);
        button.setSize(350, 100);
        button.setLocation(455, 450);
        button.setEnabled(true);
        button.addActionListener(new Actions());

        window.add(panel);
        panel.add(label);
        window.validate();
        panel.add(button);

        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }
}

视图 2:

package com.SimplyGeometry.src.windows;

import java.awt.Dimension;

import javax.swing.*;

public class SelectWindow {

    public static JFrame window;

    public SelectWindow(/*int WIDTH, int HEIGHT, String title*/) {
        window = new JFrame("SimplyGeometry");
        window.setPreferredSize(new Dimension(1320, 840));
        window.setMaximumSize(new Dimension(1320, 840));
        window.setMinimumSize(new Dimension(1320, 840));
        window.setLocationRelativeTo(null);
        window.setLayout(null);

        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

actionListener class:

package Actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.SimplyGeometry.src.windows.SelectWindow;
import com.SimplyGeometry.src.windows.StartWindow;

public class Actions implements ActionListener {

    public Actions() {
        SelectWindow window = new SelectWindow();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

}

它的发生是因为下面一行:

public StartWindow(
    ...
    button.addActionListener(new Actions());
    ...
}

Actions的构造函数中,SelectWindow正在创建。所以它会在您创建 StartWindow.

后出现在屏幕上
public Actions() {
    SelectWindow window = new SelectWindow();
}

要解决问题,请在 actionPerformed 中创建 SelectWindow

public class Actions implements ActionListener {

    public Actions() {
        //SelectWindow window = new SelectWindow();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        SelectWindow window = new SelectWindow();
    }

}