简体   繁体   English

Java Swing混合窗格

[英]Java Swing mixing panes

This seems like a newbie question except that I've been trying to wrap my head around the Swing framework for loong time. 这看起来像一个新手问题,除了我一直试图绕着Swing框架包围我的时间。

Provided you provide an image, dog.jpg, at least 500 px square, the following code should display the image in a scrollpane. 如果您提供图像dog.jpg,至少500 px square,以下代码应在滚动窗格中显示图像。 If it displayed anything , I probably wouldn't throw my hands up in despair. 如果它显示任何东西 ,我可能不会绝望地举起双手。 What am I missing? 我错过了什么?

import java.awt.BorderLayout;
import javax.swing.*;

public class ScrollSample {
  public static void main(String args[]) {
    String title = (args.length == 0 ? "JScrollPane Sample" : args[0]);
    new ScrollSample( title ) ;
    }

  public ScrollSample ( String title) {
    JFrame frame = new JFrame(title);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Icon icon = new ImageIcon("dog.jpg");
    JLabel dogLabel = new JLabel(icon);
    dogLabel.setSize( 500, 500 ) ;

    JLayeredPane layeredPane = new JLayeredPane() ;
    layeredPane.add( dogLabel, new Integer( 0 )) ;

    JPanel jp = new JPanel() ;
    jp.add( layeredPane ) ;
    jp.setSize( 500, 500 ) ;

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(jp);

    frame.getContentPane().add( scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

Thanks! 谢谢!

You must set the preferred size for JLayeredPane if you are drawing components of larger widths and sizes to it. 如果要绘制更大宽度和大小的组件,则必须为JLayeredPane设置首选大小。 Especially since you are adding it to a JPanel with default layout. 特别是因为您要将其添加到具有默认布局的JPanel。 JLayeredPane s don't have layout managers by default - so either you manage the bounds or add a preferred layout manager to the layered pane. 默认情况下, JLayeredPane没有布局管理器 - 因此要么管理边界,要么将首选布局管理器添加到分层窗格。 The simple way would be: 简单的方法是:

After

 JLayeredPane layeredPane = new JLayeredPane() ;

add

 layeredPane.setPreferredSize(new Dimension(500,500));

And then maximize your window ( or set your JFrame 's size to 600X600) when the app runs. 然后在应用运行时最大化您的窗口(或将JFrame的大小设置为600X600)。

Read up on : How to Use Layered Panes 阅读: 如何使用分层窗格

  • The default layout of a JPanel is a Flowlayout. JPanel的默认布局是Flowlayout。 A FlowLayout shows each component at its preferred size and has a 5 pixel border. FlowLayout以其首选大小显示每个组件,并具有5​​像素边框。 Use a BorderLayout instead (or add the layered pane directly to the scroll pane). 改为使用BorderLayout(或将分层窗格直接添加到滚动窗格)。
  • The default preferred size of a JLayeredPane is (0, 0). JLayeredPane的默认首选大小是(0,0)。 Set a preferred size for it. 为它设置首选大小。

A Swing GUI should be started on the EDT. 应该在EDT上启动Swing GUI。 Left as an exercise for the user. 留下作为用户的练习。

import java.awt.*;
import javax.swing.*;
import java.net.URL;

public class ScrollSample {

    public static void main(String args[]) throws Exception {
        final URL url = new URL("http://pscode.org/media/stromlo2.jpg");
        String title = (args.length == 0 ? "JScrollPane Sample" : args[0]);
        new ScrollSample( title, url ) ;
    }

    public ScrollSample ( String title, URL url) {
        JFrame frame = new JFrame(title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Icon icon = new ImageIcon(url);
        JLabel dogLabel = new JLabel(icon);
        dogLabel.setBounds(0,0,640,480);

        JLayeredPane layeredPane = new JLayeredPane() ;
        layeredPane.add( dogLabel, new Integer( 0 )) ;
        layeredPane.setPreferredSize( new Dimension(500, 500) ) ;

        JPanel jp = new JPanel(new BorderLayout()) ;
        jp.add( layeredPane ) ;

        JScrollPane scrollPane = new JScrollPane(jp);

        frame.getContentPane().add( scrollPane, BorderLayout.CENTER);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM