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种不同的游戏我想要弹出一个jFrame,说你用音乐等赢得了胜利 然后在万岁屏幕后10秒钟。关闭万岁屏幕并返回仍然开放的游戏。菜单总是在后台打开,因为我无法关闭或处理()。
总是收到错误: 非静态方法setVisible(boolean)不能从静态上下文引用
我对java很新,所以请耐心等待.... lol
答案 0 :(得分:0)
接下来是问题:
1)您尝试在此处从静态上下文(您的JFrame
方法)调用main
的非静态方法:
HoorayWithTimer.setVisible(false);
HoorayWithTimer.dispose();
2)阅读java basics about variables
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秒后关闭。我认为这有助于你。