繁体   English   中英

单击“提交”按钮时如何设置半透明的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();
        }

    }
});

我正在尝试显示透明的JFrame当单击“提交”按钮,显示面板与JLabel ...我尝试使用setOpacity(0.55f) ,但它抛出异常..我做错了什么?

不幸的是我认为没有办法保持系统窗口的装饰,你可能不得不使用默认的。 由于我不是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);
            }
        });
    }

}

未选择togglebutton的框架图片: 在此输入图像描述

选择第一个togglebutton的框架图片: 在此输入图像描述

选择第二个togglebutton的框架图片: 在此输入图像描述

@ Programmer007写道 - 例外是“java.awt.IllegalComponentStateException:框架是可显示的”。

  • 请到我看不到的地方, 有关可能的例外情况的更多信息,

  • 如上所述,不知道,一切都是关于你的努力,转变为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();
            }
        });
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM