2016-05-01 16 views
-3

を発行します。Javaの時計は、私は私のコードに次のように実装しようとしています

  1. 私はアラームを設定し、私は、ドロップダウンから選択先行ゼロはに登録されません「アラーム設定」をクリックするたびにjLabelsはアラームの隣にあります。数字だけが表示されます。これをどうすれば解決できますか?

更新コード:

package hw4; 

import java.awt.Color; 
import java.awt.Font; 
import javax.swing.JColorChooser; 
import javax.swing.UIManager; 
import javax.swing.JOptionPane; 

public class TypeAlarmClock extends javax.swing.JFrame { 

/** 
* Creates new form TypeAlarmClock 
*/ 
public TypeAlarmClock() { 
    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() { 

    jLabel1 = new javax.swing.JLabel(); 
    jLabel2 = new javax.swing.JLabel(); 
    jLabel3 = new javax.swing.JLabel(); 
    jLabel4 = new javax.swing.JLabel(); 
    jLabel5 = new javax.swing.JLabel(); 
    jLabel6 = new javax.swing.JLabel(); 
    jTextField1 = new javax.swing.JTextField(); 
    jTextField2 = new javax.swing.JTextField(); 
    jButton1 = new javax.swing.JButton(); 
    jLabel7 = new javax.swing.JLabel(); 
    jToggleButton1 = new javax.swing.JToggleButton(); 
    jLabel8 = new javax.swing.JLabel(); 
    jLabel9 = new javax.swing.JLabel(); 
    jLabel10 = new javax.swing.JLabel(); 
    jLabel11 = new javax.swing.JLabel(); 
    jLabel12 = new javax.swing.JLabel(); 
    jLabel13 = new javax.swing.JLabel(); 
    jButton2 = new javax.swing.JButton(); 
    jMenuBar1 = new javax.swing.JMenuBar(); 
    jMenu1 = new javax.swing.JMenu(); 
    jMenuItem1 = new javax.swing.JMenuItem(); 
    jSeparator1 = new javax.swing.JPopupMenu.Separator(); 
    jMenuItem2 = new javax.swing.JMenuItem(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    setTitle("Type Alarm Clock"); 

    jLabel1.setText("Clock:"); 

    jLabel2.setText("Alarm ON"); 

    jLabel3.setText("Alarm OFF"); 

    jLabel4.setText("Set Alarm:"); 

    jLabel5.setText("Hours"); 

    jLabel6.setText("Minutes"); 

    jButton1.setText("Set Alarm"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 

    jLabel7.setText("Alarm:"); 

    jToggleButton1.setText("Alarm On/Off"); 
    jToggleButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jToggleButton1ActionPerformed(evt); 
     } 
    }); 

    jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N 
    jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
    jLabel8.setText("10 :"); 

    jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N 
    jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
    jLabel9.setText("09 :"); 

    jLabel10.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N 
    jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
    jLabel10.setText("00"); 

    jLabel11.setText(":"); 

    jButton2.setText("Reset"); 
    jButton2.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton2ActionPerformed(evt); 
     } 
    }); 

    jMenu1.setText("Options"); 

    jMenuItem1.setText("Change background color"); 
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jMenuItem1ActionPerformed(evt); 
     } 
    }); 
    jMenu1.add(jMenuItem1); 
    jMenu1.add(jSeparator1); 

    jMenuItem2.setText("Change font"); 
    jMenuItem2.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jMenuItem2ActionPerformed(evt); 
     } 
    }); 
    jMenu1.add(jMenuItem2); 

    jMenuBar1.add(jMenu1); 

    setJMenuBar(jMenuBar1); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
        .addComponent(jLabel7) 
        .addGap(16, 16, 16) 
        .addComponent(jLabel12) 
        .addGap(18, 18, 18) 
        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(18, 18, 18) 
        .addComponent(jLabel13) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
         .addComponent(jToggleButton1) 
         .addGroup(layout.createSequentialGroup() 
          .addComponent(jLabel2) 
          .addGap(18, 18, 18) 
          .addComponent(jLabel3))) 
        .addGap(92, 92, 92)) 
       .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
         .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
           .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE) 
           .addGroup(layout.createSequentialGroup() 
            .addComponent(jLabel4) 
            .addGap(18, 18, 18) 
            .addComponent(jLabel5)) 
           .addGroup(layout.createSequentialGroup() 
            .addGap(68, 68, 68) 
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
           .addGroup(layout.createSequentialGroup() 
            .addGap(13, 13, 13) 
            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) 
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) 
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
            .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)) 
           .addGroup(layout.createSequentialGroup() 
            .addGap(24, 24, 24) 
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
             .addComponent(jLabel6) 
             .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))) 
          .addGap(0, 0, Short.MAX_VALUE)) 
         .addGroup(layout.createSequentialGroup() 
          .addGap(63, 288, Short.MAX_VALUE) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
           .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
           .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) 
        .addGap(23, 23, 23)))) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(51, 51, 51) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addGap(40, 40, 40) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jLabel2) 
       .addComponent(jLabel3) 
       .addComponent(jLabel7) 
       .addComponent(jLabel11) 
       .addComponent(jLabel12) 
       .addComponent(jLabel13)) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
      .addComponent(jToggleButton1) 
      .addGap(18, 18, 18) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jLabel4) 
       .addComponent(jLabel5) 
       .addComponent(jLabel6)) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jButton1)) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(jButton2) 
      .addContainerGap(22, Short.MAX_VALUE)) 
    ); 

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

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {           
    // Used to change background color 
    Color backgroundColor = JColorChooser.showDialog(this, "Choose background color", Color.white); 
    getContentPane().setBackground(backgroundColor); 
}           

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    // Take entered values for hours and minute and pass them into Alarm label 
    String hr = jTextField1.getText(); 
    int h = Integer.parseInt(hr); 
    String outHr = Integer.toString(h); 
    //System.out.println(a); 
    jLabel12.setText(String.format("%0d", outHr)); 
    jTextField1.setText(""); 

    String min = jTextField2.getText(); 
    int m = Integer.parseInt(min); 
    String outMin = Integer.toString(m); 
    //System.out.println(a); 
    jLabel13.setText(outMin); 
    jTextField2.setText(""); 
}           

private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {            
    // Change color of Alamr ON/OFF label when toggle butotn pressed 

    if(jToggleButton1.isSelected()){ 

     jLabel3.setForeground(Color.black); 
     jLabel2.setVisible(true); 
     jLabel2.setForeground(Color.red); 

    } 
    else{ 

     jLabel2.setForeground(Color.black); 
     jLabel3.setVisible(true); 
     jLabel3.setForeground(Color.red); 

    } 

}            

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {           
    // Change font of JLabels 
    Font font = new Font("Times New Roman", Font.BOLD, 14); 
    jLabel1.setFont(font); 
    jLabel2.setFont(font); 
    jLabel3.setFont(font); 
    jLabel4.setFont(font); 
    jLabel5.setFont(font); 
    jLabel6.setFont(font); 
    jLabel7.setFont(font); 

}           

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           
    // Blank out Alarm when Reset is pressed 
    jLabel12.setText(""); 
    jLabel13.setText(""); 
    JOptionPane.showMessageDialog(null, "The alarm has been reset"); 
}           

/** 
* @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(TypeAlarmClock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(TypeAlarmClock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(TypeAlarmClock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(TypeAlarmClock.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 TypeAlarmClock().setVisible(true); 
     } 
    }); 
} 

// Variables declaration - do not modify      
private javax.swing.JButton jButton1; 
private javax.swing.JButton jButton2; 
private javax.swing.JLabel jLabel1; 
private javax.swing.JLabel jLabel10; 
private javax.swing.JLabel jLabel11; 
private javax.swing.JLabel jLabel12; 
private javax.swing.JLabel jLabel13; 
private javax.swing.JLabel jLabel2; 
private javax.swing.JLabel jLabel3; 
private javax.swing.JLabel jLabel4; 
private javax.swing.JLabel jLabel5; 
private javax.swing.JLabel jLabel6; 
private javax.swing.JLabel jLabel7; 
private javax.swing.JLabel jLabel8; 
private javax.swing.JLabel jLabel9; 
private javax.swing.JMenu jMenu1; 
private javax.swing.JMenuBar jMenuBar1; 
private javax.swing.JMenuItem jMenuItem1; 
private javax.swing.JMenuItem jMenuItem2; 
private javax.swing.JPopupMenu.Separator jSeparator1; 
private javax.swing.JTextField jTextField1; 
private javax.swing.JTextField jTextField2; 
private javax.swing.JToggleButton jToggleButton1; 
// End of variables declaration     

}

+0

あなたは一度に多くのものに集中しています。あなたの質問を一度に解決することをお勧めします。現在のところ、あなたの質問は「広すぎる」として閉じられるかもしれません。 –

+0

ok私は他の質問を編集します –

答えて

1

私はアラームを設定し、私は、ドロップダウンから選択先行ゼロはに登録されません「アラーム設定」をクリックするたびにjLabelsはアラームの隣にあります。数字だけが表示されます。これをどうすれば解決できますか?

数字を文字列と混同しないでください。前者は数値の抽象概念を表し、後者はユーザが見るための数字の表示であるが、この先頭の0は無意味である。

これを解決する最も簡単な方法は、ユーザーが入力する先行0について心配することではなく、必要に応じて先頭の0を含めて時計に表示する桁数をあらかじめ決めておき、すべての数字。これはString.format("%05d", number)またはDecimalFormat.format(...)を使って簡単に行うことができます。

あなたのコードは、単純化してより効率的にするために、配列またはArrayListを使用するよう叫んでいます。


ので、それは私が試してみましたが、私のプログラムがクラッシュしたものでした。私のコードでjButton1ActionPerformedのどこで、私はそれを実装していますか?これは私がそれをタイプしたものです:jLabel12.setText(String.format( "%02d"、outHr));それはあなたがそれを入力したかないだから、エラーを引き起こしているあなたのコードを再読み込み

(JLabelのは時間のための私のラベルです)。

+0

ありがとう、それは私が試みたものでしたが、私のプログラムはクラッシュし続けます。私のコードでjButton1ActionPerformedのどこで、私はそれを実装していますか?これは私がそれをタイプしたものです:jLabel12.setText(String.format( "%02d"、outHr)); (jLabelは時間のラベルです) –

+0

@RashmiPatel:有効な[mcve]を作成して投稿してください。そうしないと、コードを実行してテストすることが難しくなります。 –

+0

実行できるようにコードを更新しました。 –

関連する問題