Java 自己重写一个jpanel 实现添加背景图片问题
答案:5 悬赏:0
解决时间 2021-01-31 11:59
- 提问者网友:霸气大叔
- 2021-01-30 21:49
我重新写了一个类继承Jpanel 来实现添加背景图片功能,然后在上面添加一个jlabel,每次更新它的显示内容,发现都是重复叠加一起显示的,怎么回事?是继承Jpanel的时候里面重写的方法不对吗?
最佳答案
- 二级知识专家网友:為→妳鎖鈊
- 2021-01-30 23:07
你可以把布局设为null,,手动添加组件的坐标试试.
全部回答
- 1楼网友:萌萌哒小可爱
- 2021-01-31 03:25
可能你画背景图的时机不正确,你得知道原来的 JLabel 是什么时候把 text 喷涂到 Graphics2D 上面的,在它相同的时候去做这件事情。
- 2楼网友:走,耍流氓去
- 2021-01-31 02:35
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyPanel extends JPanel {
JLabel label = new JLabel("hello ,for test");
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(20, 20, 600, 600);
frame.setVisible(true);
frame.add( new MyPanel()) ;
}
public MyPanel() {
this.add(label);
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
ImageIcon icon = new ImageIcon("src/t...import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyPanel extends JPanel {
JLabel label = new JLabel("hello ,for test");
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(20, 20, 600, 600);
frame.setVisible(true);
frame.add( new MyPanel()) ;
}
public MyPanel() {
this.add(label);
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
ImageIcon icon = new ImageIcon("src/t.jpg");
g.drawImage(icon.getImage(), 0, 0,this.getWidth(),this.getHeight(), null);
}
}
- 3楼网友:邪性洒脱
- 2021-01-31 01:11
应该是布局的问题.,你可以把布局设为null,,手动添加组件的坐标试试.
你把代码帖出来看看
- 4楼网友:悲观垃圾
- 2021-01-31 00:42
覆盖那个jpanel的paint,画张图上去
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯