![](/img/trans.png)
[英]JButton, JTextFeild, JLabel set background color not working
[英]Java JLabel background color not working?
我正在学习如何用Java创建应用程序。
我很难让JLabel具有背景色,而JPanel在它后面是白色。 另外,有没有办法将JPanel的大小调整为JFrame的一半?
任何帮助将不胜感激。 谢谢。
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);
}
}
当JPanel为白色时,我无法让JLabel具有背景色
您需要调用setOpaque(true);
在您的JLabel
另外,有没有办法将JPanel的大小调整为JFrame的一半?
您可以使用GridLayout
,并在其中放置2个JPanel
,那样,您将拥有2个JPanel
,每个JPanel
的大小是JFrame
一半。
同样,重命名您的类, Panel
属于AWT中的类的名称,对于Frame
和Label
,这可能会使您(以及读取您代码的人)感到困惑。
切勿扩展JFrame
,而应基于JPanel
构建GUI。 请参见扩展JFrame与在类内部创建它,以及使用多个JFrame,好/不好的做法? 普遍共识认为这很糟糕。
另外,您还应该检查是否应该避免在Swing中使用setPreferred | Maximum | MinimumSize()? 同样,是的,您应该而不是重写getPreferredSize()
方法。
不要忘记通过如下更改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
});
}
现在,有了以上所有建议,您的代码现在应如下所示:
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);
}
}
您的输出将如下所示:
请注意,我添加了一些彩色边框以显示窗格的开始和结束位置以及其他窗格的开始和结束位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.