【问题标题】:how to add Image in JPanel如何在JPanel中添加图像
【发布时间】:2012-07-12 23:14:02
【问题描述】:

我在我的框架中添加了一个 tabbedpane 并调用 tab.add(" ",new Img()) 用 JPanel 扩展 Img ..

问题是:我能否在该 JPanel 中添加 JScrollPane 并将 drawImage 添加为 JPanel 的背景,并在该图像上进行其他绘图,例如在背景图像(例如地图)上制作路线,因为我想在这些路线上应用 Prim 算法...

如果我想在上面的 tabbedpane 上添加额外的面板,我该如何控制这些选项卡操作..

示例代码是这样的……

如果您对 Prim 的算法和图算法有任何想法,请帮助我! 谢谢!

public class MainFrame extends JFrame {
    private JMenuBar menuBar = new JMenuBar();
    private JMenu menuFile = new JMenu();
    private JMenuItem menuFileExit = new JMenuItem();
    private JPanel jPanel1 = new JPanel();
    private JLabel lbl1=new JLabel();
    private JLabel lbl2=new JLabel();
    private JPanel jPanel2 = new JPanel();
    private JTabbedPane jTabbedPane1 = new JTabbedPane();
    private JPanel originalgraph = new JPanel();
    private JPanel zebuthiri = new JPanel();
    private JPanel dekhinathiri = new JPanel();
    private JPanel oattayathiri = new JPanel();
    private JPanel pobbathiri = new JPanel();
    private JPanel zeyathiri = new JPanel();
    int weight[][] = null;
    public MainFrame(int [][]w) {
        this.weight=w;
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setJMenuBar( menuBar );
        this.getContentPane().setLayout(null);
        Toolkit tk=getToolkit();
        Dimension size=tk.getScreenSize();
        this.setSize( new Dimension(size) );
        this.getContentPane().setBackground(Color.CYAN);
        menuFile.setText( "File" );
        menuFileExit.setText( "Exit" );
        menuFileExit.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { fileExit_ActionPerformed( ae ); } } );
        jPanel1.setBounds(new Rectangle(0, 0, 1365, 160));
        jPanel1.setLayout(null);
        lbl1.setBounds(new Rectangle(0, 0, 1365, 160));
        lbl1.setIcon(new ImageIcon("5.jpg"));
        lbl2.setIcon(new ImageIcon("b.jpg"));
        jPanel2.setBounds(new Rectangle(0, 630, 1365, 160));
        lbl2.setBounds(new Rectangle(0, 0, 1365, 160));
        jPanel2.setLayout(null);
        jTabbedPane1.setBounds(new Rectangle(0, 160, 1365, 470));

        menuFile.add( menuFileExit );
        menuBar.add( menuFile );
        jPanel1.add(lbl1);
        jPanel2.add(lbl2);


        jTabbedPane1.addTab("Zebu Thiri",new zebuthiri(weight));
        jTabbedPane1.addTab("Original Graph",new originalgraph(weight));
        jTabbedPane1.addTab("Dekhina Thiri",new dekhinathiri(weight));
        jTabbedPane1.addTab("Oattaya Thiri",new oattayathiri(weight));
        jTabbedPane1.addTab("Pobba Thiri",new pobbathiri(weight));
        jTabbedPane1.addTab("Zeya Thiri",new zeyathiri(weight));
        this.getContentPane().add(jTabbedPane1, null);
        this.getContentPane().add(jPanel2, null);
        this.getContentPane().add(jPanel1, null);
    }

    void fileExit_ActionPerformed(ActionEvent e) {
        System.exit(0);
    }
    public static void main(String args[]){
        int w[][]=new int [100][100];
        MainFrame f=new MainFrame(w);
        f.setVisible(true);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

【问题讨论】:

    标签: java swing user-interface jframe jpanel


    【解决方案1】:

    请阅读How to add an image to a JPanel?

    可以像这样在JPanel上添加图片

    public class ImagePanel extends JPanel{
    
        private BufferedImage image;
    
        public ImagePanel() {
           try {                
              image = ImageIO.read(new File("image name and path"));
           } catch (IOException ex) {
                // handle exception...
           }
        }
    
        @Override
        public void paintComponent(Graphics g) {
            g.drawImage(image, 0, 0, null); 
                        // see javadoc for more info on the parameters
        }
    }
    

    在上面的例子中,图像是在类的构造函数中加载的,然后由执行构造函数后默认调用的paintComponent(...) 绘制

    【讨论】:

    • 1) 一个更简单的解决方案是使用带有 ImageIcon 的 JLabel。它还负责图像的异步加载。 2) paintComponent(...) 在执行构造函数后默认调用 这显然是一个错误的说法。 paintComponent 最终由 RepaintManager 调用(或者如果您自己调用其中一个 paintXXX 方法)在需要时(即,当事件队列上出现重绘事件时)。3)始终打印异常的堆栈跟踪(到日志如果愿意)。不记录堆栈跟踪可能是调试代码的噩梦
    • -1 用于复制 incorrect 实现 ;-) 好吧,不会真的投反对票:由于原始收到的许多赞成票,很难不犯同样的错误...... (简而言之,请参阅我的评论:如果面板报告不透明,paintComponent 必须填充整个区域)
    • 请尊重Encapsulation的原则,同时覆盖super Class的方法。 paintComponent(...) 的访问说明符为 protected 而不是 public :-),虽然我很久以前就对此表示赞同,但我刚刚发现这个错误要纠正了 :-)
    猜你喜欢
    • 2017-09-07
    • 2018-09-02
    • 2010-09-22
    • 2015-06-18
    • 2014-05-28
    • 1970-01-01
    相关资源
    最近更新 更多