【问题标题】:Is there a way to insert multiple JScrollPanes into a JLayeredPane?有没有办法将多个 JScrollPanes 插入到 JLayeredPane 中?
【发布时间】:2021-03-23 19:22:58
【问题描述】:

简而言之,我正在尝试制作一个程序来模拟分配的流服务接口。该程序创建两个具有相同规格(大小、约束..)的单独 JScrollPanes,一个用于电影,一个用于系列,每个都包含一个 JPanel,然后包含其他电影或系列组件。然后,我创建了一个 JLayeredPane(不更改其布局),我想在其中添加两个 JScrollPanes 作为单独的层。但是,当将 JScrollPane 添加到 JLayeredPane 并运行代码时,JLayeredPane 始终显示最后添加的 JScrollPane,而不管我在添加时设置的索引。

        //creates the movie JScrollPane
        JScrollPane moviePanel = stuff.showMovies(movieLogic);

        //creates the series JScrollPane
        JScrollPane seriesPanel = stuff.showSeries(seriesLogic);

        //creates a JLayeredPane in which I want to insert the JScrollPanes as layers
        layerPanel = new JLayeredPane();
       

        //adds the moviePanel to index 3 and seriesPanel to index 5 in the JLayeredPane
        //I´m assuming index 3 is not as deep as index 5, meaning moviePanel should be in the front,
        //but I have also tried switching indexes just in case
        layerPanel.add(moviePanel, 3);
        layerPanel.add(seriesPanel, 5);
        
        //another attempt at getting the moviePanel to the front of the JLayeredPane, but
        //seriesPanel still shows up on top
        layerPanel.moveToFront(moviePanel); 

我错过了什么或做错了什么?

【问题讨论】:

  • 最好创建和发布一个像样的minimal reproducible example 程序。话虽如此,我确实想知道您是否想要使用 CardLayout,这是一种允许轻松交换可视化组件和容器的布局。 CardLayout tutorial
  • 这就是你做错了:layerPanel.setLayout(new BorderLayout());。这使 JLayeredPane 自己的内部布局无效并使其无法正常工作。您不应该明确设置 JLayeredPane 的布局并让它使用自己的本机布局。但是,您可以设置它所容纳的容器的布局。
  • 已编辑,我不再将 layerPanel 的布局设置为 BorderLayout。现在 JLayeredPane 什么都没有显示,我猜是因为我必须手动设置图层中 JScrollPanes 的大小和位置,但我不知道如何设置它,以便它占用所有可用空间层。你能为我指出正确的方向吗?当然也会继续进行谷歌搜索。
  • 按照已经建议的方式使用 CardLayout

标签: java swing user-interface jscrollpane jlayeredpane


【解决方案1】:

同样,最简单的解决方案可能是使用 JList 和交换模型。例如,下面使用了两个模型,一个在一周中的其他日子显示颜色字符串,并且模型在按钮的动作侦听器中交换:

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

public class SwapStuff extends JPanel {
    public static final String[] DAYS_OF_WEEK = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
            "Saturday" };
    public static final String[] COLORS = { "Red", "Orange", "Yellow", "Green", "Blue", "Violet" };
    private DefaultListModel<String> dayModel = new DefaultListModel<>();
    private DefaultListModel<String> colorModel = new DefaultListModel<>();
    private JList<String> list = new JList<>(dayModel);
    private JTextField displayField = new JTextField(20);

    public SwapStuff() {
        for (String element : DAYS_OF_WEEK) {
            dayModel.addElement(element);
        }
        for (String element : COLORS) {
            colorModel.addElement(element);
        }
        list.setVisibleRowCount(5);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scrollPane = new JScrollPane(list);

        displayField.setFocusable(false);

        JButton weekdayBtn = new JButton("Weekdays");
        JButton colorBtn = new JButton("Colors");

        weekdayBtn.addActionListener(e -> {
            if (dayModel != list.getModel()) {
                list.setModel(dayModel);
            }
        });
        colorBtn.addActionListener(e -> {
            if (colorModel != list.getModel()) {
                list.setModel(colorModel);
            }
        });
        list.addListSelectionListener(e -> {
            String selection = list.getSelectedValue();
            displayField.setText(selection);
        });

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(weekdayBtn);
        buttonPanel.add(colorBtn);

        setLayout(new BorderLayout());
        add(displayField, BorderLayout.PAGE_START);
        add(scrollPane);
        add(buttonPanel, BorderLayout.PAGE_END);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        SwapStuff mainPanel = new SwapStuff();
        JFrame frame = new JFrame("SwapStuff");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-04
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多