Java 无法显示图像图标
我必须使用fly-weight模式显示图像,我无法将图像打印到屏幕上,下面的代码演示了这个问题Java 无法显示图像图标,java,swing,Java,Swing,我必须使用fly-weight模式显示图像,我无法将图像打印到屏幕上,下面的代码演示了这个问题 public void draw(Graphics g, int tx, int ty, String name) { grem.paintIcon(null, g, tx, ty); g.drawString(name, tx, ty + H + 15 ); ImageIcon grem = new ImageIcon("../images/g
public void draw(Graphics g, int tx, int ty, String name) {
grem.paintIcon(null, g, tx, ty);
g.drawString(name, tx, ty + H + 15 );
ImageIcon grem = new ImageIcon("../images/grem.png");
}
/// next class that calls the above class
public void paint(Graphics g) {
Folder folderIcon;
String name;
int j = 0; //count number in row
int row = Top; //start in upper left
int x = Left;
//go through all the names and folders
for (int i = 0; i< names.size(); i++) {
name = (String)names.elementAt(i);
if (name.equals(selectedName))
folderIcon = fact.getFolder(true);
else
folderIcon = fact.getFolder(false);
//have that folder draw itself at this spot
folderIcon.paint(g);
x = x + HSpace; //change to next posn
j++;
if (j >= HCount) { //reset for next row
j = 0;
row += VSpace;
x = Left;
}
}
}
public void draw(图形g、int-tx、int-ty、字符串名称){
grem.paintIcon(空、g、tx、ty);
g、 抽绳(名称、tx、ty+H+15);
ImageIcon grem=新的ImageIcon(“../images/grem.png”);
}
///调用上述类的下一个类
公共空间涂料(图g){
文件夹文件夹;
字符串名;
int j=0;//在行中计数
int row=Top;//从左上角开始
int x=左;
//检查所有的名字和文件夹
对于(int i=0;i=HCount){//为下一行重置
j=0;
row+=VSpace;
x=左;
}
}
}
也许空值就是问题所在:如果我没弄错的话,应该是这样的
class MyComponent extends JComponent {
public void paint(Graphics g) {
ImageIcon icon = new ImageIcon("a.png");
int x = 0;
int y = 100;
icon.paintIcon(this, g, x, y);
}
getPreferredSize(…)
方法以返回组件的大小,否则组件的大小将为(0,0),因此可能没有要绘制的内容(取决于所使用的布局管理器)有关更多信息,请阅读上Swing教程中的部分。此外,您也可以使用JList以网格模式显示图标,而不是进行自定义绘制。请查看教程链接的目录,以查找有关如何使用列表的部分,以了解更多信息。公共类Gremlin扩展JFrame imp元素动作监听器{
String names[] = {"Andy","Bill","Bob","Dan","Eugene","Frank","Gary","Harry","Ian","Jack",
"Killlian","Liam","Mark","Nial","Obi","Phil","Richard","Stephan","Terry","Viny",}; // 20 names
public Icon img = new ImageIcon("grem1.jpg");
public JLabel grem = new JLabel(img);
JLabel bigLabel = new JLabel();
JLabel grem2 = new JLabel("New Gremlin");
public JPanel panel2 = new JPanel();
JPanel panel = new JPanel();
public Gremlin() {
JButton button = new JButton("Add Gremlin");
this.add(panel);
panel.setLayout(new GridLayout(9,6));
panel.add(panel2);
panel2.add(button);
for(int i = 0; i<20; i++){
bigLabel.add(grem = new JLabel(names[i]), panel.add(grem = new JLabel(img)));
panel.add(bigLabel);
}
button.addActionListener(this);
setSize(550,600);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
Gremlin frame = new Gremlin();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() != null ){
System.out.println("add a Gremlin");
panel.add(grem = new JLabel("NEW GREMLIN"), panel.add(grem = new JLabel(img)));
revalidate();
}
}
字符串名称[]={“安迪”、“比尔”、“鲍勃”、“丹”、“尤金”、“弗兰克”、“加里”、“哈利”、“伊恩”、“杰克”,
“Killian”、“Liam”、“Mark”、“Nial”、“Obi”、“Phil”、“Richard”、“Stephan”、“Terry”、“Viny”};//20个名字
公共图标img=新图像图标(“grem1.jpg”);
公共JLabel grem=新JLabel(img);
JLabel bigLabel=新的JLabel();
JLabel grem2=新JLabel(“新小精灵”);
public JPanel panel2=新的JPanel();
JPanel面板=新的JPanel();
公共小精灵(){
JButton按钮=新JButton(“添加小精灵”);
本条添加(面板);
面板设置布局(新网格布局(9,6));
小组。添加(小组2);
面板2.添加(按钮);
对于(inti=0;iWelcome!您使用什么库?(并将此库添加到消息的标记中)第3行在draw()中的用途是什么
?它加载同一图像的一个新副本,完全违反了flyweight原则,然后它就什么都不用了。有趣的是,该代码根本没有演示flyweight模式的使用。也许你应该说“这是一段根本没有演示问题的代码”是的,我只是尝试了不同的事情,试图开始工作,无论是您的示例代码还是您描述的问题(图像不会显示在屏幕上)与flyweight模式有任何关系。从获取要显示的图像开始,然后使用flyweight模式高效地执行。这些是单独的任务。这是有意义的,但由于某些原因,仍然不喜欢它,感谢您的输入。没问题,这是一个单独的类,应该显示图像,这是一堆废话,您可以我不知道他是在用JPanel还是whatever@gpasch,这就是为什么我要求提供一个SSCCE
,以防我的水晶球出错。根据提供的信息,我说明了明显的问题。是的,这是一个假设,油漆()方法是JPanel的重写方法。为什么你认为这不是一个逻辑假设?如果你不重写一个绘制方法,你怎么做自定义绘制?加上一个,缺少grem.paintIcon inside draw(paint,paintComponent)的一些原因这是我为我的问题提出的解决方案。