使用 JScrollPane 和 JLayeredPane 的 Swing GUI 设计 [英] Swing GUI design with JScrollPane and JLayeredPane

查看:37
本文介绍了使用 JScrollPane 和 JLayeredPane 的 Swing GUI 设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个如下图所示的 GUI 设置.

JLayeredPane 的大小应该始终相同,但 JPanelJScrollPane 的大小可以改变.我需要 JScrollPane 才能通过单击箭头显示 JLayedPane,如果 JPanelJScrollPane> 不够大,无法显示整个 JLayeredPane.

问题在于,对于下面的代码,JLayeredPane 总是会扩展以适应 JScrollPane 的大小,如果 JScrollPane> 小于 JLayeredPane,它不提供滚动能力.

对正在发生的事情有什么想法吗?有没有更简单的代码来实现这一点?

谢谢

<前><代码>contentPanePanel = new javax.swing.JPanel();contentPaneScollPane = new javax.swing.JScrollPane();contentPane = new javax.swing.JLayeredPane();contentPanePanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));contentPane.setRequestFocusEnabled(false);contentPane.setVerifyInputWhenFocusTarget(false);contentPaneScollPane.setViewportView(contentPane);javax.swing.GroupLayout contentPanePanelLayout = new javax.swing.GroupLayout(contentPanePanel);contentPanePanel.setLayout(contentPanePanelLayout);contentPanePanelLayout.setHorizo​​ntalGroup(contentPanePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(contentPaneScollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 968, Short.MAX_VALUE));contentPanePanelLayout.setVerticalGroup(contentPanePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(contentPaneScollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 628, Short.MAX_VALUE));

解决方案

是的,诀窍是将分层窗格添加到使用布局管理器的面板中,该布局管理器将保持分层窗格居中.类似的东西:

import java.awt.*;导入 javax.swing.*;导入 javax.swing.text.*;公共类 ScrollPaneSSCCE{私有静态无效 createAndShowUI(){JPanel 中心 = 新 JPanel();center.setPreferredSize(new Dimension(300, 300));center.setBackground(Color.RED);JPanel main = new JPanel( new GridBagLayout() );main.add(center, new GridBagConstraints());JFrame frame = new JFrame("Basic Sc​​rollPaneSSCCE");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.add( new JScrollPane( main ) );frame.setSize(400, 400);frame.setLocationRelativeTo( null );frame.setVisible(true);}public static void main(String[] args){EventQueue.invokeLater(new Runnable(){公共无效运行(){createAndShowUI();}});}}

I would like to have a GUI setup like the diagram below.

The JLayeredPane should always be the same size, but the JPanel and the JScrollPane can change in size. I need the JScrollPane to be able to display the JLayedPane by clicking the arrows and what not if the JPanel and JScrollPane are not large enough to show the entire JLayeredPane.

The problem is that with the code below, the JLayeredPane always expands to fit the size of the JScrollPane and in the event that the JScrollPane is smaller than the JLayeredPane, it does not provide the scrolling ability.

Any Ideas on what is going on? Is there simpler code to achieve this?

Thanks


contentPanePanel = new javax.swing.JPanel();
contentPaneScollPane = new javax.swing.JScrollPane();
contentPane = new javax.swing.JLayeredPane();

contentPanePanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

contentPane.setRequestFocusEnabled(false);
contentPane.setVerifyInputWhenFocusTarget(false);

contentPaneScollPane.setViewportView(contentPane);

javax.swing.GroupLayout contentPanePanelLayout = new javax.swing.GroupLayout(contentPanePanel);

contentPanePanel.setLayout(contentPanePanelLayout);

contentPanePanelLayout.setHorizontalGroup(
    contentPanePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(contentPaneScollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 968, Short.MAX_VALUE)
);
contentPanePanelLayout.setVerticalGroup(
    contentPanePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(contentPaneScollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 628, Short.MAX_VALUE)
);

解决方案

Yes, the trick is to add the layered pane to a panel that uses a layout manager which will keep the layered pane centered. Something like:

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

public class ScrollPaneSSCCE
{
    private static void createAndShowUI()
    {
        JPanel center = new JPanel();
        center.setPreferredSize( new Dimension(300, 300) );
        center.setBackground( Color.RED );

        JPanel main = new JPanel( new GridBagLayout() );
        main.add(center, new GridBagConstraints());

        JFrame frame = new JFrame("Basic ScrollPaneSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new JScrollPane( main ) );
        frame.setSize(400, 400);
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

这篇关于使用 JScrollPane 和 JLayeredPane 的 Swing GUI 设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆