我在NetBeans中工作,努力做我的項目,但我已經遇到了一個問題。基本上,我建立一個數字計時器,則顯示作爲毫秒作爲佈置在8.搖擺的問題與消失的JTextField
這樣的形狀7個文本框:
_
| |
_
| |
_
每個毫秒我使某些文本字段不可見的顯示不同的數。比我有一秒顯示(當毫秒計數到10時)顯示例如秒1,2,3 ...但由於某些原因,當毫秒達到10時,中間文本字段消失並且不再顯示。我不知道什麼可能會導致這個問題。
這裏是我的代碼:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
*
*/
public class TimerFrame extends javax.swing.JFrame {
/**
* Creates new form TimerFrame
*/
public TimerFrame() {
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() {
digitValue = new javax.swing.JTextField();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jTextField5 = new javax.swing.JTextField();
jTextField6 = new javax.swing.JTextField();
jTextField7 = new javax.swing.JTextField();
showDigit = new javax.swing.JButton();
AddOne = new javax.swing.JButton();
oneSecs = new javax.swing.JTextField();
showOneSecs = new javax.swing.JButton();
oneSecsDisplay = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Digital Timer");
setBackground(new java.awt.Color(70, 70, 70));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
digitValue.setText("000");
jTextField1.setBackground(new java.awt.Color(255, 0, 0));
jTextField1.setText(" ");
jTextField2.setBackground(new java.awt.Color(255, 0, 0));
jTextField2.setText(" ");
jTextField3.setBackground(new java.awt.Color(255, 0, 0));
jTextField3.setText(" ");
jTextField4.setBackground(new java.awt.Color(255, 0, 0));
jTextField4.setText(" ");
jTextField5.setBackground(new java.awt.Color(255, 0, 0));
jTextField5.setText(" ");
jTextField6.setBackground(new java.awt.Color(255, 0, 0));
jTextField6.setText(" ");
jTextField6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField6ActionPerformed(evt);
}
});
jTextField7.setBackground(new java.awt.Color(255, 0, 0));
jTextField7.setText(" ");
showDigit.setText("Show the Digit");
showDigit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
showDigitActionPerformed(evt);
}
});
AddOne.setText("Add One");
AddOne.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AddOneActionPerformed(evt);
}
});
oneSecs.setText("0");
showOneSecs.setText("Show One Secs");
showOneSecs.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
showOneSecsActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(60, 60, 60)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(226, 226, 226)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(8, 8, 8)
.add(oneSecsDisplay, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 96, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(62, 62, 62)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(layout.createSequentialGroup()
.add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(jTextField5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(28, 28, 28)
.add(jTextField6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jTextField7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(10, 10, 10))))))
.add(layout.createSequentialGroup()
.add(showOneSecs)
.add(6, 6, 6)
.add(AddOne)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(showDigit)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(digitValue, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.add(layout.createSequentialGroup()
.add(272, 272, 272)
.add(oneSecs, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(0, 0, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap(104, Short.MAX_VALUE)
.add(oneSecs, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(oneSecsDisplay, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 120, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(layout.createSequentialGroup()
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jTextField6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jTextField5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(7, 7, 7)
.add(jTextField7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, showOneSecs)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(digitValue, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(showDigit)
.add(AddOne)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jTextField6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void showDigitActionPerformed(java.awt.event.ActionEvent evt) {
// showDigit
int digit1 = Integer.parseInt(digitValue.getText());
jTextField1.setVisible(true);
jTextField2.setVisible(true);
jTextField3.setVisible(true);
jTextField4.setVisible(true);
jTextField5.setVisible(true);
jTextField6.setVisible(true);
jTextField7.setVisible(true);
if (digit1 == 0) {
jTextField4.setVisible(false);
} else if (digit1 == 1) {
jTextField1.setVisible(false);
jTextField2.setVisible(false);
jTextField4.setVisible(false);
jTextField5.setVisible(false);
jTextField7.setVisible(false);
} else if (digit1 == 2) {
jTextField2.setVisible(false);
jTextField6.setVisible(false);
} else if (digit1 == 3) {
jTextField2.setVisible(false);
jTextField5.setVisible(false);
} else if (digit1 == 4) {
jTextField1.setVisible(false);
jTextField5.setVisible(false);
jTextField7.setVisible(false);
} else if (digit1 == 5) {
jTextField3.setVisible(false);
jTextField5.setVisible(false);
} else if (digit1 == 6) {
jTextField3.setVisible(false);
} else if (digit1 == 7) {
jTextField2.setVisible(false);
jTextField4.setVisible(false);
jTextField5.setVisible(false);
jTextField7.setVisible(false);
} else if (digit1 == 9) {
jTextField5.setVisible(false);
}
}
private void AddOneActionPerformed(java.awt.event.ActionEvent evt) {
// Add One
int addOne = Integer.parseInt(digitValue.getText());
addOne = addOne + 1;
if (addOne == 10) {
addOne = 0;
int oneSecond = Integer.parseInt(oneSecs.getText());
oneSecond = oneSecond + 1;
String oneSeconds = Integer.toString(oneSecond);
oneSecs.setText(oneSeconds);
showOneSecsActionPerformed(evt);
}
String myString = Integer.toString(addOne);
digitValue.setText(myString);
showDigitActionPerformed(evt);
}
private void showOneSecsActionPerformed(java.awt.event.ActionEvent evt) {
// Show One Secs
int ones = Integer.parseInt(oneSecs.getText());
if (ones == 0) {
oneSecsDisplay.setIcon(new javax.swing.ImageIcon("0.jpg"));
} else if (ones == 1) {
oneSecsDisplay.setIcon(new javax.swing.ImageIcon("1.jpg"));
} else if (ones == 2) {
oneSecsDisplay.setIcon(new javax.swing.ImageIcon("2.jpg"));
} else if (ones == 3) {
oneSecsDisplay.setIcon(new javax.swing.ImageIcon("3.jpg"));
} else if (ones == 4) {
oneSecsDisplay.setIcon(new javax.swing.ImageIcon("4.jpg"));
} else if (ones == 5) {
oneSecsDisplay.setIcon(new javax.swing.ImageIcon("5.jpg"));
} else if (ones == 6) {
oneSecsDisplay.setIcon(new javax.swing.ImageIcon("6.jpg"));
} else if (ones == 7) {
oneSecsDisplay.setIcon(new javax.swing.ImageIcon("7.jpg"));
} else if (ones == 8) {
oneSecsDisplay.setIcon(new javax.swing.ImageIcon("8.jpg"));
} else if (ones == 9) {
oneSecsDisplay.setIcon(new javax.swing.ImageIcon("9.jpg"));
}
}
/**
* @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(TimerFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TimerFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TimerFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TimerFrame.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 TimerFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton AddOne;
private javax.swing.JTextField digitValue;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField6;
private javax.swing.JTextField jTextField7;
private javax.swing.JTextField oneSecs;
private javax.swing.JButton oneSecsDisplay;
private javax.swing.JButton showDigit;
private javax.swing.JButton showOneSecs;
// End of variables declaration
}
我已經採取了調整標題和標籤的自由,因爲這是一個真正的的Java Swing問題。 – madth3
你爲什麼使用JTextFields?只需將組成8形狀的條形圖繪製到JPanel的擴展上即可。 –
一秒內有超過10毫秒(準確地說1000)。 –