Java “当”时如何设置半透明jframe;提交;按钮被点击了吗?

Java “当”时如何设置半透明jframe;提交;按钮被点击了吗?,java,swing,jframe,transparency,Java,Swing,Jframe,Transparency,单击“提交”按钮时,我试图显示透明的JFrame,该按钮显示带有JLabel标签的面板。。。 我曾尝试使用setOpacity(0.55f),但它引发异常。。我做错了什么 @程序员007编写-异常为“ java.awt.IllegalComponentStateException:框架可显示。“ 请在我看不到的地方查看更多信息 如前所述,不知道,一切都与您的努力有关,转换为SSCCE/MCVE,简短、可运行、可编译 导入java.awt.Color; 导入java.awt.event.Ac

单击“提交”按钮时,我试图显示透明的
JFrame
,该按钮显示带有
JLabel
标签的面板。。。 我曾尝试使用
setOpacity(0.55f)
,但它引发异常。。我做错了什么

@程序员007编写-异常为“ java.awt.IllegalComponentStateException:框架可显示。“

  • 请在我看不到的地方查看更多信息

  • 如前所述,不知道,一切都与您的努力有关,转换为SSCCE/MCVE,简短、可运行、可编译

导入java.awt.Color;
导入java.awt.event.ActionEvent;
导入javax.swing.AbstractAction;
导入javax.swing.Action;
导入javax.swing.JDialog;
导入javax.swing.SwingUtilities;
导入javax.swing.Timer;
公共类GenericForm扩展了JDialog{
私有静态最终长serialVersionUID=1L;
私人定时器;
私有JDialog dialog=newjdialog();
私有整数计数=0;
公共一般形式(){
对话框。设置大小(400300);
对话框。设置未装饰(true);
对话框。设置不透明度(0.5f);
setName(“切换不透明度”);
dialog.getContentPane().setBackground(颜色为.RED);
对话框。设置位置(150150);
对话框.setVisible(true);
timer=newjavax.swing.timer(1500,updateCol());
timer.setRepeats(真);
timer.start();
}
私有操作updateCol(){
返回新的AbstractAction(“Hello World”){
私有静态最终长serialVersionUID=1L;
@凌驾
已执行的公共无效操作(操作事件e){
布尔bol=dialog.getOpacity()<0.55f;
计数+=1;
如果(计数<10){
如果(bol){
对话框。设置不透明度(1.0f);
dialog.getContentPane().setBackground(Color.WHITE);
}否则{
对话框。设置不透明度(0.5f);
dialog.getContentPane().setBackground(颜色为.RED);
}
}否则{
系统出口(0);
}
}
};
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
新的GenericForm();
}
});
}
}

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

代码:

未选择切换按钮的帧图片:

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

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

1)要更快地获得更好的帮助,请发布一个或。2) 始终复制/粘贴错误和异常输出!为了实现这一点,必须取消装饰框架(您确实执行了
frame.setUndercorated(false);
)。另外,如果在actionlistener中取消修饰现有框架,则必须在其前面调用
frame.dispose()
(在其后面调用
frame.setVisible(true)
)@LuxxMiner-thanx以获得建议……在Java6、7或更高版本中,我看不到任何异常8@LuxxMiner我做到了以上,没有例外,而且是透明的,但是,“帧关闭”按钮已消失。。面板(装载面板)不是不透明的谢谢你。。。我一直在寻找第二个切换按钮(设置不透明度)…我的另一个问题是,你如何使它的面板不透明,背景是半透明的-transparent@Programmer007如果你不介意装饰不是半透明的,你可以用第一个按钮替换
setBackground(新颜色(0,0,0,0))带有
setBackground(新颜色(defaultBackground.getRed(),defaultBackground.getGreen(),defaultBackground.getBlue(),150))。这将使背景半透明,但每个组件(按钮)都完全可见(在您的情况下,包括一个添加的面板。只要您没有在其上调用
setOpaque(false)
,它就会工作)。半透明工作。。。将在不透明面板上工作now@Programmer007这就是你想要的最终结果吗?我不明白你说的“现在可以在不透明面板上工作”是什么意思。。。这是个问题吗?如果是,我猜您在代码中指的是
loadPanel
。好。。。是的,您可以将背景设置为新颜色(0,0,0,0)
,因此,背景颜色将保持不变。(如果你问的是这个)我问的是,只有当点击“提交”按钮时,框架才会变成半透明,并显示面板(实际上是不透明的loadPanel)
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();
        }

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

}