gpt4 book ai didi

java - 向 jframe 添加更多容器

转载 作者:行者123 更新时间:2023-11-29 08:08:35 25 4
gpt4 key购买 nike

我怎样才能将更多容器添加到我的 Jframe 中?这是我的代码行,我想在一个窗口中制作一个时钟,该窗口包含同一 jframe 一侧的其他时钟,这是我的代码:

 import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
public class CopyOftheclock {
public static void main(String[] args) {
JFrame clock = new TextClockWindow();
clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clock.setVisible(true);
}
}


@SuppressWarnings("serial")
class TextClockWindow extends JFrame {
private JTextField timeField;
public TextClockWindow() {
timeField = new JTextField(7);
timeField.setFont(new Font("sansserif", Font.PLAIN, 48));

Container content = this.getContentPane();
content.setLayout(new FlowLayout());
content.add(timeField);

this.setTitle("Norway");
this.pack();
javax.swing.Timer t = new javax.swing.Timer(1000,
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String a = "";
Calendar now = Calendar.getInstance();
int h = now.get(Calendar.HOUR_OF_DAY);
if (h==24)
{
h=8;
a = "A.M";
}
if (h==1)
{
h=9;
a = "A.M";
}
if (h==2)
{
h=10;
a = "A.M";
}
if (h==3)
{
h=11;
a = "A.M";
}
if (h==4)
{
h=12;
a = "P.M";
}
if (h==5)
{
h=1;
a = "P.M";
}
if (h==6)
{
h=2;
a = "P.M";
}
if (h==7)
{
h=3;
a = "P.M";
}
if (h==8)
{
h=4;
a = "P.M";
}
if (h==9)
{
h=5;
a = "P.M";
}
if (h==10)
{
h=6;
a = "P.M";
}
if (h==11)
{
h=7;
a = "P.M";
}
if (h==12)
{
h=8;
a = "P.M";
}
if (h==13)
{
h=9;
a = "P.M";
}
if (h==14)
{
h=10;
a = "P.M";
}
if (h==15)
{
h=11;
a = "P.M";
}
if (h==16)
{
h=12;
a = "P.M";
}
if (h==17)
{
h=1;
a = "A.M";
}
if (h==18)
{
h=2;
a = "A.M";
}
if (h==19)
{
h=3;
a = "A.M";
}
if (h==20)
{
h=4;
a = "A.M";
}
if (h==21)
{
h=5;
a = "A.M";
}
if (h==22)
{
h=6;
a = "A.M";
}
if (h==23)
{
h=7;
a = "A.M";
}
int m = now.get(Calendar.MINUTE);
int s = now.get(Calendar.SECOND);
timeField.setText("" + h + ":" + m + ":" + s + " " + a);
}

});
t.start();
}
}

如果你们能帮我实现这个功能,我将不胜感激!

最佳答案

1) 显示不可编辑的文本使用JLabel而不是 JTextField .

2) 代码少,使用SimpleDateFormat的方法.

3) 使用正确的 LayoutManager ;在你的情况下(也许)GridLayout将使所有 JComponent 在屏幕上具有相同的 Dimension

4) main public static void main(String[] args) { 中的所有 GUI 相关代码都应该包装到 invokeLater() 中;更多信息 Initial Threads .

5) 也许剩下的this thread could be useful .

关于java - 向 jframe 添加更多容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9537889/

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