如何在if语句中使用setOpaque(true/false)。

huangapple go评论83阅读模式
英文:

How to use setOpaque(true/false) in an if statement

问题

import java.awt.Color;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.*;

public class TestOpaque {

    public static void main (String args[])
    {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JLabel label = new JLabel("Label with blue background");
        label.setBackground(Color.BLUE);
        label.setOpaque(false);
        frame.add(label, BorderLayout.WEST);
        
        JButton button = new JButton("Button");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                if (label.isOpaque() == false) {
                    label.setOpaque(true);
                    label.revalidate();
                }
            }
        });
        frame.add(button, BorderLayout.EAST);
        
        frame.pack();
        frame.setVisible(true);
    }
}
英文:

My JLabel won't change to a blue background. The JLabel is already set to the blue background but it is not opaque until you press the button. Why is it still not opaque?
Does setOpaque work for if statements?

import java.awt.Color;
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.event.*;

public class TestOpaque {

public static void main (String args[])
{
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JLabel label = new JLabel("Label with blue background");
    label.setBackground(Color.BLUE);
    label.setOpaque(false);
    frame.add(label, BorderLayout.WEST);
    
    JButton button = new JButton("Button");
    button.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e)
    	{
    		if (label.isOpaque() == false) {
    		label.setOpaque(true);
    		label.revalidate();
    		}
    	}
    });
    frame.add(button, BorderLayout.EAST);
    
    frame.pack();
    frame.setVisible(true);
}
}

答案1

得分: 1

if语句运行良好,但最好使用 if (!label.isOpaque()) {

您需要通过 repaint() 重新绘制GUI组件以显示背景:

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if (!label.isOpaque()) {
            label.setOpaque(true);
            label.revalidate();
            label.repaint();
        }
    }
});
英文:

The if statement works fine, although better to use if (!label.isOpaque()) {

You need to redraw the GUI component via repaint() for the background to show:

	button.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if (!label.isOpaque()) {
				label.setOpaque(true);
				label.revalidate();
				label.repaint();
			}
		}
	});

huangapple
  • 本文由 发表于 2020年8月22日 23:20:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63537820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定