有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java标签消失

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

例如: (无法显示图片)

  1. 下午13:00(这是经常出现的)课程A
  2. 下午13:30课程b

有时它会这样做:

  1. 下午13:00(总是出现)(然后什么都没有)
  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);


}

}


共 (1) 个答案

  1. # 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
    
    }
    
    }