私のアプリケーションの一部はチュートリアルとして機能しています。その目的のために、コンテンツとして画像を持つ JLabel を表示する JPanels を取得しました。ただし、画像が画面に収まるサイズよりも大きい場合は、収まらない部分が切り取られます。私がする必要があるのは、JLabel が与えられたスペースに収まるように画像のサイズを変更することです。JScrollPanes も使用してみましたが、違いはありませんでした (ただし、画像のサイズを変更することを好みます)。
を使用して、画像のスケーリングされたインスタンスを取得しようとしました
Image scaledImg = myPicture.getScaledInstance(width, height, Image.SCALE_SMOOTH);
しかし、それは何の違いもありませんでした。
これは現在のコードです:
private JLabel getTutorialLabel(int type) {
String path = "/Images/Tutorial/test" + type + ".png";
try {
BufferedImage tutorialPic = ImageIO.read(getClass().getResource(path));
// int height = (int) (screenDim.height * 0.9);
// int width = (int) (screenDim.width * 0.9);
// Image scaledImg = myPicture.getScaledInstance(width, height, Image.SCALE_SMOOTH);
JLabel picLabel = new JLabel(new ImageIcon(tutorialPic));
return picLabel;
} catch (IOException ex) {
Logger.getLogger(Tutorial.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
画像を正しく表示するにはどうすればよいですか?
JLabel (JMenuBar と画面下部のいくつかのボタン) と共に表示される他のコンポーネントがあることに注意してください。そのため、JLabel が指定されたスペースを埋めるために画像を作成する必要があります。