繁体   English   中英

在GUI中显示时间

[英]Display time in GUI

我正在开展一个非常简单的个人项目,但这是一个更大项目的一部分。 任何帮助表示赞赏。 这是我的代码和输出错误,当我运行它时,它显示一个大窗口和一个显示时间的单独小窗口。 正确的方法应该是在JLabel的大框架中显示时间。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//package Employees;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class DateShowAPII extends javax.swing.JFrame {

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

    JLabel = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel2.setText("Time");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(104, 104, 104)
            .addComponent(jLabel2)
            .addGap(28, 28, 28)
            .addComponent(JLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(179, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,    layout.createSequentialGroup()
            .addContainerGap(139, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(JLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2))
            .addGap(132, 132, 132))
    );

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

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {

    JFrame frame = new JFrame();

    String date = new SimpleDateFormat("hh:mm:ss").format(new Date());
    JLabel l = new JLabel(date);

    frame.getContentPane().add(l);

    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new DateShowAPII().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JLabel JLabel;
private javax.swing.JLabel jLabel2;
// End of variables declaration
}

DateShowAPII是一个框架(这是一个大框架),在main中,你创建另一个框架(并打包它) - 这是一个有时间的小框架。

相反,不要创建另一个框架并将JLabel添加到您创建的新DateShowAPII

1)你声明了两个JFrames

DateShowAPII扩展了javax.swing.JFrame

JFrame frame = new JFrame();

看起来与内容相同

2)使用Swing TimerSwing Action fire JLabel#setText(date)

3)不要将resersed Java方法名称用作变量或类名,这意味着

JLabel = new javax.swing.JLabel();

重命名为

myJLabel = new javax.swing.JLabel();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM