如何在一个面板中加载图像和菜单?

时间:2013-08-06 09:14:33

标签: java swing jframe jpanel

目前我正在制作一个应用程序,其中有菜单,但是当我尝试添加图像时,当我使用paintComponent()方法时,它会出错。

以下是我的代码:

public class ImageActivity extends JFrame {

    private JPanel contentPane;
    private BufferedImage image;



    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ImageActivity frame = new ImageActivity();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ImageActivity() {

        try
        {
            image = ImageIO.read(new File("/images/src.jpg"));


        }
        catch(Exception e)
        {

        }

        setTitle("Image Editor");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        JMenuItem mntmLoadImage = new JMenuItem("Load Image");
        mnFile.add(mntmLoadImage);

        JMenuItem mntmSaveImage = new JMenuItem("Save Image");
        mnFile.add(mntmSaveImage);

        JSeparator separator = new JSeparator();
        mnFile.add(separator);

        JMenuItem mntmExit = new JMenuItem("Exit");
        mnFile.add(mntmExit);

        JMenu mnEdit = new JMenu("Edit");
        menuBar.add(mnEdit);

        JMenuItem mntmIncreaseBright = new JMenuItem("Increase Bright");
        mnEdit.add(mntmIncreaseBright);

        JMenuItem mntmDecreaseBright = new JMenuItem("Decrease Bright");
        mnEdit.add(mntmDecreaseBright);

        JSeparator separator_1 = new JSeparator();
        mnEdit.add(separator_1);

        JMenuItem mntmRestoreImage = new JMenuItem("Restore Image");
        mnEdit.add(mntmRestoreImage);

        JMenu mnHelp = new JMenu("Help");
        menuBar.add(mnHelp);

        JMenuItem mntmHelpCtrl = new JMenuItem("Help ctrl + K");
        mnHelp.add(mntmHelpCtrl);

        JMenuItem mntmAboutImageEditor = new JMenuItem("About Image Editor");
        mnHelp.add(mntmAboutImageEditor);

        JSeparator separator_2 = new JSeparator();
        mnHelp.add(separator_2);

        JMenuItem mntmAboutCompany = new JMenuItem("About Company");
        mnHelp.add(mntmAboutCompany);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        protected  void paintComponent(Graphics g) 
        {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);             
        } 

    }

}

任何人都可以指导我,我错了吗?

  

我是初学者,所以请帮助我。

1 个答案:

答案 0 :(得分:1)

  

我错了吗?

  1. 覆盖JFrame
  2. 尝试在方法中创建方法
  3. 假设JFramepaintComponent方法。
  4. 除非您有特殊需要,否则请使用JLabel代替渲染图片

    <强>更新

    你可以改为尝试更像......

    contentPane = new JPanel() {
        protected void paintComponent(Graphics g) 
        {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, this);             
        } 
    }
    

    <强>更新

    您忽略了ImageIO可能抛出的异常,它将告诉您图像是否无法加载

     try
     {
          image = ImageIO.read(new File("/images/src.jpg"));
     }
     catch(Exception e)
     {
          e.printStackTrace();
     }