public void showTime() { Calendar c = Calendar.getInstance(); hour = Integer.toString(c.get(Calendar.HOUR)); minute = Integer.toString(c.get(Calendar.MINUTE)); second = Integer.toString(c.get(Calendar.SECOND)); aorp = Integer.toString(c.get(Calendar.AM_PM)); }我的代码里有这个,但是还是不显示时间
时间: 2024-02-11 21:07:48 浏览: 13
在你的代码中,`showTime()` 方法只是获取了当前的时间信息,并将其赋给了相应的变量,但是并没有更新窗口中的显示。为了实现时间显示的功能,你需要在 `showTime()` 方法中更新窗口中 `JLabel` 对象的文本,以反映当前的时间信息。
具体来说,你可以使用以下代码来更新 `JLabel` 对象的文本:
```
HH.setText(hour);
MM.setText(minute);
SS.setText(second);
AorP.setText(aorp);
```
这样,每次调用 `showTime()` 方法时,都会更新窗口中 `JLabel` 对象的文本,从而实现时间的显示功能。同时,你也需要确保在构造方法中将 `JLabel` 对象添加到窗口中,以便能够正确地显示时间。
相关问题
public void showTime() { p1 = new Panel(new GridLayout(1, 3)); Calendar c = Calendar.getInstance(); hour = Integer.toString(c.get(Calendar.HOUR)); minute = Integer.toString(c.get(Calendar.MINUTE)); second = Integer.toString(c.get(Calendar.SECOND)); aorp = Integer.toString(c.get(Calendar.AM_PM)); JLabel HH = new JLabel(hour, SwingConstants.CENTER); JLabel MM = new JLabel(minute, SwingConstants.CENTER); JLabel SS = new JLabel(second, SwingConstants.CENTER); JLabel AorP = new JLabel(aorp, SwingConstants.CENTER); HH.setFont(new Font(Font.SERIF+":", Font.ITALIC, 36)); MM.setFont(new Font(Font.SERIF+":", Font.ITALIC, 36)); SS.setFont(new Font(Font.SERIF, Font.ITALIC, 36)); AorP.setFont(new Font(Font.SERIF, Font.ITALIC, 26)); p1.add(HH); p1.add(MM); p1.add(SS); this.add(p1); this.add(AorP); }为什么不显示
你的 `showTime()` 方法中,你创建了一个 `Panel` 对象,并将 `JLabel` 添加到这个 `Panel` 中,然后将 `Panel` 添加到了 `JFrame` 窗口中,但是你还添加了一个 `JLabel` 到 `JFrame` 窗口中,这个 `JLabel` 并没有添加到任何一个容器中,因此不会被显示出来。你可以将这个 `JLabel` 添加到 `Panel` 中,然后将 `Panel` 添加到 `JFrame` 窗口中,就可以正常显示了。
另外,你需要注意的是,如果你的代码是在一个事件处理方法中执行的,例如 `actionPerformed()` 方法,你需要调用 `repaint()` 方法来强制重绘窗口,以显示新的组件。下面是修改后的代码:
```java
public void showTime() {
p1 = new Panel(new GridLayout(1, 3));
Calendar c = Calendar.getInstance();
hour = Integer.toString(c.get(Calendar.HOUR));
minute = Integer.toString(c.get(Calendar.MINUTE));
second = Integer.toString(c.get(Calendar.SECOND));
aorp = Integer.toString(c.get(Calendar.AM_PM));
JLabel HH = new JLabel(hour, SwingConstants.CENTER);
JLabel MM = new JLabel(minute, SwingConstants.CENTER);
JLabel SS = new JLabel(second, SwingConstants.CENTER);
JLabel AorP = new JLabel(aorp, SwingConstants.CENTER);
HH.setFont(new Font(Font.SERIF+":", Font.ITALIC, 36));
MM.setFont(new Font(Font.SERIF+":", Font.ITALIC, 36));
SS.setFont(new Font(Font.SERIF, Font.ITALIC, 36));
AorP.setFont(new Font(Font.SERIF, Font.ITALIC, 26));
p1.add(HH);
p1.add(MM);
p1.add(SS);
this.add(p1);
p1.add(AorP); // 将 AorP 添加到 p1 中
this.repaint(); // 重绘窗口
}
```
这里将 `AorP` 添加到了 `p1` 中,然后调用了 `repaint()` 方法来重绘窗口。这样就可以正常显示了。
public class TestClock extends JFrame { private String aorp; private String hour; private String minute; private String second; private Panel p1; public TestClock() { super("time"); this.setSize(1500, 1300); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLayout(new GridLayout(2, 1)); p1 = new Panel(new GridLayout(1, 3)); JLabel HH = new JLabel(hour, SwingConstants.CENTER); JLabel MM = new JLabel(minute, SwingConstants.CENTER); JLabel SS = new JLabel(second, SwingConstants.CENTER); JLabel AorP = new JLabel(aorp, SwingConstants.CENTER); HH.setFont(new Font(Font.SERIF+":", Font.ITALIC, 36)); MM.setFont(new Font(Font.SERIF+":", Font.ITALIC, 36)); SS.setFont(new Font(Font.SERIF, Font.ITALIC, 36)); AorP.setFont(new Font(Font.SERIF, Font.ITALIC, 26)); p1.add(HH); p1.add(MM); p1.add(SS); this.add(p1); this.add(AorP); this.repaint(); HH.setText(hour); MM.setText(minute); SS.setText(second); AorP.setText(aorp); } public void showTime() { Calendar c = Calendar.getInstance(); hour = Integer.toString(c.get(Calendar.HOUR)); minute = Integer.toString(c.get(Calendar.MINUTE)); second = Integer.toString(c.get(Calendar.SECOND)); aorp = Integer.toString(c.get(Calendar.AM_PM)); } public static void main(String[] args) { // TODO Auto-generated method stub TestClock clock = new TestClock(); while (true) { clock.showTime(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
在你的代码中,`TestClock` 的构造方法中已经将 `JLabel` 对象添加到了窗口中,但是你在 `main` 方法中也添加了一个 `while(true)` 循环,这可能会导致窗口无法正常显示。因此,你应该将 `while(true)` 循环删除,并使用 `Timer` 对象来定时更新窗口中的时间显示。
具体来说,你可以在 `TestClock` 的构造方法中添加以下代码:
```
Timer t = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showTime();
HH.setText(hour);
MM.setText(minute);
SS.setText(second);
AorP.setText(aorp);
}
});
t.start();
```
这样,每隔一秒钟,`Timer` 对象就会调用 `showTime()` 方法来更新时间信息,并将更新后的时间信息显示在窗口中。同时,你也需要将 `while(true)` 循环删除,并将 `TestClock` 对象的创建放在 `main` 方法中。
完整代码如下:
```
public class TestClock extends JFrame {
private String aorp;
private String hour;
private String minute;
private String second;
private Panel p1;
public TestClock() {
super("time");
this.setSize(1500, 1300);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(2, 1));
p1 = new Panel(new GridLayout(1, 3));
JLabel HH = new JLabel(hour, SwingConstants.CENTER);
JLabel MM = new JLabel(minute, SwingConstants.CENTER);
JLabel SS = new JLabel(second, SwingConstants.CENTER);
JLabel AorP = new JLabel(aorp, SwingConstants.CENTER);
HH.setFont(new Font(Font.SERIF + ":", Font.ITALIC, 36));
MM.setFont(new Font(Font.SERIF + ":", Font.ITALIC, 36));
SS.setFont(new Font(Font.SERIF, Font.ITALIC, 36));
AorP.setFont(new Font(Font.SERIF, Font.ITALIC, 26));
p1.add(HH);
p1.add(MM);
p1.add(SS);
this.add(p1);
this.add(AorP);
this.repaint();
HH.setText(hour);
MM.setText(minute);
SS.setText(second);
AorP.setText(aorp);
Timer t = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showTime();
HH.setText(hour);
MM.setText(minute);
SS.setText(second);
AorP.setText(aorp);
}
});
t.start();
}
public void showTime() {
Calendar c = Calendar.getInstance();
hour = Integer.toString(c.get(Calendar.HOUR));
minute = Integer.toString(c.get(Calendar.MINUTE));
second = Integer.toString(c.get(Calendar.SECOND));
aorp = Integer.toString(c.get(Calendar.AM_PM));
}
public static void main(String[] args) {
TestClock clock = new TestClock();
}
}
```
尝试运行这个程序,你就可以看到窗口中正确显示当前的时间了。