[英]JPanel won't add the JLabel text?
class Gui {
protected JFrame j = new JFrame("My First window");
protected JPanel p = new JPanel();
protected Container c;
private GridBagConstraints g = new GridBagConstraints();
public Gui() {
j.setSize(350, 250);
p.setSize(j.getSize());
this.c = j.getContentPane();
}
public void createMyGui() {
p.setLayout(new GridBagLayout());
c.add(p);
setButtons();
setGuiBackground();
j.setVisible(true);
p.setVisible(true);
}
private void setGuiBackground() {
p.setBackground(Color.black);
}
private void setButtons() {
}
private void setLabels() {
g.fill = GridBagConstraints.HORIZONTAL;
g.ipady = 40;
g.weightx = 5.0;
g.insets = new Insets(0,0,0,0);
g.gridwidth = 3;
g.gridx = 0;
g.gridy = 1;
JLabel l1 = new JLabel("<html>Text color: <font color='red'>Red!</font>");
p.add(l1, g);
}
}
Basically, the gui just opens a window, with a black background, as I wanted, but it does not show up the text. 基本上,GUI会按照我的意愿只是打开一个黑色背景的窗口,但不会显示文本。 I've been on a SO question, for setting up a text on a GUI, and it says to use JLabel, and HTML to style the text.
我一直在讨论在GUI上设置文本的SO问题,它说使用JLabel和HTML来设置文本样式。
What is wrong with this? 这有什么问题? Why won't the text show up?
为什么文本不显示?
You haven't called setLabels();
您尚未调用
setLabels();
. 。 You might have to change the following method into:
您可能必须将以下方法更改为:
public void createMyGui() {
p.setLayout(new GridBagLayout());
c.add(p);
setButtons();
setGuiBackground();
setLabels();
j.setVisible(true);
p.setVisible(true);
}
This works, I got rid of the container and call the setLabels() 这有效,我摆脱了容器并调用setLabels()
class Gui{ 桂类{
protected JFrame j = new JFrame("My First window");
protected JPanel p = new JPanel();
private GridBagConstraints g = new GridBagConstraints();
public Gui() {
j.setSize(350, 250);
p.setSize(j.getSize());
j.setContentPane(p);
createMyGui();
j.setVisible(true);
p.setVisible(true);
}
public void createMyGui() {
p.setLayout(new GridBagLayout());
setButtons();
setLabels();
setGuiBackground();
}
private void setGuiBackground() {
p.setBackground(Color.WHITE);
}
private void setButtons() {
}
private void setLabels() {
g.fill = GridBagConstraints.HORIZONTAL;
g.ipady = 40;
g.weightx = 5.0;
g.insets = new Insets(0,0,0,0);
g.gridx = 0;
g.gridy = 1;
JLabel l1 = new JLabel("<html>Text color: <font color=red>Red!</font></html>");
p.add(l1, g);
}
public static void main(String[] args){
standard s = new standard();
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.