Vẽ hình nền trên JFrame
Source code để vẽ 1 hình trên JFrame
02 03 import java.awt.*; 04 05 import javax.swing.ImageIcon; 06 import javax.swing.JButton; 07 import javax.swing.JFrame; 08 import javax.swing.JPanel; 09 import javax.swing.JScrollPane; 10 11 public class PaintFrame extends JFrame 12 { 13 private static final long serialVersionUID = 1L; 14 JScrollPane scrollPane; 15 ImageIcon icon; 16 17 public PaintFrame() 18 { 19 icon = new ImageIcon(“images/MU.png”); 20 21 JPanel panel = new JPanel() 22 { 23 public void paintComponent(Graphics g) 24 { 25 // Trường hợp 1: Hiển thị hình ảnh với kích cỡ đầy đủ 26 //g.drawImage(icon.getImage(), 0, 0, null); 27 // Trường hợp 2: Co giãn kích thước hình ảnh lớn bằng kích thước JFrame 28 Dimension d = getSize(); 29 g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null); 30 // Trường hợp 3: Cố định vị trí hình ảnh trong scroll pane 31 // Point p = scrollPane.getViewport().getViewPosition(); 32 // g.drawImage(icon.getImage(), p.x, p.y, null); 33 setOpaque( false ); 34 super.paintComponent(g); 35 } 36 }; 37 ////vẽ 1 button lên JFrame để thử 38 JButton button = new JButton( “Hello world” ); 39 panel.add( button ); 40 41 scrollPane = new JScrollPane( panel ); 42 setContentPane( scrollPane ); 43 } 44 45 public static void main(String [] args) 46 { 47 PaintFrame frame = new PaintFrame(); 48 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 49 frame.setSize(300, 300); 50 frame.setVisible(true); 51 } 52 }
|
Kết quả như hình sau

Chúc thành công!
dao said
sao em làm theo thầy nó báo lỗi này ạ :
1) cannot find symbol method velenhinh(java.awt.Graphics)
Code của em là :
public class hinhnen extends JFrame{
private static final long serialVersionUID = 1L;
private JScrollPane scrollPane;
private ImageIcon icon;
private JPanel panel;
private JButton button;
public hinhnen() {
icon = new ImageIcon(“xau.jpg”);
panel = new JPanel()
{
public void velenhinh(Graphics gra)
{
gra.drawImage(icon.getImage(), 0, 0, null);
Dimension d = getSize();
gra.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);
//Point p = scrollPane.getViewport().getViewPosition();
//gra.drawImage(icon.getImage(), p.x, p.y, null);
setOpaque( false );
super.velenhinh(gra);
}
};
button=new JButton(“de len hinh ne”);
panel.add( button );
scrollPane = new JScrollPane( panel );
this.setContentPane(scrollPane);
}
public static void main(String arg[])
{
JFrame frame = new hinhnen();
frame.setVisible(true);
}
}
Xin thầy xem dùm em.Em xin cám ơn
yinyang said
bạn extends lớp từ JFrame. JFrame là super class của lớp hinhnen này.
Nếu bạn gọi super.velenhinh(gra) thì sẽ báo lỗi bởi JFrame ko có method velenhinh. Bạn hãy bỏ từ khóa super đi hoặc thay bằng this.
Có lẽ bạn định viết đệ quy ở đây hả??
Thuy said
thầy chỉ giúp em cách thay đổi màu nền của frame khi nhấn button bằng ngôn ngữ C#.
vovanhai said
private void Form1_Load(object sender, System.EventArgs e) {
this.BackColor = Color.FromArgb(255, 0, 0, 255);
}
nguyen dinh thang said
chao thay a,em hoc rat do mon ja va khong biet vi no qua kho hay do em nhac hoc nua,
nam nay em hoc nam hai roi,dang hoc mon java nang cao,thay no con kho hon nen em quyet dinh hoc lai mon java1 lieu co kip khong thay.
thay co the cho em cac vi du ve GUI dc khong a,cang nhieu vi du cang tot nha thay.
em chan thanh cam on thay
Tien Dung said
Em chào thầy !
Em dùng Netbean 6.8. Em đã code như thầy hướng dẫn thế nhưng Frame vẫn không có nền. Thầy xem và giải thích giúp em
Em chân thành cảm ơn thầy
package GiaoDien;
import java.awt.Dimension;
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.*;
import javax.swing.JScrollPane;
/**
*
* @author Administrator
*/
public class PaintFrame1 extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
JScrollPane scrollPane;
ImageIcon img;
/** Creates new form PaintFrame */
public PaintFrame1() {
// initComponents();
img = new ImageIcon(“dk.png”);
JPanel panel = new JPanel(){
public void paintComponent(Graphics g){
Dimension d = getSize();
g.drawImage(img.getImage(),0,0, d.width,d.height,null);
setOpaque(false);
super.paintComponent(g);
}
};
JButton button = new JButton( “Hello world”);
panel.add( button );
scrollPane = new JScrollPane(panel);
setContentPane(scrollPane);
}
public static void main(String[] args) {
PaintFrame1 pf = new PaintFrame1();
pf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pf.setSize(300, 300);
pf.setVisible(true);
}
}
Võ Văn Hải said
Xem lại đường dẫn file hình là OK.
longdoan89 said
@Tiến Dũng: bạn xem lại chỗ dấu “”. Bạn copy nguyên theo code của Thầy thì sẽ bị sai dấu “”. Bạn xóa đi và gõ lại bình thường là sẽ được! Tương tự cho chữ “Hello world”
@Thầy: Cảm ơn thầy nhiều về code Java! em thấy thích thú với môn này!
Duy said
Dưởng như chỉ toàn copy, không ai thèm tự đánh nên mới ra lỗi nhiều vậy thôi. Bài trên rất hữu dụng cho việc thiết kế, lại 1 điều hay mà mình được biết, cám ơn bạn nhiều
trung said
Thưa thầy, cho em hỏi một vấn đề :
dùng image làm background trong swing, nhưng khi cho di chuyển 1 đối tượng trên backgruond (vd: trái banh lăn) : mỗi lần đối tượng di chuyển, bóng của nó không được xóa đi mà vẫn còn ở vị trí cũ.
Vd: trái banh chạy trên màn hình, khi trái banh di chuyển hết mọi bề mặt của màn hình, cuối cùng nền chỉ toàn là hình của trái banh.
phải làm cách nào để không để lại dấu vết sau mỗi lần di chuyển?
Võ Văn Hải said
em dùng component.repaint() để vẽ lại.
Lê Khương Mạnh Tuấn Hùng said
Em nghĩ thầy nên sharing coding để tụi em học hỏi hic hic
hinh nen dien thoai said
Thầy cho em hỏi là dùng Swing rồi thì có nên dùng luôn AWT không thầy? (vì thấy thầy import AWT)
Võ Văn Hải said
SWING và AWT không loại trừ nhau. Trên Swing bạn vẫn phải dùng các object có trong AWT rất nhiều.
Chinh Duc said
Em cảm ơn thầy vì bài viết rất bổ ích
Chinh Duc said
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.*;
public class Server extends JFrame
{
private static final long serialVersionUID = 1L;
JLabel lbserver;
JLabel lbcongport;
JLabel lbclientconnect; //client connected
JLabel lbdatarececlient; //data receipt from client
JLabel lbdatasendclient; //data send to client
JButton btnexit;
JTextArea txtclientconnect;
JTextArea txtdatarececlient;
JTextArea txtdatasendclient;
JTextField txfport;
ImageIcon icon;
public Server()
{
setTitle(“Chat Server”);
setSize(1000,700);
icon = new ImageIcon(“images/nen.jpg”);
JPanel paintpanel = new JPanel()
{
public void paintComponent(Graphics g)
{
Dimension d = getSize();
g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);
setOpaque(false);
super.paintComponent(g);
}
};
JPanel title = new JPanel();
lbserver = new JLabel(“Server”);
lbserver.setFont(new Font(“Arials “,Font.BOLD,40));
title.add(lbserver);
this.add(title, BorderLayout.NORTH);
//Lam 1 panel de nhap server port
JPanel port = new JPanel();
txfport = new JTextField(“”, 5);
txfport.enable(false);
lbcongport = new JLabel(“Server Port: “);
lbcongport.setFont(new Font(“Arials “,Font.BOLD,13));
port.add(lbcongport);
port.add(txfport);
this.add(port, BorderLayout.WEST);
//Lam 1 panel de chua button exit
JPanel exit = new JPanel();
btnexit = new JButton(“Exit Server”);
exit.add(btnexit);
btnexit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
// TODO Auto-generated method stub
System.exit(0);
}
});
this.add(exit, BorderLayout.EAST);
//Lam 1 panel de chua cac textarea the hien luong data
JPanel data = new JPanel();
data.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//Client Connected
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
lbclientconnect = new JLabel(“Client connected”);
lbclientconnect.setFont(new Font(“Arials “,Font.BOLD,14));
data.add(lbclientconnect, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.1;
txtclientconnect = new JTextArea(27, 18);
txtclientconnect.enable(false);
data.add(txtclientconnect, gbc);
//Data send to client
gbc.gridx = 3;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
lbdatasendclient = new JLabel(“Data send to client”);
lbdatasendclient.setFont(new Font(“Arials “,Font.BOLD,14));
data.add(lbdatasendclient, gbc);
gbc.gridx = 3;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.1;
txtdatasendclient = new JTextArea(27, 22);
txtdatasendclient.enable(false);
data.add(txtdatasendclient, gbc);
//Data receipt from client
gbc.gridx = 2;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
lbdatarececlient = new JLabel(“Data receipt from client”);
lbdatarececlient.setFont(new Font(“Arials “,Font.BOLD,14));
data.add(lbdatarececlient, gbc);
gbc.gridx = 2;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.1;
txtdatarececlient = new JTextArea(27, 22);
txtdatarececlient.enable(false);
data.add(txtdatarececlient, gbc);
this.add(data, BorderLayout.CENTER);
}
public static void main(String []args)
{
Server chatserver = new Server();
chatserver.setDefaultCloseOperation(chatserver.EXIT_ON_CLOSE);
chatserver.setVisible(true);
}
}
Thưa thầy, thầy cho em hỏi sao bài của em lại ko hiển thị dc hình nền ạ?
Võ Văn Hải said
Bị đè lên rồi còn đâu.
Em nên dùng this.getToolkit().getImage(“imgs/xxx.jpg”) thay vì dùng ImageIcon.