在 Swing 中更新 JLabel 中的图像图标?使用 revalidate() 和 repaint() 但不工作
Updating an Image Icon in a JLabel in Swing? Using revalidate() and repaint() but not working
好的,我刚刚开始习惯 OOP,现在正在学习 swing。我正在制作一个简单的应用程序,它是一个由 4 个图像(一个 X、一个 O、一个正方形和一个三角形)组成的 2x2 网格,单击任何一个都会将颜色的形状切换为蓝色。
虽然我无法让它切换到新图像,但我认为它与我的程序的一些基本内容有关。
介意看看吗?
JFrame class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import javax.swing.JPanel;
public class Frame1 {
public JFrame frame;
Frame1 window = new Frame1();
window.frame.setVisible(true);
}
public Frame1() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 900, 900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Squares x = new Squares("images\black-X.png", "images\blue-X.png", 0, 0, 450, 450, "x");
Squares o = new Squares("images\black-O.png", "images\blue-O.png", 450, 0, 450, 450, "o");
Squares sq = new Squares("images\black-sq.png", "images\blue-sq.png", 0, 425, 450, 450, "sq");
Squares tri = new Squares("images\black-tri.png", "images\blue-tri.png", 450, 410, 450, 450, "tri");
frame.getContentPane().add(x.getLabel());
frame.getContentPane().add(o.getLabel());
frame.getContentPane().add(sq.getLabel());
frame.getContentPane().add(tri.getLabel());
}
}
Mouselistener Class:
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class clickListener implements MouseListener{
Squares ob = new Squares();
public clickListener(Squares newSquare) {
ob = newSquare;
}
public void mouseClicked(MouseEvent e) {
ob.changePic();
}
}
然后是一个对象 class 我正在为每个图像创建一个对象
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Squares {
String pic1, pic2, name;
int x, y, width, height;
JPanel panel = new JPanel();
JLabel label = new JLabel();
public Squares() {
;
}
public Squares(String pic1, String pic2, int x, int y, int width, int height, String name) {
this.pic1 = pic1;
this.pic2 = pic2;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.name = name;
BufferedImage myPic1 = null;
try {
myPic1 = ImageIO.read(new File(pic1));
} catch (IOException ex) {System.out.println("error in image upload");}
/*
panel.setBounds(x, y, width, height);
panel.add(new JLabel(new ImageIcon(myPic1)));
panel.addMouseListener(new clickListener(this));
*/
label = new JLabel(new ImageIcon(myPic1));
label.setBounds(x, y, width, height);
label.addMouseListener(new clickListener(this));
}
public JLabel getLabel() {
return label;
}
public String getName() {
return this.name;
}
public void changePic() {
JOptionPane.showMessageDialog(null, "change pic reached for " + this.name);
BufferedImage myPic2 = null;
try {myPic2 = ImageIO.read(new File(pic2));}
catch(IOException ex) {System.out.println("error in image upload");}
label = new JLabel(new ImageIcon(myPic2));
label.setBounds(x, y, width, height);
label.repaint();
label.revalidate();
}
}
我最初使用的是包含每个 JLabel 的 JPanel,但为了简化事情我去掉了它们。
是的,欢迎提出任何建议。谢谢!
所以 "core" 问题出在你的 changePic
方法中
public void changePic() {
JOptionPane.showMessageDialog(null, "change pic reached for " + this.name);
BufferedImage myPic2 = null;
try {
myPic2 = ImageIO.read(new File(pic2));
} catch (IOException ex) {
System.out.println("error in image upload");
}
label = new JLabel(new ImageIcon(myPic2));
label.setBounds(x, y, width, height);
label.repaint();
label.revalidate();
}
在此方法中,您创建了 JLabel
的新实例,但它从未添加到附加到屏幕的任何容器中,因此永远无法显示。
简单的解决方案,就是简单地更改 JLabel
的现有实例的 icon
属性
public void changePic() {
JOptionPane.showMessageDialog(null, "change pic reached for " + this.name);
BufferedImage myPic2 = null;
try {
myPic2 = ImageIO.read(new File(pic2));
} catch (IOException ex) {
System.out.println("error in image upload");
}
label.setIcon(new ImageIcon(pic2));
}
观察...
在检查代码时,我发现了一些可以做得更好的地方。
- (尽管它的名字)管理主机确实不是
Frame1
的职责。通过不同的容器可以更好地管理核心功能,这会分离组件并使其更加灵活和可重复使用——您也不需要在组件的其他职责中包含框架管理的复杂性。
- 您应该避免
null
布局。一个更简单的解决方案是使用 GridLayout
,因为您提供的实现存在布局问题
Squares
不需要知道父容器想要的位置或大小——这并不是父容器应该直接做出的决定。相反,Squares
应该向父容器提供大小调整提示,以便它可以更好地决定如何布局所有子组件。 JLabel
本身能够提供此信息。
- 可以说
Squares
应该从 JLabel
扩展——这纯粹是为了简单。 JLabel
是显示图像的地方,因此它是一个不错的选择,但是在它周围包裹 class 只会使它的管理更加麻烦,在这种情况下,增加的价值很小。有一个关于组合而不是继承的协议,但在这种情况下,我不确定它会增加更多的价值
ClickListener
不需要创建 Squares
的实例,它只是构造函数需要任何调用者将 Squares
的实例传递给它
- 由于它的核心功能,
Squares
如果无法加载任何一个图像,它应该会失败,这将使诊断这些问题更容易,而不是让程序继续运行 运行 处于 "broken" 状态
恕我直言
例子
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Game {
public static void main(String[] args) {
new Game();
}
public Game() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GamePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
public class GamePane extends JPanel {
public GamePane() throws IOException {
initialize();
}
private void initialize() throws IOException {
Square x = new Square("images\black-X.png", "images\blue-X.png", "x");
Square o = new Square("images\black-O.png", "images\blue-O.png", "o");
Square sq = new Square("images\black-sq.png", "images\blue-sq.png", "sq");
Square tri = new Square("images\black-tri.png", "images\blue-tri.png", "tri");
setLayout(new GridLayout(2, 2));
add(x);
add(o);
add(sq);
add(tri);
}
}
public class ClickListener extends MouseAdapter {
private Square ob;
public ClickListener(Square newSquare) {
ob = newSquare;
}
public void mouseClicked(MouseEvent e) {
ob.changePic();
}
}
public class Square extends JLabel {
String name;
private BufferedImage myPic1, myPic2;
public Square(String pic1, String pic2, String name) throws IOException {
this.name = name;
myPic1 = ImageIO.read(new File(pic1));
myPic2 = ImageIO.read(new File(pic2));
setIcon(new ImageIcon(myPic1));
addMouseListener(new ClickListener(this));
}
public String getName() {
return this.name;
}
public void changePic() {
setIcon(new ImageIcon(myPic2));
}
}
}
好的,我刚刚开始习惯 OOP,现在正在学习 swing。我正在制作一个简单的应用程序,它是一个由 4 个图像(一个 X、一个 O、一个正方形和一个三角形)组成的 2x2 网格,单击任何一个都会将颜色的形状切换为蓝色。
虽然我无法让它切换到新图像,但我认为它与我的程序的一些基本内容有关。
介意看看吗?
JFrame class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import javax.swing.JPanel;
public class Frame1 {
public JFrame frame;
Frame1 window = new Frame1();
window.frame.setVisible(true);
}
public Frame1() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 900, 900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Squares x = new Squares("images\black-X.png", "images\blue-X.png", 0, 0, 450, 450, "x");
Squares o = new Squares("images\black-O.png", "images\blue-O.png", 450, 0, 450, 450, "o");
Squares sq = new Squares("images\black-sq.png", "images\blue-sq.png", 0, 425, 450, 450, "sq");
Squares tri = new Squares("images\black-tri.png", "images\blue-tri.png", 450, 410, 450, 450, "tri");
frame.getContentPane().add(x.getLabel());
frame.getContentPane().add(o.getLabel());
frame.getContentPane().add(sq.getLabel());
frame.getContentPane().add(tri.getLabel());
}
}
Mouselistener Class:
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class clickListener implements MouseListener{
Squares ob = new Squares();
public clickListener(Squares newSquare) {
ob = newSquare;
}
public void mouseClicked(MouseEvent e) {
ob.changePic();
}
}
然后是一个对象 class 我正在为每个图像创建一个对象
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Squares {
String pic1, pic2, name;
int x, y, width, height;
JPanel panel = new JPanel();
JLabel label = new JLabel();
public Squares() {
;
}
public Squares(String pic1, String pic2, int x, int y, int width, int height, String name) {
this.pic1 = pic1;
this.pic2 = pic2;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.name = name;
BufferedImage myPic1 = null;
try {
myPic1 = ImageIO.read(new File(pic1));
} catch (IOException ex) {System.out.println("error in image upload");}
/*
panel.setBounds(x, y, width, height);
panel.add(new JLabel(new ImageIcon(myPic1)));
panel.addMouseListener(new clickListener(this));
*/
label = new JLabel(new ImageIcon(myPic1));
label.setBounds(x, y, width, height);
label.addMouseListener(new clickListener(this));
}
public JLabel getLabel() {
return label;
}
public String getName() {
return this.name;
}
public void changePic() {
JOptionPane.showMessageDialog(null, "change pic reached for " + this.name);
BufferedImage myPic2 = null;
try {myPic2 = ImageIO.read(new File(pic2));}
catch(IOException ex) {System.out.println("error in image upload");}
label = new JLabel(new ImageIcon(myPic2));
label.setBounds(x, y, width, height);
label.repaint();
label.revalidate();
}
}
我最初使用的是包含每个 JLabel 的 JPanel,但为了简化事情我去掉了它们。
是的,欢迎提出任何建议。谢谢!
所以 "core" 问题出在你的 changePic
方法中
public void changePic() {
JOptionPane.showMessageDialog(null, "change pic reached for " + this.name);
BufferedImage myPic2 = null;
try {
myPic2 = ImageIO.read(new File(pic2));
} catch (IOException ex) {
System.out.println("error in image upload");
}
label = new JLabel(new ImageIcon(myPic2));
label.setBounds(x, y, width, height);
label.repaint();
label.revalidate();
}
在此方法中,您创建了 JLabel
的新实例,但它从未添加到附加到屏幕的任何容器中,因此永远无法显示。
简单的解决方案,就是简单地更改 JLabel
icon
属性
public void changePic() {
JOptionPane.showMessageDialog(null, "change pic reached for " + this.name);
BufferedImage myPic2 = null;
try {
myPic2 = ImageIO.read(new File(pic2));
} catch (IOException ex) {
System.out.println("error in image upload");
}
label.setIcon(new ImageIcon(pic2));
}
观察...
在检查代码时,我发现了一些可以做得更好的地方。
- (尽管它的名字)管理主机确实不是
Frame1
的职责。通过不同的容器可以更好地管理核心功能,这会分离组件并使其更加灵活和可重复使用——您也不需要在组件的其他职责中包含框架管理的复杂性。 - 您应该避免
null
布局。一个更简单的解决方案是使用GridLayout
,因为您提供的实现存在布局问题 Squares
不需要知道父容器想要的位置或大小——这并不是父容器应该直接做出的决定。相反,Squares
应该向父容器提供大小调整提示,以便它可以更好地决定如何布局所有子组件。JLabel
本身能够提供此信息。- 可以说
Squares
应该从JLabel
扩展——这纯粹是为了简单。JLabel
是显示图像的地方,因此它是一个不错的选择,但是在它周围包裹 class 只会使它的管理更加麻烦,在这种情况下,增加的价值很小。有一个关于组合而不是继承的协议,但在这种情况下,我不确定它会增加更多的价值 ClickListener
不需要创建Squares
的实例,它只是构造函数需要任何调用者将Squares
的实例传递给它- 由于它的核心功能,
Squares
如果无法加载任何一个图像,它应该会失败,这将使诊断这些问题更容易,而不是让程序继续运行 运行 处于 "broken" 状态
恕我直言
例子
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Game {
public static void main(String[] args) {
new Game();
}
public Game() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GamePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
public class GamePane extends JPanel {
public GamePane() throws IOException {
initialize();
}
private void initialize() throws IOException {
Square x = new Square("images\black-X.png", "images\blue-X.png", "x");
Square o = new Square("images\black-O.png", "images\blue-O.png", "o");
Square sq = new Square("images\black-sq.png", "images\blue-sq.png", "sq");
Square tri = new Square("images\black-tri.png", "images\blue-tri.png", "tri");
setLayout(new GridLayout(2, 2));
add(x);
add(o);
add(sq);
add(tri);
}
}
public class ClickListener extends MouseAdapter {
private Square ob;
public ClickListener(Square newSquare) {
ob = newSquare;
}
public void mouseClicked(MouseEvent e) {
ob.changePic();
}
}
public class Square extends JLabel {
String name;
private BufferedImage myPic1, myPic2;
public Square(String pic1, String pic2, String name) throws IOException {
this.name = name;
myPic1 = ImageIO.read(new File(pic1));
myPic2 = ImageIO.read(new File(pic2));
setIcon(new ImageIcon(myPic1));
addMouseListener(new ClickListener(this));
}
public String getName() {
return this.name;
}
public void changePic() {
setIcon(new ImageIcon(myPic2));
}
}
}