如何在java swing中的jframe中添加图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21325678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:03:27  来源:igfitidea点击:

How to add image in jframe in java swing?

javaimageswingjframe

提问by user3227258

How to add image . i want to add image only above line not on whole jframe how can i do this in jframe swing. i tried but i ma able to set background image on whole screen.i need to set an background only above line.

如何添加图像。我想只在线条上方而不是在整个 jframe 上添加图像,我怎么能在 jframe 摆动中做到这一点。我试过了,但我可以在整个屏幕上设置背景图像。我只需要在行上方设置背景。

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import javax.swing.*;
import java.sql.*;
public class Demo1 extends JFrame
{

  public void paint(Graphics g) {
        super.paint(g);  // fixes the immediate problem.
        Graphics2D g2 = (Graphics2D) g;
        Line2D lin = new Line2D.Float(20, 150, 1350, 150);
        g2.draw(lin);
    }       

  public Demo1(  )
  {
      {
      getContentPane().setBackground(new java.awt.Color(240,255,255));
      }     
      this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      Container c = getContentPane();
      c.setLayout( new FlowLayout() );
      setUndecorated(true);
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      setBounds(0,0,screenSize.width, screenSize.height); 
 }

  public static void main( String[] args )
  {
      Demo1 frame = new Demo1();
      frame.setVisible(true);
  }
}

采纳答案by AJ.

And for adding a image you can use g.drawImage(Image img, int x, int y, ImageObserver observer)in paint()function.

添加一个图像,你可以g.drawImage(Image img, int x, int y, ImageObserver observer)paint()函数中使用。

or

或者

Make a JLabeland add image iconto it

制作一个JLabel并为其添加图像图标

回答by Sagar Chavan

class MyImagePanel extends JPanel{ 
    BufferedImage image;
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        if(image != null){
            g.drawImage(image, 0, 0, this);
        }
    }
}

回答by ravibagul91

//Global Declaration

//全局声明

private BufferedImage bi;

//SomeWhere

//某处

try {
        bi = ImageIO.read(new File("C:/1.jpg"));//Write path of your image here
    } catch (IOException ex) {
        Logger.getLogger(ClassName.class.getName()).log(Level.SEVERE, null, ex);//Change ClassName to your class Name
    }


    final JPanel panel = new JPanel(){
        @Override
        protected void paintComponent(Graphics g){
            Graphics g2 = g.create();
            g2.drawImage(bi, 0, 0, getWidth(), getHeight(), null);
            g2.dispose();
        }

        @Override
        public Dimension getPreferredSize(){
            return new Dimension(bi.getWidth()/2, bi.getHeight()/2);
        }
    };

    mainPanel.add(panel, BorderLayout.CENTER);
    //Add other components to mainPanel
    frm.add(mainPanel);