有没有办法在加载时调整图像大小?图像链接到 JList
这是我写的代码。它唯一的问题是我无法调整图像大小。我尝试过 getScaledInstance
不起作用。关于如何解决这个问题的任何想法。另外,为什么我必须使用容器来显示图片,面板是否足以显示结果,但在添加容器之前我没有得到任何输出。
package split;
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.Container;
public class Split extends JFrame implements ListSelectionListener{
JFrame f;
JSplitPane s1;
JList list1;
JPanel listP,ImgP;
JLabel img;
Container contentPane;
String a[] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
Split(){
list1 = new JList(a);
list1.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e) {
switch(list1.getSelectedIndex()){
case 0:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\Mercury_profile_MDIS_MESSENGER.jpg"));
break;
case 1:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\Venus_from_Mariner_10.jpg"));
break;
case 2:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\download.jpg"));
break;
case 3:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\download (1).jpg"));
break;
case 4:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\Jupiter_and_its_shrunken_Great_Red_Spot.jpg"));
break;
case 5:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\download (2).jpg"));
break;
case 6:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\download (3).jpg"));
break;
case 7:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\Neptune_-_Voyager_2_(29347980845)_flatten_crop.jpg"));
break;
}
}
});
listP = new JPanel();
listP.add(list1);
img = new JLabel();
ImgP = new JPanel();
ImgP.add(img);
s1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,listP, ImgP);
s1.setOneTouchExpandable(true);
s1.setDividerLocation(150);
contentPane = getContentPane();
contentPane.add(s1);
setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
new Split();
}
@Override
public void valueChanged(ListSelectionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
This is the code I wrote. It works only problem I can't resize the images. I've tried out getScaledInstance
wasn't working. Any ideas on how to fix this. Also, why did I have to use a container to show the pictures shouldn't the panels be enough to show the results but until I added the container I wasn't getting any outputs.
package split;
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.Container;
public class Split extends JFrame implements ListSelectionListener{
JFrame f;
JSplitPane s1;
JList list1;
JPanel listP,ImgP;
JLabel img;
Container contentPane;
String a[] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
Split(){
list1 = new JList(a);
list1.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e) {
switch(list1.getSelectedIndex()){
case 0:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\Mercury_profile_MDIS_MESSENGER.jpg"));
break;
case 1:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\Venus_from_Mariner_10.jpg"));
break;
case 2:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\download.jpg"));
break;
case 3:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\download (1).jpg"));
break;
case 4:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\Jupiter_and_its_shrunken_Great_Red_Spot.jpg"));
break;
case 5:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\download (2).jpg"));
break;
case 6:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\download (3).jpg"));
break;
case 7:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\Neptune_-_Voyager_2_(29347980845)_flatten_crop.jpg"));
break;
}
}
});
listP = new JPanel();
listP.add(list1);
img = new JLabel();
ImgP = new JPanel();
ImgP.add(img);
s1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,listP, ImgP);
s1.setOneTouchExpandable(true);
s1.setDividerLocation(150);
contentPane = getContentPane();
contentPane.add(s1);
setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
new Split();
}
@Override
public void valueChanged(ListSelectionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
JList
时,所需的任何大小调整都可以在渲染器中完成。例如,选择时左侧列表中的图像是主视图大小的 10%。这是渲染器:
这是使用渲染器的独立源代码:
When using a
JList
, any resizing that is required can be done in the renderer. E.G. the images in the list on the left are 10% of the size of the main view when selected.Here is the renderer:
Here is the self-contained source code that uses the renderer:
我可能建议使用
JScrollPane
,这将允许您的JLabel
/image 占据比窗口当前可用的空间更多的空间,例如...参见如何使用滚动窗格了解更多详细信息。
您还应该学习使用
ImageIO.read
,从长远来看,它会为您节省很多问题和麻烦,请参阅 读取/加载图像如果您仍然想调整图像大小,那么您还可以看看...
作为几个示例
I might suggest using a
JScrollPane
, which would allow yourJLabel
/image to occupy more space then is currently available to the window, for example...See How to Use Scroll Panes for more details.
You should also learn to make use of
ImageIO.read
, it will save you a lot of issues and hair pulling in the long run, see Reading/Loading an ImageIf you still want to resize the image, then you could also have a look at ...
as few examples