gpt4 book ai didi

Java标签消失

转载 作者:行者123 更新时间:2023-12-01 12:25:53 25 4
gpt4 key购买 nike

我想制定一个简单的议程,但不知何故,当我运行编译器时,它不会加载标签的文本,有时会加载。我该如何解决?

示例:(无法显示图片)

  1. 13:00pm(这是总是出现的)类(class) A
  2. 下午 13:30 类(class) b

有时它会这样做:

  1. 13:00pm(总是出现)(然后什么也没有)
  2. 下午 13:30

(请保持简单,因为我是初学者,来自荷兰)。

代码:(我是初学者,所以不要看那些复制粘贴的东西)

import javax.swing.*;
import java.awt.*;

class P{
public static void main(String [] args){
JFrame frame = new JFrame(" Agenda 10/13/2014");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(620,620);
frame.setResizable(false);
frame.setVisible(true);

JLabel labeltime1 = new JLabel("1:00 - 1:30pm: ");
JLabel labeltime2 = new JLabel("1:30 - 2:00pm: ");
JLabel labeltime3 = new JLabel("2:00 - 2:30pm: ");
JLabel labeltime4 = new JLabel("2:30 - 3:00pm: ");
JLabel labeltime5 = new JLabel("3:00 - 3:30pm: ");

labeltime1.setForeground(Color.red);
labeltime2.setForeground(Color.red);
labeltime3.setForeground(Color.red);
labeltime4.setForeground(Color.red);
labeltime5.setForeground(Color.red);

JLabel space1 = new JLabel("\n");
JLabel space2 = new JLabel("\n");
JLabel space3 = new JLabel("\n");
JLabel space4 = new JLabel("\n");
JLabel space5 = new JLabel("\n");

JPanel timeP = new JPanel();
timeP.setBackground(Color.black);
timeP.setLayout(new BoxLayout(timeP, BoxLayout.Y_AXIS));

timeP.add(space5);
timeP.add(labeltime1);
timeP.add(space1);
timeP.add(labeltime2);
timeP.add(space2);
timeP.add(labeltime3);
timeP.add(space3);
timeP.add(labeltime4);
timeP.add(space4);
timeP.add(labeltime5);

frame.getContentPane().add(BorderLayout.WEST, timeP);

JPanel courses = new JPanel();
courses.setLayout(new BoxLayout(courses, BoxLayout.Y_AXIS));
courses.setBackground(Color.black);
frame.getContentPane().add(BorderLayout.CENTER,courses);

//Enter your course
JLabel course1 = new JLabel(" Course A");
JLabel course2 = new JLabel(" Course B");
JLabel course3 = new JLabel(" Course C");
JLabel course4 = new JLabel(" Course D");
JLabel course5 = new JLabel(" Course E");

course1.setForeground(Color.yellow);
course2.setForeground(Color.yellow);
course3.setForeground(Color.yellow);
course4.setForeground(Color.yellow);
course5.setForeground(Color.yellow);

JLabel space6 = new JLabel("\n");
JLabel space7 = new JLabel("\n");
JLabel space8 = new JLabel("\n");
JLabel space9 = new JLabel("\n");
JLabel space10 = new JLabel("\n");

courses.add(space6);
courses.add(course1);
courses.add(space7);
courses.add(course2);
courses.add(space8);
courses.add(course3);
courses.add(space9);
courses.add(course4);
courses.add(space10);
courses.add(course5);


}

}

最佳答案

frame.setVisible(true); 移动到最后一行将解决您的问题。您需要在添加组件后调用 setvisible 。或者您可以调用 repaint(),revalidate();

import javax.swing.*;
import java.awt.*;

class P{
public static void main(String [] args){
JFrame frame = new JFrame(" Agenda 10/13/2014");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(620,620);
frame.setResizable(false);
//frame.setVisible(true);//don't call this method here

JLabel labeltime1 = new JLabel("1:00 - 1:30pm: ");
JLabel labeltime2 = new JLabel("1:30 - 2:00pm: ");
JLabel labeltime3 = new JLabel("2:00 - 2:30pm: ");
JLabel labeltime4 = new JLabel("2:30 - 3:00pm: ");
JLabel labeltime5 = new JLabel("3:00 - 3:30pm: ");

labeltime1.setForeground(Color.red);
labeltime2.setForeground(Color.red);
labeltime3.setForeground(Color.red);
labeltime4.setForeground(Color.red);
labeltime5.setForeground(Color.red);

JLabel space1 = new JLabel("\n");
JLabel space2 = new JLabel("\n");
JLabel space3 = new JLabel("\n");
JLabel space4 = new JLabel("\n");
JLabel space5 = new JLabel("\n");

JPanel timeP = new JPanel();
timeP.setBackground(Color.black);
timeP.setLayout(new BoxLayout(timeP, BoxLayout.Y_AXIS));

timeP.add(space5);
timeP.add(labeltime1);
timeP.add(space1);
timeP.add(labeltime2);
timeP.add(space2);
timeP.add(labeltime3);
timeP.add(space3);
timeP.add(labeltime4);
timeP.add(space4);
timeP.add(labeltime5);

frame.getContentPane().add(BorderLayout.WEST, timeP);

JPanel courses = new JPanel();
courses.setLayout(new BoxLayout(courses, BoxLayout.Y_AXIS));
courses.setBackground(Color.black);
frame.getContentPane().add(BorderLayout.CENTER,courses);

//Enter your course
JLabel course1 = new JLabel(" Course A");
JLabel course2 = new JLabel(" Course B");
JLabel course3 = new JLabel(" Course C");
JLabel course4 = new JLabel(" Course D");
JLabel course5 = new JLabel(" Course E");

course1.setForeground(Color.yellow);
course2.setForeground(Color.yellow);
course3.setForeground(Color.yellow);
course4.setForeground(Color.yellow);
course5.setForeground(Color.yellow);

JLabel space6 = new JLabel("\n");
JLabel space7 = new JLabel("\n");
JLabel space8 = new JLabel("\n");
JLabel space9 = new JLabel("\n");
JLabel space10 = new JLabel("\n");

courses.add(space6);
courses.add(course1);
courses.add(space7);
courses.add(course2);
courses.add(space8);
courses.add(course3);
courses.add(space9);
courses.add(course4);
courses.add(space10);
courses.add(course5);
frame.setVisible(true);//call here

}

}

关于Java标签消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26338374/

25 4 0