![](/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.