【问题标题】:Display java JPanel in a JFrame在 JFrame 中显示 java JPanel
【发布时间】:2011-07-18 20:40:24
【问题描述】:

我无法正确显示我的 JPanel。我想使用不同的扩展 JPanel 来显示我希望用户使用这个程序做什么(最终是为了显示照片)。下面是目前仅有的两个类的代码。不幸的是,我在使用第一个面板让用户能够选择不同的图形图像时直接使用它时遇到了问题。

发生的情况是,在单击“文件”菜单中的“打开”菜单项之前,我无法显示我的 JPanel。一旦 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());
   }

}

【问题讨论】:

  • 1) 您有问题吗? 2) 为了尽快获得更好的帮助,请发布 SSCCE (pscode.org/sscce.html)。对于涉及图片的SSCCE,可以热链接到网络上的一些图片(例如pscode.org/media/#image)或在代码中生成它们。

标签: java swing jframe jpanel


【解决方案1】:

您尚未在 main 方法中向 mainFrame 的内容窗格中添加任何组件。添加面板的唯一时间是在此 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) 行之前添加类似这样的内容:

mainFrame.add(new NewAlbum());

顺便说一句,约定是 Java 源代码中的所有方法都以小写字母开头。 assignToTransientPanel 将是您方法的更好名称。

【讨论】:

  • Russ,谢谢你的第二眼。当我读到你的回复时,我真的笑出了声。我觉得自己很笨,但这是在新环境中编程时犯的新错误。
  • 安德鲁,如果他的帖子回答了你原来的问题,你会想“接受”拉斯的回答。
猜你喜欢
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 2014-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-02
  • 1970-01-01
相关资源
最近更新 更多