有没有一种方法可以在创建Swing GUI窗口时设置JLabel文本?

时间:2020-04-21 08:32:38

标签: java swing static jlabel

我正在使用Swing GUI制作表单程序。

现在我需要将一些文本自动设置为JLabel,但不能:

public static void main(String[] args) throws ParseException {

    JFrame frame = new JFrame("MainForm");
    frame.setContentPane(new MainForm().jpanel1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    // window position set
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int height = screenSize.height;
    int width = screenSize.width;
    frame.setLocation(width/2 - 100, height/2 - 100);

    lable_count.setText("please wait"); // this is not allow here because label is not static

    students = MongoDB.GetStudentFromMongo();
}

我该怎么办?

1 个答案:

答案 0 :(得分:0)

我是从马特(Matt)的彗星那里得到的

public static void main(String[] args) throws ParseException {

        MainForm form = new MainForm(); // <<add this line
        JFrame frame = new JFrame("MainForm");
        frame.setContentPane(form.jpanel1); // <<change this
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setTitle("学生基本信息");
        // window position set
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        frame.setLocation(width/2 - 100, height/2 - 100);

        form.lable_count.setText("please wait"); //<< then this will work
}