JLabel在开始时不可见

时间:2018-10-30 19:18:17

标签: java swing jframe jlabel

我想拥有一个带有JLabel的JFrame,以显示当前状态。不幸的是,JLabel在一开始并不可见。

public class publishStatus extends JFrame{
JLabel JL;
int uploads = 0;
public publishStatus() {
    JL = new JLabel (uploads +" Upload");
    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(JL);

    setSize(300,200);
    setLocation(800,400);
    setVisible(true);
}

public void refresh() {
    uploads++;
    JL.setText(uploads +" Upload");

}

public static void main(String[] args) throws HeadlessException, Exception {
    publishStatus f = new publishStatus();
    System.setProperty("webdriver.gecko.driver", "geckodriver");
    WebDriver driver = new FirefoxDriver();
    WebDriverWait wait = new WebDriverWait(driver, 20);
f.refresh();

}}

所以我喜欢的行为是,框架加载后显示为“ 0 Upload”,然后在加载驱动程序后立即变为“ 1 Upload”。 预先感谢!

1 个答案:

答案 0 :(得分:0)

问题是,您启动应用程序后正在刷新JLabel的内容。因此,在刷新JLabel的那段时间看不到。

f.refresh(); -更新上传计数器并将最新值设置为JLabel。

您可能有一个名为“刷新”的按钮,并且可以将刷新操作附加到该按钮。这样,每当您单击Refresh按钮时,您都会看到JLabel正在刷新。

这是修改后的代码:

public class PublishStatus extends JFrame{
    JLabel JL;
    int uploads = 0;
    public PublishStatus() {
        JL = new JLabel (uploads +" Upload");
        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(JL);

        JButton button = new JButton("Refresh");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                uploads++;
                JL.setText(uploads +" Upload");
            }
        });

        getContentPane().add(button);

        setSize(300,200);
        setLocation(800,400);
        setVisible(true);
    }

    public static void main(String[] args) throws HeadlessException, Exception {
        PublishStatus f = new PublishStatus();
    }
}