JLabel.setVisible在运行时

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

我有一个主要类,它以GridBagLayout可见性设置为JLabel来实例化false

我想在程序运行时将标签设置为可见,我已经尝试过了,但是不起作用。它将仅显示默认布局。

主类别:

gui = new gui();
gui.display();
gui.label.setVisible(true); 

Gridbag布局类:

public JFrame frame;
public JLabel label1; 


/**
 * Launch the application.
 */
public static void display(){
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                 gridLayout window = new gridLayout();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


 * Create the application.
 */
public gridLayout() {
    initialize();

}

/**
 * Initialize the contents of the frame.
 */
@SuppressWarnings("static-access")
public void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 600, 1000);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridBagLayout gridBagLayout = new GridBagLayout();
    frame.getContentPane().setLayout(gridBagLayout);

}

label1 = new JLabel(new ImageIcon("hi"));
GridBagConstraints gbc_label1 = new GridBagConstraints();
gbc_label1.insets = new Insets(0, 0, 5, 5);
gbc_label1.gridx = 1;
gbc_label1.gridy = 1;
label1.setVisible(false); 
frame.getContentPane().add(label1, gbc_label1);
java swing jlabel layout-manager gridbaglayout
1个回答
0
投票

您想在程序运行时显示标签,对吗?这与布局管理器无关。我举一个例子,只要显示对话框(代表您的任务/程序),标签就可见。我希望您可以根据需要采用它。可能您必须将程序/任务放在自己的线程中。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Y extends JFrame {
  public static final long serialVersionUID = 100L;

  public Y() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 240);

    JLabel lb= new JLabel("Programme is running ...");
    lb.setVisible(false);
    add(lb, BorderLayout.CENTER);
    JButton b= new JButton("Launch programme (dialog)");
    b.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        lb.setVisible(true);
        JDialog dlg= new JDialog(Y.this, "The dialog", true);
        dlg.setSize(100, 100);
        dlg.setVisible(true);
        lb.setVisible(false);
      }
    });
    add(b, BorderLayout.SOUTH);
    setVisible(true);
  }


  static public void main(String args[]) {
    EventQueue.invokeLater(Y::new);
  }

}
© www.soinside.com 2019 - 2024. All rights reserved.