gpt4 book ai didi

java - 在 GUI 中显示时间

转载 作者:行者123 更新时间:2023-11-29 03:47:31 24 4
gpt4 key购买 nike

我正在做一个非常简单的个人项目,但这是一个更大项目的一部分。任何帮助表示赞赏。这是我的代码,输出是错误的,当我运行它时,它显示一个大窗口和一个单独的小窗口显示时间。正确的方法应该是在 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 是一个框架(大框架),主要是您创建另一个框架(并将其打包)——这是随时间变化的小框架。

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

关于java - 在 GUI 中显示时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10253097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com