Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Finestra non viene visualizzata

    ciao!

    mi sono reso conto che a forza di creare gui con gli editor visuali, mi sono dimenticato parecchie cose.
    così, dovendo riscrivere un programmino, ho pensato di creare la gui a mano.
    solo che quando lancio il programma, la finestra praticamente non compare a video.
    come se non venisse diesgnata:
    codice:
    public class MainWindow extends JFrame {
    
        private JFrame mainFrame;
        private JTextPane textPane;
        private JScrollPane scrollPane;
        private JMenuBar menuBar;
        private JMenu menuFile;
        private JMenuItem itemChoose;
        private JMenuItem itemSave;
        private JMenuItem itemExit;
    
        public MainWindow() {
            createGui();
        }
    
        private void createGui() {
            mainFrame = new JFrame("Lista directory");
            textPane = new JTextPane();
            scrollPane = new JScrollPane();
            menuBar = new JMenuBar();
            menuFile = new JMenu();
            itemChoose = new JMenuItem();
            itemSave = new JMenuItem();
            itemExit = new JMenuItem();
            mainFrame.setSize(new Dimension(700, 700));
            mainFrame.setLayout(new BorderLayout());
            mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            mainFrame.setResizable(false);
            mainFrame.setLocationRelativeTo(null);
            textPane.setEditable(false);
            scrollPane.setViewportView(textPane);
            menuFile.setText("File");
            menuFile.add(itemChoose);
            menuFile.add(itemSave);
            menuFile.add(itemExit);
            menuBar.add(menuFile);
            mainFrame.setJMenuBar(menuBar);
            mainFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);
            mainFrame.pack();
        }
    
        public static void main(String args[]) {
            if (System.getProperty("os.name").toLowerCase().contains("mac")) {
                System.setProperty("apple.laf.useScreenMenuBar", "true");
            } else {
                try {
                    for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                }
            }
    
            EventQueue.invokeLater(() -> {
                new MainWindow().setVisible(true);
            });
        }
    }
    dove sto sbagliando??

  2. #2
    Perché nella classe estendi JFrame
    codice:
    public class MainWindow extends JFrame

    e poi lo dichiari nuovamente nel metodo?
    codice:
    private void createGui() {
            mainFrame = new JFrame("Lista directory");
    non devi dichiararla se la estendi nella classe.

    Puoi fare semplicemente:
    codice:
    private void createGui() {
            super("Lista Directory");
            ....
            setSize(new Dimension(700, 700));
            setLayout(new BorderLayout());
            ....
            ....
    }
    Ciao.
    I computer sono incredibilmente veloci, accurati e stupidi.
    Gli uomini sono incredibilmente lenti, inaccurati e intelligenti.
    Insieme sono una potenza che supera l'immaginazione.

    A.Einstein

  3. #3
    ciao!

    si mi ero risposto da solo nel frattempo.
    scemenza mia:
    codice:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.WindowConstants;
    
    public class MainWindow extends JFrame {
    
        private JTextPane textPane;
        private JScrollPane scrollPane;
        private JMenuBar menuBar;
        private JMenu menuFile;
        private JMenuItem itemChoose;
        private JMenuItem itemSave;
        private JMenuItem itemExit;
    
        public MainWindow() {
            super("Lista directory");
            createGui();
        }
    
        private void createGui() {
            textPane = new JTextPane();
            scrollPane = new JScrollPane();
            menuBar = new JMenuBar();
            menuFile = new JMenu();
            itemChoose = new JMenuItem();
            itemSave = new JMenuItem();
            itemExit = new JMenuItem();
            setPreferredSize(new Dimension(700, 700));
            setLayout(new BorderLayout());
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setResizable(false);
            textPane.setEditable(false);
            scrollPane.setViewportView(textPane);
            getContentPane().add(scrollPane, BorderLayout.CENTER);
            menuFile.setText("File");
            menuFile.add(itemChoose);
            menuFile.add(itemSave);
            menuFile.add(itemExit);
            menuBar.add(menuFile);
            setJMenuBar(menuBar);
            pack();
            setLocationRelativeTo(null);
        }
    
        public static void main(String args[]) {
            if (System.getProperty("os.name").toLowerCase().contains("mac")) {
                System.setProperty("apple.laf.useScreenMenuBar", "true");
            } else {
                try {
                    for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                }
            }
    
            SwingUtilities.invokeLater(() -> {
                new MainWindow().setVisible(true);
            });
        }
    }
    grazie!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.