【问题标题】:JLabel panel opacity and general background coloringJLabel 面板不透明度和一般背景着色
【发布时间】:2016-10-21 04:21:16
【问题描述】:

代码背后的想法是一个简单的乘法游戏,它生成 2 个数字,我必须输入正确的答案。

基本上,我的问题是:

  1. 当我执行operacao.setOpaque(false);它什么也不做,或者至少不是我期望它做的(http://puu.sh/pyVcE/813aa1843a.png - 灰色区域不应该是粉红色的,因为背景是粉红色的吗?)。 JLabels 也是如此,setOpaque(false) 在(在这种情况下)数字后面留下灰色背景。
  2. 我有最后一个评论部分,因为我看到这里有人说要改变绘制方法,它确实有效,但引起了一些奇怪的问题(当我启动控制台时,所有东西都被绘制,只有 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());
    }*/

【问题讨论】:

标签: java swing jframe opacity


【解决方案1】:

您需要将文本字段设为粉红色。您可能必须这样做。

resposta.setOpaque(false);

我已经重构了你的代码,如下所示。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

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!");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setSize(300, 200);
        getContentPane().setBackground(pink);
        panel = new TransperantPanel();
        mensagem = new TransperantPanel();
        operacao = new TransperantPanel();
        mensagem.setLayout(new FlowLayout(FlowLayout.CENTER));
        operacao.setLayout(new FlowLayout(FlowLayout.CENTER));

        sucesso = new JTextArea();
        sucesso.setEditable(false);

        Random randomGen = new Random();
        int random1 =0 , random2 = 0;   
        while (random1 == 0)
            random1 = randomGen.nextInt(10);
        while (random2 == 0)
            random2 = randomGen.nextInt(10);
        int res = random1 * random2;

        numero1 = new JLabel();
        numero2 = new JLabel();
        numero1.setText(random1 + " *");
        numero2.setText(random2 + " =");    


        resposta = new JTextField(2);
        resposta.setOpaque(false);
        resposta.addActionListener(new MinhaAcao());
        numero1.setFont(minhaFont);
        numero2.setFont(minhaFont);
        resposta.setFont(minhaFont);

        operacao.add(numero1);
        operacao.add(numero2);
        operacao.add(resposta);

        mensagem.add(sucesso);

        add(operacao);
        add(mensagem, BorderLayout.SOUTH);

    }

    public static void main(String[] args){
        Frame f = new Frame();
        f.setVisible(true);
    }

    class TransperantPanel extends JPanel {

        public TransperantPanel() {
            setOpaque(false);
        }

    }
}

我所做的是

  1. 将背景设置为框架的 contentPane。
  2. 创建了一个透明面板(将面板的不透明设置为 false)。
  3. 将 JTextfield 的不透明设置为 false。

【讨论】:

  • 是的,已经解决了,非常感谢!虽然,小问题,这个新类在commapred时有什么不同,例如panel = new JPanel(); panel.setOpaque(false);因为我很难找到差异。也许我第一次做错了什么?还是谢谢!
  • @ASDF 它没有做任何事情,只是避免了一些样板代码。如果假设有许多面板共享相同的属性。
猜你喜欢
  • 2014-08-03
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
  • 2012-07-27
  • 2017-03-15
  • 1970-01-01
相关资源
最近更新 更多