一段时间后关闭JFrame

时间:2014-12-09 05:00:31

标签: java swing timer jframe

所以我正在制作一个有3个难度的游戏:

  • 初级
  • 中级
  • 专家

在专家级别中,我想在60秒后关闭JFrame,无论是点击按钮开始还是任何其他方式,都不需要花哨。我已经尝试了很多方法,但我保持 得到我想要的东西。

这是我的代码:

package battleship;
import static battleship.Countdown.interval;
import static battleship.Countdown.timer;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class Test {
static int interval;
static Timer timer;

    public static void main(String[] args) {
        new Test();
  Scanner sc = new Scanner(System.in);
    System.out.print("Input seconds => : ");
    String secs = sc.nextLine();
    int delay = 1000;
    int period = 1000;
    timer = new Timer();
    interval = Integer.parseInt(secs);
    System.out.println(secs);
       timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {
            System.out.println(setInterval());

        }
    }, delay, period);
}

    private void setIcon1() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("ramboo.png")));
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                setInterval();
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("BATALLA NAVAL - NIVEL PRINCIPIANTE" );
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.getContentPane().setPreferredSize(new Dimension(900, 700));

                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    void setVisible(boolean b) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void setIconImage(Image image) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public class TestPane extends JPanel {

  public TestPane() {

            try {
           for (int i=0; i<=1; i++){
                add(crearespacios());
           }
           for (int i=0; i<=1; i++){
  add(crearbombas());
           }

                //agregar frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


            } 

            catch (IOException exp) {
                exp.printStackTrace();
            }
        }

        protected JToggleButton crearbombas() throws IOException {

            JToggleButton btn = new JToggleButton();
            btn.setModel(new StickyModel());
            btn.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("botondefault.png"))));
            btn.setSelectedIcon(new ImageIcon(ImageIO.read(getClass().getResource("bombaa.png"))));
            return btn;

        }

        protected JToggleButton crearespacios() throws IOException {

            JToggleButton btn = new JToggleButton();
            btn.setModel(new StickyModel());
            btn.setBounds(5, 5, 50, 50);
            btn.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("botondefault.png"))));
            btn.setSelectedIcon(new ImageIcon(ImageIO.read(getClass().getResource("botondefault.png"))));
            return btn;

        }

        protected JButton Instructions() throws IOException {

            JButton btnX = new JButton();
            btnX.setModel(new StickyModel());
            btnX.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("rambofinal.png"))));
            btnX.setSelectedIcon(new ImageIcon(ImageIO.read(getClass().getResource("rambofinal.png"))));
            return btnX;

        }

        protected JTextField Instrucciones() throws IOException {

            JTextField jtxt = new JTextField();
            JTextField jtxt2 = new JTextField();
            jtxt.setText("Existen 18 espacios, 5 barcos ocupan 6 de ellos.");
            jtxt2.setText("Tenes 16 clicks.");
            return jtxt;
        }



    }



private static final int setInterval() {
    if (interval == 1)
        timer.cancel();

    return --interval;

}


    public class StickyModel extends JToggleButton.ToggleButtonModel {

        public void reset() {
            super.setSelected(false);
        }

        @Override
        public void setSelected(boolean b) {
            if (!isSelected()) {
                super.setSelected(b);
            }
        }

    }

}

在这里尝试使用System.exit(0)System.runfinalization;,但它只显示了&#34;控制台输出(?)&#34;而不是框架

没有计时器:

package battleship;
import java.util.Timer;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();

    }

    private void setIcon1() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("ramboo.png")));
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("BATALLA NAVAL - NIVEL PRINCIPIANTE" );
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setPreferredSize(new Dimension(900, 700));

                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    void setVisible(boolean b) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void setIconImage(Image image) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public class TestPane extends JPanel {

  public TestPane() {

            try {
           for (int i=0; i<=1; i++){
                add(crearespacios());
           }
           for (int i=0; i<=1; i++){
  add(crearbombas());
           }

                //agregar frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


            } 

            catch (IOException exp) {
                exp.printStackTrace();
            }
        }

        protected JToggleButton crearbombas() throws IOException {

            JToggleButton btn = new JToggleButton();
            btn.setModel(new StickyModel());
            btn.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("botondefault.png"))));
            btn.setSelectedIcon(new ImageIcon(ImageIO.read(getClass().getResource("bombaa.png"))));
            return btn;

        }

        protected JToggleButton crearespacios() throws IOException {

            JToggleButton btn = new JToggleButton();
            btn.setModel(new StickyModel());
            btn.setBounds(5, 5, 50, 50);
            btn.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("botondefault.png"))));
            btn.setSelectedIcon(new ImageIcon(ImageIO.read(getClass().getResource("botondefault.png"))));
            return btn;

        }

        protected JButton Instructions() throws IOException {

            JButton btnX = new JButton();
            btnX.setModel(new StickyModel());
            btnX.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("rambofinal.png"))));
            btnX.setSelectedIcon(new ImageIcon(ImageIO.read(getClass().getResource("rambofinal.png"))));
            return btnX;

        }

        protected JTextField Instrucciones() throws IOException {

            JTextField jtxt = new JTextField();
            JTextField jtxt2 = new JTextField();
            jtxt.setText("Existen 18 espacios, 5 barcos ocupan 6 de ellos.");
            jtxt2.setText("Tenes 16 clicks.");
            return jtxt;
        }



    }

    public class StickyModel extends JToggleButton.ToggleButtonModel {

        public void reset() {
            super.setSelected(false);
        }

        @Override
        public void setSelected(boolean b) {
            if (!isSelected()) {
                super.setSelected(b);
            }
        }

    }

}

顺便说一句......游戏是战舰,我在该代码中创建了一个自定义按钮,这就是为什么我需要与按钮在同一个Frame.java中的计时器。

0 个答案:

没有答案