如何在“提交"时设置半透明的jframe?按钮被点击? [英] how to set semi-transparent jframe when "submit" button is clicked?

查看:80
本文介绍了如何在“提交"时设置半透明的jframe?按钮被点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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),但是会引发异常..我在做什么错了?

I am trying to display transparent JFrame when "submit" button is clicked which displays panel with a JLabel... I have tried using setOpacity(0.55f), but it throws exception.. what am i doing wrong?

推荐答案

不幸的是,我认为无法保留系统窗口的修饰,您可能必须使用默认的修饰.由于我不是100%不确定要切换整个框架的不透明度还是仅切换框架的背景,因此我在示例中包括了这两个函数. (如果您不想装修,mKorbels的回答将为您提供更多帮助)

Unfortunately I think there's no way to keep the system window decoration, you will probably have to go with the default one. Since I'm not 100% sure if you want to toggle the opacity of the whole frame or just the frame's background, I've included both functions in my example. (mKorbels answer help you more if you don't want to have a decoration)

代码:

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);
            }
        });
    }

}

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

Picture of frame with no togglebutton selected:

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

Picture of frame with the first togglebutton selected:

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

Picture of frame with the second togglebutton selected:

这篇关于如何在“提交"时设置半透明的jframe?按钮被点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆