在 scrollPane 中显示多个图像导致组件根据图像大小丢失
Displaying multiple Images in scrollPane results in components missing based on image size
我正在使用以下结构根据需要在标签上显示最多 3 张图像:
TabbedPane
|_ScrollPane
|_Panel with BorderLayout
|_Label with ImageIcon (Placed North)
|_Label with ImageIcon (Placed Center)
|_Panel with BorderLayout (Placed South)
|_Label with ImageIcon (Placed North)
|_Button (Placed West)
|_Button (Placed East)
放置在标签上的图像每张为1654*2340像素("null.jpg"为1*1像素)
通过另一个选项卡上的按钮,我可以打印出我认为在测试期间很重要的各种参数(主要是 size/position 组件)。
如果我使用 "null.jpg" 作为第三个标签的图片,则一切正常;我看到 2 张图片和按钮(如果几乎看不到 "null.jpg" 的附加像素)。
如果我现在切换到使用另一个 1654 * 2340 像素的图像,一切都会变得疯狂:按钮丢失,第三张图片未显示并且(查看附加按钮给出的参数)两个按钮的大小应该是那里是-2305像素。
尺寸是由第二个面板(2340 像素)上显示(未)显示的图像和第二个面板的高度(35 像素)-> 35-2340=-2305
但是我不知道为什么会发生这种情况,因为使用较小的图像一切正常。正如您从下面的代码中看到的,我检查了各种参数,从面板上的标签大小到按钮位置和大小。
为什么会发生这种情况,我该如何解决?我连续搜索了 4 个小时,但找不到发生这种情况的原因。下面是实际重现问题的测试示例代码。删除 ImageIcon p3
上的注释并将其放在定义 p3
returns 不同结果的另一行前面。
同样:p1-p3 是大小为 1654*2340 像素的图像,其中 "null" 是 1*1.
如果还有其他需要,我很乐意提供帮助!
非常感谢任何帮助!
package my_package;
import java.awt.EventQueue;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.net.MalformedURLException;
public class testsforSO extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testsforSO frame = new testsforSO();
frame.setSize(300, 300);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public testsforSO() {
getContentPane().setLayout(new BorderLayout(0, 0));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
getContentPane().add(tabbedPane);
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane();
tabbedPane.addTab("Pictures", null, scrollPane, null);
JPanel panel_1 = new JPanel();
scrollPane.setViewportView(panel_1);
panel_1.setLayout(new BorderLayout(0, 0));
JLabel lblNewLabel = new JLabel();
panel_1.add(lblNewLabel, BorderLayout.NORTH);
JLabel lblNewLabel_1 = new JLabel();
panel_1.add(lblNewLabel_1, BorderLayout.CENTER);
JPanel panel_2 = new JPanel();
panel_1.add(panel_2, BorderLayout.SOUTH);
panel_2.setPreferredSize(new Dimension(scrollPane.getWidth(), 35));
panel_2.setLayout(new BorderLayout(0, 0));
JLabel lblNewLabel_2 = new JLabel();
panel_2.add(lblNewLabel_2, BorderLayout.NORTH);
JButton btnNewButton = new JButton("Saving Button");
panel_2.add(btnNewButton, BorderLayout.WEST);
JButton btnNewButton_1 = new JButton("Printing Button");
panel_2.add(btnNewButton_1, BorderLayout.EAST);
try {
ImageIcon p1 = new ImageIcon(new File("C:\path\to\p1.jpg").toURI().toURL());
ImageIcon p2 = new ImageIcon(new File("C:\path\to\p2.jpg").toURI().toURL());
ImageIcon p3 = new ImageIcon(new File("C:\path\to\p3.jpg").toURI().toURL());
//ImageIcon p3 = new ImageIcon(new File("C:\path\to\null.jpg").toURI().toURL());
lblNewLabel.setIcon(p1);
lblNewLabel_1.setIcon(p2);
lblNewLabel_2.setIcon(p3);
} catch (MalformedURLException e) {
e.printStackTrace();
}
JButton btnMyButton = new JButton("Details");
btnMyButton.setBounds(10, 11, 89, 23);
btnMyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("1 Height"+lblNewLabel.getHeight()+" Y: "+ lblNewLabel.getY());
System.out.println("2 Height"+lblNewLabel_1.getHeight()+" Y: "+ lblNewLabel_2.getY());
System.out.println("3 Width: "+lblNewLabel_2.getSize().getWidth()+" Height: "+lblNewLabel_2.getSize().getHeight());
System.out.println("3 x/y: "+SwingUtilities.convertPoint(lblNewLabel_2, lblNewLabel_2.getLocation(), panel_1).toString());
System.out.println("End of 2: "+(lblNewLabel_1.getY()+lblNewLabel_1.getHeight()));
System.out.println("Panel_2.isVisible: "+Boolean.toString(panel_2.isVisible()));
System.out.println("Panel_2 x/y/w/h: "+panel_2.getX()+" "+panel_2.getY()+" "+panel_2.getWidth()+" "+panel_2.getHeight());
System.out.println("Saving Button x/y/w/h: "+btnNewButton.getX()+" "+btnNewButton.getY()+" "+btnNewButton.getWidth()+" "+btnNewButton.getHeight());}
});
panel.setLayout(null);
panel.add(btnMyButton);
tabbedPane.add("Update Button", panel);
}
}
快速编辑:我没有收到任何异常或编译器错误...
您已使用以下行将包含第三张图片和按钮的面板的最大高度限制为 35 像素:
panel_2.setPreferredSize(new Dimension(scrollPane.getWidth(), 35));
这足以在图像大小为零时显示按钮。但是对于大图,只显示了上半部分,没有 space 留给按钮。
删除此行将解决您的问题。
我正在使用以下结构根据需要在标签上显示最多 3 张图像:
TabbedPane
|_ScrollPane
|_Panel with BorderLayout
|_Label with ImageIcon (Placed North)
|_Label with ImageIcon (Placed Center)
|_Panel with BorderLayout (Placed South)
|_Label with ImageIcon (Placed North)
|_Button (Placed West)
|_Button (Placed East)
放置在标签上的图像每张为1654*2340像素("null.jpg"为1*1像素)
通过另一个选项卡上的按钮,我可以打印出我认为在测试期间很重要的各种参数(主要是 size/position 组件)。
如果我使用 "null.jpg" 作为第三个标签的图片,则一切正常;我看到 2 张图片和按钮(如果几乎看不到 "null.jpg" 的附加像素)。
如果我现在切换到使用另一个 1654 * 2340 像素的图像,一切都会变得疯狂:按钮丢失,第三张图片未显示并且(查看附加按钮给出的参数)两个按钮的大小应该是那里是-2305像素。
尺寸是由第二个面板(2340 像素)上显示(未)显示的图像和第二个面板的高度(35 像素)-> 35-2340=-2305
但是我不知道为什么会发生这种情况,因为使用较小的图像一切正常。正如您从下面的代码中看到的,我检查了各种参数,从面板上的标签大小到按钮位置和大小。
为什么会发生这种情况,我该如何解决?我连续搜索了 4 个小时,但找不到发生这种情况的原因。下面是实际重现问题的测试示例代码。删除 ImageIcon p3
上的注释并将其放在定义 p3
returns 不同结果的另一行前面。
同样:p1-p3 是大小为 1654*2340 像素的图像,其中 "null" 是 1*1.
如果还有其他需要,我很乐意提供帮助!
非常感谢任何帮助!
package my_package;
import java.awt.EventQueue;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.net.MalformedURLException;
public class testsforSO extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testsforSO frame = new testsforSO();
frame.setSize(300, 300);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public testsforSO() {
getContentPane().setLayout(new BorderLayout(0, 0));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
getContentPane().add(tabbedPane);
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane();
tabbedPane.addTab("Pictures", null, scrollPane, null);
JPanel panel_1 = new JPanel();
scrollPane.setViewportView(panel_1);
panel_1.setLayout(new BorderLayout(0, 0));
JLabel lblNewLabel = new JLabel();
panel_1.add(lblNewLabel, BorderLayout.NORTH);
JLabel lblNewLabel_1 = new JLabel();
panel_1.add(lblNewLabel_1, BorderLayout.CENTER);
JPanel panel_2 = new JPanel();
panel_1.add(panel_2, BorderLayout.SOUTH);
panel_2.setPreferredSize(new Dimension(scrollPane.getWidth(), 35));
panel_2.setLayout(new BorderLayout(0, 0));
JLabel lblNewLabel_2 = new JLabel();
panel_2.add(lblNewLabel_2, BorderLayout.NORTH);
JButton btnNewButton = new JButton("Saving Button");
panel_2.add(btnNewButton, BorderLayout.WEST);
JButton btnNewButton_1 = new JButton("Printing Button");
panel_2.add(btnNewButton_1, BorderLayout.EAST);
try {
ImageIcon p1 = new ImageIcon(new File("C:\path\to\p1.jpg").toURI().toURL());
ImageIcon p2 = new ImageIcon(new File("C:\path\to\p2.jpg").toURI().toURL());
ImageIcon p3 = new ImageIcon(new File("C:\path\to\p3.jpg").toURI().toURL());
//ImageIcon p3 = new ImageIcon(new File("C:\path\to\null.jpg").toURI().toURL());
lblNewLabel.setIcon(p1);
lblNewLabel_1.setIcon(p2);
lblNewLabel_2.setIcon(p3);
} catch (MalformedURLException e) {
e.printStackTrace();
}
JButton btnMyButton = new JButton("Details");
btnMyButton.setBounds(10, 11, 89, 23);
btnMyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("1 Height"+lblNewLabel.getHeight()+" Y: "+ lblNewLabel.getY());
System.out.println("2 Height"+lblNewLabel_1.getHeight()+" Y: "+ lblNewLabel_2.getY());
System.out.println("3 Width: "+lblNewLabel_2.getSize().getWidth()+" Height: "+lblNewLabel_2.getSize().getHeight());
System.out.println("3 x/y: "+SwingUtilities.convertPoint(lblNewLabel_2, lblNewLabel_2.getLocation(), panel_1).toString());
System.out.println("End of 2: "+(lblNewLabel_1.getY()+lblNewLabel_1.getHeight()));
System.out.println("Panel_2.isVisible: "+Boolean.toString(panel_2.isVisible()));
System.out.println("Panel_2 x/y/w/h: "+panel_2.getX()+" "+panel_2.getY()+" "+panel_2.getWidth()+" "+panel_2.getHeight());
System.out.println("Saving Button x/y/w/h: "+btnNewButton.getX()+" "+btnNewButton.getY()+" "+btnNewButton.getWidth()+" "+btnNewButton.getHeight());}
});
panel.setLayout(null);
panel.add(btnMyButton);
tabbedPane.add("Update Button", panel);
}
}
快速编辑:我没有收到任何异常或编译器错误...
您已使用以下行将包含第三张图片和按钮的面板的最大高度限制为 35 像素:
panel_2.setPreferredSize(new Dimension(scrollPane.getWidth(), 35));
这足以在图像大小为零时显示按钮。但是对于大图,只显示了上半部分,没有 space 留给按钮。
删除此行将解决您的问题。