简体   繁体   English

在JFrame中显示Java JPanel

[英]Display java JPanel in a JFrame

I'm having problems getting my JPanel to display properly. 我在使JPanel正确显示时遇到问题。 I want to use different extended JPanels to display what I want the user to do with this program (which is ultimately to display photographs). 我想使用不同的扩展JPanels来显示我希望用户使用该程序执行的操作(最终将显示照片)。 Below is the code for the only two classes that exist at this point. 下面是此时仅存在的两个类的代码。 Unfortunately, I'm having problems just getting this to work right out of the gate with the first panel which was to present the user with the ability to select different graphic images. 不幸的是,我在使用第一个面板向用户展示选择不同图形图像的能力时,就无法完全解决这个问题。

What's happening is, I can't get my JPanel to display until I click the "Open" menu item in the File menu. 发生的事情是,直到单击“文件”菜单中的“打开”菜单项,我才能显示我的JPanel。 Once that JOptionPane shows, so does my JPanel (NewAlbum). 一旦该JOptionPane显示,我的JPanel(NewAlbum)也将显示。

class PhotoGallery {
    static JPanel transientPanel = null;
    static final JFrame mainFrame = new JFrame("Photo Gallery");

    public static void main(String[] args) {
        JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);

        JMenuItem open = new JMenuItem("Open");
        open.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(mainFrame, "Hello World");
            }
        });
        fileMenu.add(open);

        JMenuItem newAlbum = new JMenuItem("New Album");
        open.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AssignToTransientPanel((JPanel) new NewAlbum());
                Container content = mainFrame.getContentPane();
                content.removeAll();
                content.add(transientPanel);
                content.validate();
                content.repaint();
            }
       });
       fileMenu.add(newAlbum);

       JMenuItem exit = new JMenuItem("Exit");
       exit.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               System.exit(0);
           }
       });
       fileMenu.add(exit);

       JMenuBar pgMenu = new JMenuBar();
       pgMenu.add(fileMenu);
       mainFrame.setJMenuBar(pgMenu);
       mainFrame.setSize(640, 480);
       mainFrame.setLocation(20, 45);

       mainFrame.validate();
       mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       mainFrame.setVisible(true);
   }

    public static void AssignToTransientPanel(JPanel jp) {
        if(transientPanel != null)
            mainFrame.remove(transientPanel);
            transientPanel = jp;
        }
    }
}

class NewAlbum extends JPanel {
    JButton selectImages = new JButton("Select Images");
    JFileChooser jfc;
    File[] selectedFiles;

    public NewAlbum() {
        selectImages.setLocation(25, 25);
        add(selectImages);

        selectImages.addActionListener(new ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent ae) {
             jfc = new JFileChooser();
             jfc.setMultiSelectionEnabled(true);
             jfc.showOpenDialog(getParent());
             selectedFiles = jfc.getSelectedFiles();
         }
      });

      this.validate();
   }

   public int getHeight() {
       return getParent().getSize().height - 20;
   }

   public int getWidth() {
       return getParent().getSize().width - 20;
   }

   public Dimension getPreferredSize() {
       return new Dimension(this.getWidth(), this.getHeight());
   }

} }

You have not added any components to the mainFrame's content pane in the main method. 您尚未在main方法的mainFrame的内容窗格中添加任何组件。 The only time a panel gets added is in this ActionListener: 唯一添加面板的时间是在此ActionListener中:

    open.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            AssignToTransientPanel((JPanel) new NewAlbum());
            Container content = mainFrame.getContentPane();
            content.removeAll();
            content.add(transientPanel);
            content.validate();
            content.repaint();
        }
   });

This is only getting called when "Open" is clicked as you have, I assume accidentally, added the ActionListener to the open JMenuItem rather than the newAlbum JMenuItem. 仅当您单击“打开”时才调用此函数,我想这是偶然地将ActionListener添加到打开的JMenuItem而不是newAlbum JMenuItem。 To add content on startup you need to add something like this before the mainFrame.setVisible(true) line: 要在启动时添加内容,您需要在mainFrame.setVisible(true)行之前添加以下内容:

mainFrame.add(new NewAlbum());

BTW, the convention is for all methods in Java source code to start with a lower case letter. 顺便说一句,约定是Java源代码中的所有方法均以小写字母开头。 assignToTransientPanel would be a better name for your method. 对于您的方法,assignToTransientPanel将是一个更好的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM