JPanel JavaSwing help me....

sovo007007

Member
11/1/16
59
2
8
34
Em đang làm 1 cái soft nho nhỏ như sau:
Dựng 1 JFrame rồi panel sau đó trên JPanel có các button. Nhưng có vấn đề em mắc phải là lấy ảnh Map làm background cho JPanel.
Em tìm hiểu mãi mà chưa làm được, mong các bậc tiền bối tháo gỡ giúp ạ, em cảm ơn nhiều!
 

Joe

Thành viên VIP
21/1/13
2,732
1,255
113
There are several ways to set an image in JPanel as background image. The best and the easiest way is to use JLabel as the Image bearer for JPanel. Example
PHP:
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javax.imageio.ImageIO;

// Joe Nartca (C)
public class aSWING extends JFrame implements ActionListener {
  public aSWING( ) throws Exception {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel lab = new JLabel(new ImageIcon(ImageIO.read(new File("BabyGirlEatsMelon.jpg"))));
    JPanel jp = new JPanel();
    jp.add(lab); 
    JButton but = new JButton("GO");
    but.addActionListener(this);
    jp.add(but);
    add("Center", jp);
    setSize(500, 360);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    System.out.println("I want more melon.....");
  }
  public static void main(String... args) throws Exception {
    new aSWING( );
  }
}
Here is the display

Jpanel.png
 
  • Like
Reactions: sovo007007

sovo007007

Member
11/1/16
59
2
8
34
There are several ways to set an image in JPanel as background image. The best and the easiest way is to use JLabel as the Image bearer for JPanel. Example
PHP:
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javax.imageio.ImageIO;

// Joe Nartca (C)
public class aSWING extends JFrame implements ActionListener {
  public aSWING( ) throws Exception {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel lab = new JLabel(new ImageIcon(ImageIO.read(new File("BabyGirlEatsMelon.jpg"))));
    JPanel jp = new JPanel();
    jp.add(lab);
    JButton but = new JButton("GO");
    but.addActionListener(this);
    jp.add(but);
    add("Center", jp);
    setSize(500, 360);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    System.out.println("I want more melon.....");
  }
  public static void main(String... args) throws Exception {
    new aSWING( );
  }
}
Here is the display

View attachment 1975
But I want to add a button on image. If when use Jlabel I can't. Help me and thank
 

Joe

Thành viên VIP
21/1/13
2,732
1,255
113
But I want to add a button on image
You should express a bit more clearer so that a foreigner like me could understand. As I said there are lots of ways, the next simplest way is to create an ImagePanel. Example
PHP:
public class BackGroundSWING extends JFrame implements ActionListener {
  public BackGroundSWING( ) throws Exception {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MyJPanel jp = new MyJPanel(new ImageIcon(ImageIO.read(new File("./pic/BabyGirlEatsMelon.jpg"))));
    JButton but = new JButton("GO");
    but.addActionListener(this);
    jp.add(but);
    add("Center", jp);
    pack();
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    System.out.println("I want more melon.....");
  }
  public static void main(String... args) throws Exception {
    BackGroundSWING bg = new BackGroundSWING( );
  }
  private class MyJPanel extends JPanel {
    private Image img;
    public MyJPanel(ImageIcon ic) {
      img = ic.getImage();
      Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
      setPreferredSize(size);
      setMinimumSize(size);
      setMaximumSize(size);
      setSize(size);
    } 
    public void paintComponent(Graphics g) {
      g.drawImage(img, 0, 0, null);
    }
  }
}
baby.png
 
Sửa lần cuối:

sovo007007

Member
11/1/16
59
2
8
34
A @Joe em tìm được 1 cách khá hay:
PHP:
jPanel1 = new javax.swing.JPanel(){
    ImageIcon icon = new ImageIcon("./pic/background.jpg");
    public void paintComponent(Graphics g){
        Dimension d = getSize();
        g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);
        setOpaque(false);
        super.paintComponent(g);
    }
};
và set: jPanel1.setOpaque(false);
Tuy nhiên khi design thì ảnh nó không hiện lên, chỉ khi run mới hiển thị A ạ :(
 
Last edited by a moderator:
  • Like
Reactions: Joe

Joe

Thành viên VIP
21/1/13
2,732
1,255
113
PHP:
jPanel1 = new javax.swing.JPanel(){
   ...
};
It's the inline coding. Useful when the codes are small. It saves codes and runs FASTER than an explicitly-extended class (see MyPanel). However, if the setSize() is too big the background image would become "gritty".
Tuy nhiên khi design thì ảnh nó không hiện lên, chỉ khi run mới hiển thị A ạ :(
It's quite so. Reason: The GUI is only visible when the main thread starts. With SWING it is the method setVisible(true) and with JavaFX it's the Stage.show() method.