Java 如何更改JLabel的映像
我知道这个问题以前被回答过几次,但是我发现的那些问题都是用程序特定的代码块来回答的,我很难辨别哪些特定的代码实际上改变了图像。我试图在运行时通过按下按钮更改GUI上的jlabel映像Java 如何更改JLabel的映像,java,image,jlabel,Java,Image,Jlabel,我知道这个问题以前被回答过几次,但是我发现的那些问题都是用程序特定的代码块来回答的,我很难辨别哪些特定的代码实际上改变了图像。我试图在运行时通过按下按钮更改GUI上的jlabel映像 public JPanel createContentPane (){ JPanel totalGUI = new JPanel(); totalGUI.setLayout(null); pictureArea = new JPanel(); pictureArea.setLayo
public JPanel createContentPane (){
JPanel totalGUI = new JPanel();
totalGUI.setLayout(null);
pictureArea = new JPanel();
pictureArea.setLayout(null);
pictureArea.setLocation(560, 0);
pictureArea.setSize(860, 500);
totalGUI.add(pictureArea);
picture = new JLabel(image);
picture.setLocation(0, 0);
picture.setSize(800, 800);
picture.setHorizontalAlignment(0);
pictureArea.add(picture);
//skipping other code
decision2 = new JButton("Next");
decision2.setLocation(160, 20);
decision2.setSize(70, 30);
decision2.addActionListener(this);
buttonPanel.add(decision2);
return totalGUI;
}
public void actionPerformed(ActionEvent e) {
//skipped other code
else if(e.getSource() == decision2){
//code i need for changing the image
}
}
感谢您提供的任何帮助。您正在寻找
JLabel的方法
您正在寻找JLabel的方法
你试过这个吗
//更改图像所需的代码
调用定义了JLabel的函数,并传递图像位置,例如:images/image.png
yourfunction(String imagelocation)
{
BufferedImage bufImg=ImageIO.read(new File(image_location));
jlabel.setIcon(new ImageIcon(bufImg));
}
参考资料:您试过这个吗
//更改图像所需的代码
调用定义了JLabel的函数,并传递图像位置,例如:images/image.png
yourfunction(String imagelocation)
{
BufferedImage bufImg=ImageIO.read(new File(image_location));
jlabel.setIcon(new ImageIcon(bufImg));
}
参考资料:请添加书签。如果您搜索,您将找到解释。请添加书签。如果你搜索,你会找到解释。