代碼背後的想法是一個簡單的乘法遊戲,它產生2個數字,我必須輸入相關答案。JLabel面板不透明和一般背景着色
基本上,我的問題(s)爲(是):
- 當我做operacao.setOpaque(假);它什麼都不做,或者至少不是我所期望的那樣(http://puu.sh/pyVcE/813aa1843a.png - 灰色區域不應該是粉紅色,因爲背景是粉紅色的?)。與JLabels相同,setOpaque(false)在(在這種情況下)數字後留下灰色背景。
- 我有最後一個評論部分,因爲我看到有人在這裏說改變繪畫方法,它確實工作,但造成了一些奇怪的問題(當我啓動控制檯時繪製了所有內容,只有JTextField會清晰),然後我用setOpacity(1)「固定」它;的setBackground(粉紅色); - 這是做這件事的正確方法嗎?
public class Frame extends JFrame {
private JPanel panel, mensagem, operacao;
private JTextArea sucesso;
private JLabel numero1, numero2;
private JTextField resposta;
private Color pink = new Color(255, 213, 224);
//more vars
public Frame() {
super("Jogo de Multiplicar!");
setOpacity(1);
setBackground(pink);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setSize(300, 200);
panel = new JPanel();
mensagem = new JPanel();
operacao = new JPanel();
mensagem.setLayout(new FlowLayout(FlowLayout.CENTER));
operacao.setLayout(new FlowLayout(FlowLayout.CENTER));
sucesso = new JTextArea();
sucesso.setEditable(false);
sucesso.setOpaque(false);
sucesso.setFont(minhaFont2);
Random randomGen = new Random();
while (random1 == 0)
random1 = randomGen.nextInt(10);
while (random2 == 0)
random2 = randomGen.nextInt(10);
res = random1 * random2;
numero1 = new JLabel();
numero2 = new JLabel();
numero1.setText(random1 + " *");
numero2.setText(random2 + " =");
numero1.setOpaque(false);
numero1.setFont(minhaFont);
numero2.setFont(minhaFont);
resposta = new JTextField(2);
resposta.addActionListener(new MinhaAcao());
resposta.setFont(minhaFont);
operacao.add(numero1);
operacao.add(numero2);
operacao.add(resposta);
mensagem.add(sucesso);
operacao.setOpaque(true);
operacao.setBackground(pink);
mensagem.setOpaque(true);
mensagem.setBackground(pink);
//add(panel, BorderLayout.NORTH);
add(operacao);
add(mensagem, BorderLayout.SOUTH);
}/*
public void paint(Graphics g) {
g.setColor(pink);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}*/
1)爲了更好地提供幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 2)使用不需要鏡像核心類名稱的自定義類的明智名稱! –