【问题标题】:Java Clock issues [closed]Java时钟问题[关闭]
【发布时间】:2016-05-01 00:04:02
【问题描述】:

我正在尝试在我的代码中实现以下内容:

  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                   

}

【问题讨论】:

  • 你在这里同时关注很多事情。我建议您提出的问题一次只尝试解决您的一个问题。按照目前的情况,您的问题可能会因为“过于宽泛”而被关闭。
  • 好的,我会编辑掉其他问题

标签: java swing netbeans


【解决方案1】:

每次我设置警报并单击“设置警报”时,我从下拉列表中选择的前导零不会注册到警报旁边的 jLabels 中。只显示数字。我该如何解决这个问题?

不要将数字与字符串混淆。前者代表数字的抽象概念,对于这个前导0是没有意义的,而后者则是显示数字给用户看。

我认为解决此问题的最佳和最简单的方法是甚至不必担心用户输入的前导 0,而是提前决定您希望时钟显示多少位数,包括需要时的前导 0,并显示所有那些数字。这可以通过String.format("%05d", number)DecimalFormat.format(...) 轻松完成。

作为附带建议,您的代码迫切需要使用数组或 ArrayList 来简化并提高效率。


这就是我尝试过的,但我的程序不断崩溃。我在 jButton1ActionPerformed 下的代码中的什么位置以及如何实现?这就是我输入它的方式: jLabel12.setText(String.format("%02d", outHr)); (jLabel 是我几个小时的标签)

重新阅读导致错误的代码,因为这不是您输入的方式。

【讨论】:

  • 谢谢,这就是我尝试过的,但我的程序不断崩溃。我在 jButton1ActionPerformed 下的代码中的什么位置以及如何实现?这就是我输入它的方式: jLabel12.setText(String.format("%02d", outHr)); (jLabel 是我几个小时的标签)
  • @RashmiPatel:为了获得最佳帮助,您应该创建并发布有效的minimal reproducible example。否则我们将很难运行和测试您的代码。
  • 我更新了代码,让它可以运行。
  • @RashmiPatel:显然没有先阅读我提供的链接就这样做了——不好。在这样做的过程中,您发布了一个巨大的程序,其中包含许多与手头的问题完全无关的代码,迫使志愿者费力地通过大量代码试图找到对问题有意义的代码。请善待那些在空闲时间帮助你的志愿者——创建一个真正的minimal reproducible example。再次请在发布之前阅读链接,因为它需要可运行,但要短 LOT
  • @RashmiPatel:见编辑回答
猜你喜欢
  • 2012-04-03
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
  • 2012-05-07
  • 2013-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多