[英]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);
}
}
基本上,GUI會按照我的意願只是打開一個黑色背景的窗口,但不會顯示文本。 我一直在討論在GUI上設置文本的SO問題,它說使用JLabel和HTML來設置文本樣式。
這有什么問題? 為什么文本不顯示?
您尚未調用setLabels();
。 您可能必須將以下方法更改為:
public void createMyGui() {
p.setLayout(new GridBagLayout());
c.add(p);
setButtons();
setGuiBackground();
setLabels();
j.setVisible(true);
p.setVisible(true);
}
這有效,我擺脫了容器並調用setLabels()
桂類{
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.