Posicionamento do JFrame no LookAndFeel

0 respostas
S

Pessoal é o seguinte, o seguinte programa java reproduz um comportamento errado na plataforma ubuntu 11.04, testei no win7 e não reproduziu erro
algum.

Gostaria que se pudessem testar em outras plataformas e sistemas operacionais, principalmente o Mac, quem tiver...

O programa mostra um jframe, e quando o usuário clica e segura pressionado a tecla 'ctrl' aparece um novo jframe. Ao liberar a tecla
'ctrl' o segundo jframe é fechado. Ao repetir o procedimento, o segundo jframe muda de posição, isso no ubuntu 11.04...

Para não reproduzir o comportamento no segundo jframe é necessário comentar a linha initLAF(); no método createAndShowGUI()...
isso no ubuntu 11.04. No windows 7 o segundo jframe sempre fica na mesma posição (comportamento desejado)

Se alguém souber o porquê!, ou como corrigir...

/**
 *
 * @author suspiro10
 */
public class LAF_Position {

    private static Thread thread2;
    private static boolean waitThread1 = false;
    private static boolean threadDone = false;
    private static NewJFrame newJFrame;

    private static void addComponentsToPane(Container pane) {

        pane.add(new JLabel("tecle e libere tecla 'ctrl' "));

        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {

            public boolean dispatchKeyEvent(KeyEvent e) {
                if (e.getID() == KeyEvent.KEY_PRESSED) {
                    /*
                     * ctrl pressed
                     */
                    if (e.getKeyCode() == 17) {
                        if (newJFrame == null) {
                            newJFrame = new NewJFrame();
                        }

                        threadDone = true;
                        thread2 = new BasicThreadJFrame();
                        thread2.start();
                    }
                }
                return false;
            }
        });

    }

    private static void initLAF() {
        String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
        try {
            UIManager.setLookAndFeel(lookAndFeel);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LAF_Position.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(LAF_Position.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(LAF_Position.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(LAF_Position.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void createAndShowGUI() {
     JFrame.setDefaultLookAndFeelDecorated(true); 
      initLAF(); //comente  esta linha para não reproduzir o erro 
    
       
       JFrame frame = new JFrame("LAF"); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addComponentsToPane(frame.getContentPane());
        frame.setSize(new Dimension(200, 200));
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static class BasicThreadJFrame extends Thread {

        public void run() {
            newJFrame.setVisible(true);
        }
    }

    static class NewJFrame extends JFrame {

        public NewJFrame() {
            add(new JLabel("2 jframe"));
            setSize(100, 100);

            KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {

                public boolean dispatchKeyEvent(KeyEvent e) {
                    if (e.getID() == KeyEvent.KEY_RELEASED) {
                        /*
                         * ctrl pressed
                         */
                        if (e.getKeyCode() == 17) {
                            setVisible(false);
                        }
                    }
                    return false;
                }
            });
        }
    }
}
Criado 2 de agosto de 2011
Respostas 0
Participantes 1