单击 "submit" 按钮时如何设置半透明 jframe?

how to set semi-transparent jframe when "submit" button is clicked?

loadingLab=new JLabel("The name is being saved..");
loadPanel.add(loadingLab);
submitBttn=new JButton("Submit");
submitBttn.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {  
        System.out.println("Submit Button Clicked!!");
        try {
            //something is wrong in here as it throws an exception
            //what is wrong?
            frame.setUndecorated(false);
            frame.setOpacity(0.55f);

            //when above both lines are commented, the code works fine
            //but doesnt have transparency  
            frame.add(loadPanel,BorderLayout.SOUTH);
            frame.setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
});

我正在尝试在单击 "submit" 按钮时显示透明 JFrame,显示面板带有 JLabel... 我试过使用 setOpacity(0.55f),但它抛出异常..我做错了什么?

@Programmer007 wrote - the exception is " java.awt.IllegalComponentStateException: The frame is displayable."

  • 请在我看不到的地方提供更多信息about the possible exceptions to read

  • 如前所述不知道,一切都是靠自己的努力,转化为SSCCE/MCVE,简短,可运行,可编译

.

import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class GenericForm extends JDialog {

    private static final long serialVersionUID = 1L;
    private Timer timer;
    private JDialog dialog = new JDialog();
    private int count = 0;

    public GenericForm() {
        dialog.setSize(400, 300);
        dialog.setUndecorated(true);
        dialog.setOpacity(0.5f);
        dialog.setName("Toggling with opacity");
        dialog.getContentPane().setBackground(Color.RED);
        dialog.setLocation(150, 150);
        dialog.setVisible(true);
        timer = new javax.swing.Timer(1500, updateCol());
        timer.setRepeats(true);
        timer.start();
    }

    private Action updateCol() {
        return new AbstractAction("Hello World") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                boolean bol = dialog.getOpacity() < 0.55f;
                count += 1;
                if (count < 10) {
                    if (bol) {
                        dialog.setOpacity(1.0f);
                        dialog.getContentPane().setBackground(Color.WHITE);
                    } else {
                        dialog.setOpacity(0.5f);
                        dialog.getContentPane().setBackground(Color.RED);
                    }
                } else {
                    System.exit(0);
                }
            }
        };
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GenericForm();
            }
        });
    }
}

不幸的是,我认为没有办法保留系统 window 装饰,您可能不得不使用默认装饰。由于我不能 100% 确定您是要切换整个框架的不透明度还是只是框架的背景,我在我的示例中包含了这两个函数。 (mKorbels的回答对不想装饰的你更有帮助)

代码:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

public class TransparentExample extends JFrame {

    public TransparentExample() {

        super("TransparentExample");
        Color defaultBackground = getBackground();
        float defaultOpacity = getOpacity();

        JToggleButton button1 = new JToggleButton("Toggle background transparency");
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (button1.isSelected()) {
                    setBackground(new Color(defaultBackground.getRed(), defaultBackground.getGreen(),
                            defaultBackground.getBlue(), 150));
                } else {
                    setBackground(defaultBackground);
                }
            }
        });

        JToggleButton button2 = new JToggleButton("Toggle opacity of whole frame");
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                if (button2.isSelected()) {
                    setOpacity(0.55f);
                } else {
                    setOpacity(defaultOpacity);
                }
                setVisible(true);
            }
        });

        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(button1);
        getContentPane().add(button2);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame.setDefaultLookAndFeelDecorated(true);
                TransparentExample frame = new TransparentExample();
                frame.setVisible(true);
            }
        });
    }

}

未选择切换按钮的框架图片:

选中第一个切换按钮的框架图片:

选中第二个切换按钮的框架图片: