0

目前我正在制作一个应用程序,其中有菜单,但是当我尝试添加图像时,当我使用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);             
        } 

    }

}

任何人都可以指导我哪里错了??

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

4

1 回答 1

1

我哪里错了??

  1. 覆盖JFrame
  2. 尝试在方法中创建方法
  3. 假设JFrame有一个paintComponent方法。

除非您有真正的特殊需要,否则请改用 aJLabel来渲染图像

更新

您可以改为尝试类似...

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();
 }
于 2013-08-06T09:18:51.410 回答