[英]Java JLabel background color not working?
I'm learning how to create application in Java. 我正在学习如何用Java创建应用程序。
I'm having trouble getting the JLabel to have a background color whilst the JPanel is white, behind it. 我很难让JLabel具有背景色,而JPanel在它后面是白色。 Also, is there a way to resize the JPanel to half of what the JFrame is? 另外,有没有办法将JPanel的大小调整为JFrame的一半?
Any help would be very much appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。
package PracticeOne;
import java.awt.BorderLayout;
public class PracticeOne {
public static void main(String[] args) {
Frame container = new Frame();
Panel box = new Panel();
Label txt = new Label();
box.add(txt);
container.add(box, BorderLayout.CENTER);
}
}
package PracticeOne;
import javax.swing.JFrame;
public class Frame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
Frame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setTitle("Testing this out");
}
}
package PracticeOne;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
public class Panel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public Dimension d = new Dimension(100,100);
Panel(){
this.setSize(d);
this.setAlignmentX(CENTER_ALIGNMENT);
this.setBackground(Color.WHITE);
}
}
package PracticeOne;
import java.awt.Color;
import javax.swing.JLabel;
public class Label extends JLabel {
/**
*
*/
private static final long serialVersionUID = 1L;
Label(){
this.setSize(50, 50);
this.setText("ya boy is working here");
this.setForeground(Color.BLACK);
this.setBackground(Color.ORANGE);
}
}
I'm having trouble getting the JLabel to have a background color whilst the JPanel is white 当JPanel为白色时,我无法让JLabel具有背景色
You need to call setOpaque(true);
您需要调用setOpaque(true);
in your JLabel
在您的JLabel
Also, is there a way to resize the JPanel to half of what the JFrame is? 另外,有没有办法将JPanel的大小调整为JFrame的一半?
You could use a GridLayout
, and place 2 JPanel
s in it, that way, you're going to have 2 JPanel
s using half the size of your JFrame
each. 您可以使用GridLayout
,并在其中放置2个JPanel
,那样,您将拥有2个JPanel
,每个JPanel
的大小是JFrame
一半。
Also, rename your classes, Panel
belongs to the name of a class in AWT, same for Frame
and Label
, this might confuse your (and whoever reads your code). 同样,重命名您的类, Panel
属于AWT中的类的名称,对于Frame
和Label
,这可能会使您(以及读取您代码的人)感到困惑。
Never extend JFrame
, instead build your GUI based on JPanel
s. 切勿扩展JFrame
,而应基于JPanel
构建GUI。 See extends JFrame vs creating it inside of class and The use of multiple JFrames, Good / Bad practice? 请参见扩展JFrame与在类内部创建它,以及使用多个JFrame,好/不好的做法? The general consensus says it's bad. 普遍共识认为这很糟糕。
Also you should also check Should I avoid the use of setPreferred|Maximum|MinimumSize() in Swing? 另外,您还应该检查是否应该避免在Swing中使用setPreferred | Maximum | MinimumSize()? Again, yes, you should and instead override the getPreferredSize()
method. 同样,是的,您应该而不是重写getPreferredSize()
方法。
Don't forget to place your program on the Event Dispatch Thread (EDT) by changing your main()
method as follows: 不要忘记通过如下更改main()
方法将程序放在事件调度线程(EDT)上 :
public static void main(String[] args) {
//Java 8 with lambda expressions
SwingUtilities.invokeLater(() ->
//Your code here
);
//Java 7 and below (Or 8 without lambda expressions)
SwingUtilities.invokeLater(new Runnable() {
//Your code here
});
}
Now, with all the above recommendations, your code should now look like this: 现在,有了以上所有建议,您的代码现在应如下所示:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class HalfSizePanelWithLabelInDifferentColor {
private JFrame frame;
private Container contentPane;
private JPanel pane;
private JPanel pane2;
private JLabel label;
private static final Dimension dim = new Dimension(100, 100);
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new HalfSizePanelWithLabelInDifferentColor().createAndShowGui());
}
public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
contentPane = frame.getContentPane();
pane = new JPanel() {
@Override
public Dimension getPreferredSize() {
return dim;
}
};
pane2 = new JPanel();
pane.setOpaque(false);
pane2.setOpaque(false);
pane.setBorder(BorderFactory.createLineBorder(Color.RED));
pane2.setBorder(BorderFactory.createLineBorder(Color.BLUE));
label = new JLabel("Hello World!");
label.setBackground(Color.GREEN);
label.setOpaque(true);
contentPane.setLayout(new GridLayout(2, 1));
pane.add(label);
contentPane.add(pane);
contentPane.add(pane2);
contentPane.setBackground(Color.WHITE);
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
And your output would be like this: 您的输出将如下所示:
Note that I added some colored borders to show where a pane starts and ends and where the other one starts and ends 请注意,我添加了一些彩色边框以显示窗格的开始和结束位置以及其他窗格的开始和结束位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.