Java Label消失了

时间:2014-10-13 10:57:45

标签: java label frame

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

例如: (不能显示图片)

  1. 下午13:00(这是总是出现的)课程A
  2. 13:30 pm课程b
  3. 有时这样做:

    1. 下午13:00(总是出现)(然后什么都没有)
    2. 下午13:30
    3. (请保持简单,因为我是初学者,来自荷兰)。

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

      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);
      
      
      }
      

      }

1 个答案:

答案 0 :(得分:1)

frame.setVisible(true);移动到lastline将解决您的问题。您需要在添加组件后调用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

}

}