【问题标题】:Print blocks in sfml以 sfml 打印块
【发布时间】:2016-07-27 04:32:25
【问题描述】:

我正在用 java 做一个程序,当我单击按钮时我想在新窗口中传递,但是当我运行程序时,如果我不按下按钮,它也会自动打开 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 类:

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

    }

}

【问题讨论】:

    标签: java swing import sfml jsfml


    【解决方案1】:

    这是因为下面的行:

    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();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      相关资源
      最近更新 更多