jpanel 图片的显示

derrick5335 2010-05-08 06:47:54
遇到一个很奇怪的问题,
首先是一个图片

import java.awt.*;
import javax.swing.*;

public class Circle extends JPanel {
private int positionX = 50;
private int positionY = 50;
ImageIcon image = new ImageIcon("Sample.jpg");

public Circle() {
super();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(image.getImage(), positionX, positionY, this);
g2d.dispose();
}
}

然后在我的UI里面有个button, 目的是为了点了之后能在UI的jpanel上面显示我的这个图片

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Circle c = new Circle();
c.setLocation(30, 30);
c.setSize(530, 160);
jPanel1.add(c);
}


奇怪的是, 当我点了那个radioButton的时候, 在我的UI上的的确确出现了Circle的jPanel(我故意将Circle的底色和我UI设置不同, jPanel是的的确确显示出来了), 但是就是没有我的那个sample.jpg的图片显示. 不知道是为什么, 图片的路径应该是没有问题, 因为我后来又用以下的代码测试了一下,


import java.awt.*;
import javax.swing.*;

public class RotateImage extends JFrame {

MyPanel panel = new MyPanel();

public RotateImage() {
setSize(400, 400);
setContentPane(panel);
setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public class MyPanel extends JPanel {

ImageIcon image = new ImageIcon("Sample.jpg");

public MyPanel() {
super();
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(image.getImage(), 0, 0, this);
g2d.dispose();
}
}

public static void main(String[] args) {
new RotateImage();
}
}

显示那个小图片是完全没有问题的.

希望各位能帮我看看
...全文
308 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
derrick5335 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 wd9053 的回复:]

引用 6 楼 derrick5335 的回复:

引用 5 楼 wd9053 的回复:

Java code
public class MyFrame extends JFrame
{
/////////......................

private void jRadioButton1ActionPerformed(java.awt.event.ActionE……
[/Quote]
谢谢你, 问题解决了, 分数给你
dejinzhao 2010-05-11
  • 打赏
  • 举报
回复
图片没出来、改成下面的,就显示出来了、我已经测试了
ImageIcon image = new javax.swing.ImageIcon(getClass().getResource("/Sample.jpg"));
derrick5335 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wd9053 的回复:]

Java code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RotateImage extends JFrame implements ActionListener{

MyPanel jpanel1 = new MyPanel();
JButton ……
[/Quote]
这个是可以显示的, 但是不知道我的为什么就是显示不出来
derrick5335 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wd9053 的回复:]

或者下面这种也行
Java code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RotateImage extends JFrame implements ActionListener{

MyPanel jpanel1 = new MyPanel();
……
[/Quote]
我测试了一下, 这段代码也是不能显示的. 估计和我的问题一样.
derrick5335 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 llf371755616 的回复:]

package com.snail.test;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Circle extends JPanel {
private ……
[/Quote]
我用你的代码测试了一下, 和我有一样的问题, 按了按钮之后, 可以打印出test, 但是是不能显示图片的...
llf371755616 2010-05-10
  • 打赏
  • 举报
回复
package com.snail.test;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Circle extends JPanel {
private int positionX = 50;
private int positionY = 50;
ImageIcon image = new ImageIcon("D:/mm.jpg");

public Circle() {
super();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(image.getImage(), positionX, positionY, this);
g2d.dispose();
}
}
class RotateImage extends JFrame implements ActionListener {

MyPanel imgpanel = new MyPanel();
JPanel panel = new JPanel();

public RotateImage() {
setSize(400, 400);
JButton button = new JButton("Show");
panel.setLayout(null);
button.setBounds(300, 20, 80, 20);
button.addActionListener(this);
panel.add(button);

setContentPane(panel);
setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public class MyPanel extends JPanel {

ImageIcon image = new ImageIcon("D:/mm.jpg");

public MyPanel() {
super();
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(image.getImage(), 0, 0, this);
g2d.dispose();
}
}

public static void main(String[] args) {
new RotateImage();
}

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Test");
Circle c = new Circle();
c.setLocation(30, 30);
c.setSize(530, 160);
panel.add(c);
// pack();
this.repaint();//整个Frame重绘下就可以了
}
}
derrick5335 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wd9053 的回复:]

你修改了窗体中的组件后需要重新通知布局管理器为所有组件重新布局(调用JFrame中的pack()方法),不然新加入的组件无法显示
[/Quote]

那应该怎么改呢?你第一次给我的那个代码里面有些乱码.
bayougeng 2010-05-09
  • 打赏
  • 举报
回复
你可先试试去掉button的控制,直接在构造的时候将图片放上去。
如果还是不行的话,我觉得可能是图片路径的问题。
如果可以,则可能是重绘和布局的问题。
wd9053 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 derrick5335 的回复:]

引用 5 楼 wd9053 的回复:

Java code
public class MyFrame extends JFrame
{
/////////......................

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) { ……
[/Quote]
pack()是调用布局管理器的方法,不知道你那里为啥结果不对
wd9053 2010-05-09
  • 打赏
  • 举报
回复
或者下面这种也行
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RotateImage extends JFrame implements ActionListener{

MyPanel jpanel1 = new MyPanel();
JButton b = new JButton("test");
public RotateImage() {
add(b, BorderLayout.NORTH);
b.addActionListener(this);
//setSize(400, 400);
setPreferredSize(new Dimension(400, 400));
pack();
setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent evt) {
Circle c = new Circle();
c.setLocation(30, 30);
c.setSize(530, 160);
//setVisible(false);
add(jpanel1);
//setVisible(true);
pack();
}

public class MyPanel extends JPanel {

ImageIcon image = new ImageIcon("Sample.jpg");

public MyPanel() {
super();
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(image.getImage(), 0, 0, this);
g2d.dispose();
}
}

public static void main(String[] args) {
new RotateImage();
}
}
wd9053 2010-05-09
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RotateImage extends JFrame implements ActionListener{

MyPanel jpanel1 = new MyPanel();
JButton b = new JButton("test");
public RotateImage() {
add(b, BorderLayout.NORTH);
b.addActionListener(this);
setSize(400, 400);
//setPreferredSize(new Dimension(400, 400));
//pack();
setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent evt) {
Circle c = new Circle();
c.setLocation(30, 30);
c.setSize(530, 160);
setVisible(false);
add(jpanel1);
setVisible(true);
//pack();
}

public class MyPanel extends JPanel {

ImageIcon image = new ImageIcon("Sample.jpg");

public MyPanel() {
super();
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(image.getImage(), 0, 0, this);
g2d.dispose();
}
}

public static void main(String[] args) {
new RotateImage();
}
}
derrick5335 2010-05-09
  • 打赏
  • 举报
回复
我又加了分数, 希望有人能够帮忙看看
derrick5335 2010-05-09
  • 打赏
  • 举报
回复
没有人知道么
wd9053 2010-05-09
  • 打赏
  • 举报
回复
public class MyFrame extends JFrame
{
/////////......................

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Circle c = new Circle();
c.setLocation(30, 30);
c.setSize(530, 160);
jPanel1.add(c);
pack();
}

////////......................

}
derrick5335 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wd9053 的回复:]

Java code
public class MyFrame extends JFrame
{
/////////......................

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) { ……
[/Quote]

试过了, 还是不行, 仍然是能够画出来circle的panel, 但是显示不出来那个图片.
wd9053 2010-05-08
  • 打赏
  • 举报
回复
你修改了窗体中的组件后需要重新通知布局管理器为所有组件重新布局(调用JFrame中的pack()方法),不然新加入的组件无法显示
derrick5335 2010-05-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wd9053 的回复:]

Java code

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Circle c = new Circle();
c.setLocation(30, 30);
c.set……
[/Quote]

不大明白, 能说的清楚一点么
wd9053 2010-05-08
  • 打赏
  • 举报
回复

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Circle c = new Circle();
c.setLocation(30, 30);
c.setSize(530, 160);
jPanel1.add(c);
××Frame.this.pack();
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧