我一直在尝试制作一个相对于窗口大小大小的 JLabel,但由于某种原因,这个 JLabel 没有出现在屏幕上。
这是包含基本界面的 MainGUI 类中使用的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainGUI extends JFrame{
JPanel core;
GridBagConstraints c;
JLabel[] sts;
public MainGUI(){
core = new JPanel(new GridBagLayout());
getContentPane().add(core, BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
sts = new JLabel[10];
int width = (int)(66/100) * getWidth(), height = (int)(75/100) * getHeight(); //problem: due to these sizes the JLabel is not appearing
for(int i = 0; i < sts.length; i++){
sts[i] = new JLabel("test");
sts[i].setOpaque(true);
sts[i].setBackground(Color.BLACK);
sts[i].setForeground(Color.BLACK);
sts[i].setPreferredSize(new Dimension(width,height)); //size being set
}
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
core.add(sts[1], c);
}
}
任何帮助找到解决方案将不胜感激,在此先感谢。