【发布时间】:2016-10-21 04:21:16
【问题描述】:
代码背后的想法是一个简单的乘法游戏,它生成 2 个数字,我必须输入正确的答案。
基本上,我的问题是:
- 当我执行operacao.setOpaque(false);它什么也不做,或者至少不是我期望它做的(http://puu.sh/pyVcE/813aa1843a.png - 灰色区域不应该是粉红色的,因为背景是粉红色的吗?)。 JLabels 也是如此,setOpaque(false) 在(在这种情况下)数字后面留下灰色背景。
- 我有最后一个评论部分,因为我看到这里有人说要改变绘制方法,它确实有效,但引起了一些奇怪的问题(当我启动控制台时,所有东西都被绘制,只有 JTextField 会被清除),然后我用 setOpacity(1) “修复”它;设置背景(粉红色); - 这是正确的做法吗?
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) 为了尽快获得更好的帮助,请发帖 minimal reproducible example 或 Short, Self Contained, Correct Example。 2) 为不反映核心类名称的自定义类使用合理的名称!