在JFrame中显示Java JPanel [英] Display java JPanel in a JFrame

查看:90
本文介绍了在JFrame中显示Java JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法正确显示我的JPanel.我想使用不同的扩展JPanels来显示我希望用户使用该程序执行的操作(最终将显示照片).下面是此时仅存在的两个类的代码.不幸的是,我在使用第一个面板向用户展示选择不同图形图像的能力时,无法完全解决这个问题.

I'm having problems getting my JPanel to display properly. 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). 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.

正在发生的事情是,直到单击文件"菜单中的打开"菜单项,我才能显示我的JPanel.一旦该JOptionPane显示,我的JPanel(NewAlbum)也将显示.

What's happening is, I can't get my JPanel to display until I click the "Open" menu item in the File menu. Once that JOptionPane shows, so does my 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());
   }

}

推荐答案

您尚未在main方法的mainFrame的内容窗格中添加任何组件.添加面板的唯一时间是在此ActionListener中:

You have not added any components to the mainFrame's content pane in the main method. The only time a panel gets added is in this 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();
        }
   });

仅当您单击打开"时才调用此函数,我想是偶然地将ActionListener添加到了打开的JMenuItem中,而不是newAlbum JMenuItem中.要在启动时添加内容,您需要在mainFrame.setVisible(true)行之前添加以下内容:

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. To add content on startup you need to add something like this before the mainFrame.setVisible(true) line:

mainFrame.add(new NewAlbum());

顺便说一句,约定是Java源代码中的所有方法均以小写字母开头.对于您的方法,assignToTransientPanel会是一个更好的名称.

BTW, the convention is for all methods in Java source code to start with a lower case letter. assignToTransientPanel would be a better name for your method.

这篇关于在JFrame中显示Java JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆