我正在尝试创建一个显示在JLabel内部的计时器,我知道代码本身就像我用System.out.println一样尝试它,但我无法弄清楚如何让其他类看到JLabel的。
我是一个完全的初学者,并且自昨天上午以来一直试图解决这个问题。我尝试使用Google搜索,查看视频教程,阅读教程,查找类似问题,使用getter和setter,使用不同的变量。什么都没有帮助。我确实意识到这是一个非常容易的问题,但我无法弄清楚,我真的很感激任何形式的帮助。
包含计时器代码的类:
package Timernew;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class newTimer {
Timer timer = null;
int counter = 5;
int delay = 1500;
//method that will be called when the timer needs to start
public void startTimer() {
ActionListener action = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (counter == 0) {
timer.stop();
lblTimerE.setText("Time up");
} else {
lblTimerE.setText("Wait for " + counter + " sec");
counter--;
}
}
}; //end of action performed statement
timer = new Timer(delay, action);
timer.setInitialDelay(0);
timer.start(); //this will start the timer
} //end of method
}
包含JLabel和触发按钮的类:
package Timernew;
import javax.swing.JLabel;
public class Easy extends javax.swing.JFrame {
newTimer time = new newTimer();
/**
* Creates new form Easy
*/
public Easy() {
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() {
jPanel1 = new javax.swing.JPanel();
lblTimerE = new javax.swing.JLabel();
btnStart = new javax.swing.JButton();
lblWordE = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(204, 255, 0));
lblTimerE.setText(" ");
btnStart.setText("Start");
btnStart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnStartActionPerformed(evt);
}
});
lblWordE.setText(" ");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(134, 134, 134)
.addComponent(lblTimerE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 243, Short.MAX_VALUE)
.addComponent(btnStart)
.addGap(58, 58, 58))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(196, 196, 196)
.addComponent(lblWordE, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(45, 45, 45)
.addComponent(lblTimerE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(60, 60, 60)
.addComponent(btnStart)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 133, Short.MAX_VALUE)
.addComponent(lblWordE, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(54, 54, 54))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
time.startTimer();
}
/**
* @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(Easy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Easy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Easy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Easy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Easy().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JButton btnStart;
public javax.swing.JPanel jPanel1;
public javax.swing.JLabel lblTimerE;
public javax.swing.JLabel lblWordE;
// End of variables declaration
}
错误
cannot find symbol
symbol: variable lblTimerE
答案 0 :(得分:0)
我复制了你的代码并稍微调整了一下:因为你似乎需要在def writeToFile(jsObject: JsObject, fileName: String): Unit = {
println("writing "+fileName)
val pw = new PrintWriter(new File(fileName))
pw.write(jsObject.toString())
pw.close()
}
substitutedJsObjects.zipWithIndex.foreach {
case (jsObject, index) => {
val fileName = index.toString + ".json"
writeToFile(jsObject, fileName)
}
}
类中使用你的JLabel,所以我将它作为构造函数参数传递。我没有碰到剩下的代码,因为它不是问题的一部分。
NewTimer