Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java swing组件未显示在JScrollPane中_Java_Swing - Fatal编程技术网

Java swing组件未显示在JScrollPane中

Java swing组件未显示在JScrollPane中,java,swing,Java,Swing,我正在尝试在可滚动的窗格中加载一些图像。但出于某种原因,它没有出现。下面是我添加图像的代码 private JFileChooser fileChooser = new JFileChooser(){ @Override public void approveSelection(){ File files[] = fileChooser.getSelectedFiles(); JPanel panel = new

我正在尝试在可滚动的窗格中加载一些图像。但出于某种原因,它没有出现。下面是我添加图像的代码

 private JFileChooser fileChooser = new JFileChooser(){
        @Override
        public void approveSelection(){
            File files[] = fileChooser.getSelectedFiles();
            JPanel panel = new JPanel(new GridLayout(files.length, 1));
            for(int lop=0; lop< files.length; lop++){

                BufferedImage image = null;
                try {                
                    image = ImageIO.read(files[lop]);
                } catch (IOException ex) {}
                BufferedImage img = new BufferedImage(100, 100, 1);
                Graphics2D g = img.createGraphics();
                g.drawImage(image, 0, 0, 100, 100, null);
                g.dispose();

                ImageIcon icon = new ImageIcon(img);
                JLabel lable = new JLabel(icon);
                panel.add(lable);    

            }
            jScrollPane1.getViewport().add(panel);    
            super.approveSelection();
        }
    };
private JFileChooser fileChooser=new JFileChooser(){
@凌驾
公共无效批准选举(){
File files[]=fileChooser.getSelectedFiles();
JPanel panel=newjpanel(newgridlayout(files.length,1));
for(int-lop=0;lop
使用上面的fileCHooser,我选择了一些要加载到垂直滚动窗格中的图像,不知何故,滚动窗格水平滚动器显示了长度的变化,但滚动窗格中没有内容。请查看下面的屏幕截图。在“形状”标题下:您将看到一个带有扩展滚动条的空容器

问候,,
Aqif Hamid

问题在于这行代码:

jScrollPane1.getViewport().add(new JFrame().add(panel));
为什么要创建JFrame

您应该像这样创建JScrollPane:

jScrollPane = new JScrollPane(panel);
jScrollpane.setViewportView(panel);
或按如下方式设置滚动窗格的视图:

jScrollPane = new JScrollPane(panel);
jScrollpane.setViewportView(panel);
另外,您应该只使用
面板。添加(标签)
。GridLayout会将标签放置在适当的位置。 你不应该忽视例外情况。将空的catch块转换为:

try {                
    image = ImageIO.read(files[lop]);
} 
catch (IOException ex) {
    throw new RuntimeException(ex);
}

抱歉,我编辑了我在jScrollpane.setViewportView(面板)上的帖子;它甚至不是那样工作的。我只是想用这种方法解决问题。Thnxs“swing组件没有出现在JScrollPane中”天啊,我认为这从来没有发生过……这一小时。但在最后一天可能会有两次。请以后搜索。