Java 通过图像滑动的用户界面
我正在尝试更改用户界面的Java 通过图像滑动的用户界面,java,image,swing,mouseevent,Java,Image,Swing,Mouseevent,我正在尝试更改用户界面的JLabels中显示的图像 下面的课程是对这个概念的一个非常简单的测试。用户界面获取一个装满图像的文件夹(字段imageFolderPath),并在唯一的JLabel中显示第一个调整大小的图像;单击图像会提示UI在文件夹中显示以下图像 至少应该这样。实际上,没有显示任何图像。故障显然是方法reloadImage(),在重新缩放图像或重新绘制JLabel时出现的,但我没有找到或纠正问题。有什么想法吗 public class Test extends JFrame {
JLabel
s中显示的图像
下面的课程是对这个概念的一个非常简单的测试。用户界面获取一个装满图像的文件夹(字段imageFolderPath
),并在唯一的JLabel
中显示第一个调整大小的图像;单击图像会提示UI在文件夹中显示以下图像
至少应该这样。实际上,没有显示任何图像。故障显然是方法reloadImage()
,在重新缩放图像或重新绘制JLabel
时出现的,但我没有找到或纠正问题。有什么想法吗
public class Test extends JFrame {
private JPanel contentPane;
private JLabel boardImage;
private ImageIcon icon;
public String imageFolderPath = "C:\\modify\\it\\to\\suit\\your\\needs\\";
public File[] files;
public int indexImage = 0;
public int imageResolution = 450;
// =========================================================
// TODO | Constructors
/**
* Create the frame.
*/
public Test() {
if( !new File(imageFolderPath).exists() )
new File(imageFolderPath).mkdir();
this.files = new File(imageFolderPath).listFiles();
System.out.println("Can show:");
for( File file : files )
System.out.println("\t"+file.getName());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, imageResolution, imageResolution);
contentPane = new JPanel();
contentPane.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
// Every time the image is clicked, another one is shown
indexImage++;
reloadImage();
}
});
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
boardImage = new JLabel();
boardImage.setBounds(0, 0, imageResolution, imageResolution);
reloadImage();
contentPane.add(boardImage);
}
// =========================================================
// TODO | Support methods
/** Reloads the image of the {@link ChessBoard} at its current state. */
public void reloadImage() {
if( files[indexImage % files.length].exists() )
System.out.println("The file " + files[indexImage % files.length].getName() + " exists");
else System.out.println("The file " + files[indexImage % files.length].getName() + " doesnt exists");
// Reload the image - resized
BufferedImage buffer = null;
try {
buffer = ImageIO.read( files[indexImage % files.length] );
} catch (IOException e) { e.printStackTrace(); }
Image rescaledImage = buffer.getScaledInstance(imageResolution, imageResolution, Image.SCALE_SMOOTH);
icon = new ImageIcon(rescaledImage);
boardImage = new JLabel(icon);
// boardImage.setText( files[indexImage % files.length].getName() );
System.out.println("Is now showing " + files[indexImage % files.length].getName() );
boardImage.repaint();
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
实际上,没有显示任何图像
不要创建新的JLabel。该JLabel的实例尚未添加到帧中
只需更改现有JLabel的图标即可:
//boardImage = new JLabel(icon);
boardImage.setIcon( icon );
标签将自动使用新图标重新绘制自身
实际上,没有显示任何图像
不要创建新的JLabel。该JLabel的实例尚未添加到帧中
只需更改现有JLabel的图标即可:
//boardImage = new JLabel(icon);
boardImage.setIcon( icon );
标签将自动使用新图标重新绘制自身