JLabel面板不透明度和一般背景着色

时间:2016-06-20 01:36:34

标签: java swing jframe opacity

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

基本上,我的问题是(是):

  1. 当我做operacao.setOpaque(假);它没有做任何事情,或者至少不是我期望它做的事情(http://puu.sh/pyVcE/813aa1843a.png - 灰色区域不应该是粉红色的,因为背景是粉红色的吗?)。 JLabels也是如此,setOpaque(false)在(在本例中)数字后面留下灰色背景。
  2. 我有最后一个评论部分,因为我看到周围的人说要改变绘制方法,它确实有效,但是引起了一些奇怪的问题(当我启动控制台时,所有内容都被绘制,只有JTextField会清除),然后我用setOpacity(1)“修复”它;的setBackground(粉红色); - 这是一种正确的做法吗?
  3.  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 个答案:

答案 0 :(得分:3)

你需要将textfield变成粉红色。你可能必须这样做。

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. 创建一个透明面板(将面板的Opaque设置为false)。
  3. 将JTextfield的不透明设置为false。