在创建Swing GUI窗口时是否可以设置JLabel文本?

问题描述 投票:0回答:1

我正在使用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();
}

我该怎么办?

java swing static jlabel
1个回答
0
投票

我从马特的漫画店得到

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
}
© www.soinside.com 2019 - 2024. All rights reserved.