0
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class HoorayWithTimer extends javax.swing.JFrame {

int counter;

public HoorayWithTimer() {
    initComponents();
}
/*
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    timerLabel = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(timerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 334, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(0, 271, Short.MAX_VALUE)
            .addComponent(timerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

    timerLabel.getAccessibleContext().setAccessibleName("timerLabel");

    pack();
}// </editor-fold>

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(HoorayWithTimer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(HoorayWithTimer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(HoorayWithTimer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(HoorayWithTimer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>


    Timer timer = new Timer(10000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            HoorayWithTimer.setVisible(false);
            HoorayWithTimer.dispose();

        }
    });

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new HoorayWithTimer().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JLabel timerLabel;
// End of variables declaration

}

所以是的,我从菜单中有一个菜单,您可以在 somone 获胜时启动 3 种不同的游戏 关闭万岁屏幕并返回仍然打开的游戏。菜单始终在后台保持打开状态,因为我也无法关闭或 dispose()。

总是出错:不能从静态上下文中引用非静态方法 setVisible(boolean)

我对java有点陌生,所以请耐心等待......哈哈

4

1 回答 1

0

接下来是问题:

JFrame1)您尝试在此处从静态上下文(您的main方法)调用非静态方法:

HoorayWithTimer.setVisible(false);
HoorayWithTimer.dispose();

2) 阅读有关变量的 Java 基础知识

3)你可以写下你的Timer喜欢:

    Timer timer = new Timer(10000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            HoorayWithTimer.this.setVisible(false);
            HoorayWithTimer.this.dispose();
        }
    });
    timer.start();

并在构造函数中使用它,在方法中initComponents。只需Timer在方法中删除您的初始化main,并将上面的代码放在pack()方法之后initComponents。在这种情况下,您的框架将在 10 秒后关闭。我认为它对你有帮助。

于 2013-11-15T07:45:28.423 回答