我现在正在尝试将 JMenuItem 的 JFrame 的不透明度设置为 50%。 我最后一次尝试是这样的:
op50.addActionListener((ActionEvent y) -> {
setUndecorated(true);
AWTUtilities.setWindowOpacity(this, 0.5F);
});
但无论我在做什么,我都会遇到 java.awt.IllegalComponentStateException 错误,并显示消息:框架可显示。
我不知道该怎么做,所以请帮助我。
最佳答案
如果没有看到完整的代码示例,就不可能知道哪里出了问题,但这里有一个示例,说明如何从 JMenuItem 执行此操作。 *从评论中可以看出,这在 Java 1.6 之外不起作用。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import com.sun.awt.AWTUtilities;
public class Main
{
public static void main(String[] args)
{
final JFrame frame = new JFrame();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Menu");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("Change Opacity");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
AWTUtilities.setWindowOpacity(frame, 0.5F);
}
});
menu.add(menuItem);
frame.setJMenuBar(menuBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
关于Java - 从 JMenuItem 设置 JFrame 不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48103123/