Java / Swing -> Creating a notification JFrame, and the error “The frame is displayable”

后端 未结 2 433
暗喜
暗喜 2020-12-11 23:24

Ok, I am not that versed in Java / Swing, and I am running into a problem. My application is throwing this error: \"Exception in thread \"AWT-EventQueue-0\" java.awt.Illegal

相关标签:
2条回答
  • 2020-12-11 23:34

    The error you are getting is calling setUndecorated(true) after the frame is made displayable by calling pack(). Call setUndecorated before pack and it should work.

    0 讨论(0)
  • 2020-12-11 23:43
    • +1 thanks for this thread, your code solved quite correctly undecorated conatiner, everything is about correct ordering of methods (wooolaaa we are back to Java_1.4.2 edges)

    • but bug still is there, isn't possible to create decorated JFrame with changed Look And Feel

    • based on proper ordering of methods

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    
    public class TranslucentWindow extends JFrame {
    
        private static final long serialVersionUID = 1L;
    
        public TranslucentWindow() {
            super("Test translucent window");
            setLayout(new FlowLayout());
            add(new JButton("test"));
            add(new JCheckBox("test"));
            add(new JRadioButton("test"));
            add(new JProgressBar(0, 100));
            JPanel panel = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(400, 300);
                }
                private static final long serialVersionUID = 1L;
    
                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    g.setColor(Color.red);
                    g.fillRect(0, 0, getWidth(), getHeight());
                }
            };
            panel.add(new JLabel("Very long textxxxxxxxxxxxxxxxxxxxxx "));
            add(panel);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setUndecorated(true);
            pack();
            setLocationRelativeTo(null);
            setOpacity(0.70f);
        }
    
        public static void main(String[] args) {
            try {
                for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            //JFrame.setDefaultLookAndFeelDecorated(true);
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    Window w = new TranslucentWindow();
                    w.setVisible(true);
                }
            });
        }
    } 
    
    0 讨论(0)
提交回复
热议问题