是否可以在不禁用CardLayout的情况下添加ScrollPane?

时间:2019-10-04 17:37:37

标签: java swing jframe

我有一个卡片布局,可以通过按钮切换面板。但是,该代码(切换面板)仅在以下行时有效:

    JScrollPane scrPane = new JScrollPane(card1);
    frame.add(scrPane);

被删除。在其他情况下,单击按钮不会有任何效果。有没有保持滚动的选项(我需要这个,因为主应用程序会有很多换行的文字),而没有禁用切换卡片的选项?

package com.code;

import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.event.*;

public class Card {

    public static void main(String[] args) {

        JFrame frame = new JFrame("App");
        frame.setVisible(true);
        frame.setSize(1200, 800);//Give it a size
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel mainPanel = new JPanel(new CardLayout());
        frame.add(mainPanel);

        JPanel menu = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel card1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel card2 = new JPanel(new FlowLayout(FlowLayout.LEFT));

        mainPanel.add(menu, "menu");
        mainPanel.add(card1, "card1");
        mainPanel.add(card2, "card2");

        JLabel l1 = new JLabel("label 1");
        JLabel l2 = new JLabel("label 2");
        card1.add(l1);
        card2.add(l2);
        JButton click = new JButton("Click!");
        menu.add(click);

        JScrollPane scrPane = new JScrollPane(card1);
        frame.add(scrPane);

        click.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
                cardLayout.show(mainPanel, "card1");
            }
        });

    }

}

1 个答案:

答案 0 :(得分:2)

默认情况下,一个JFrame(其内容窗格)uses BorderLayout。这意味着BorderLayout.CENTER只能有1个组件。当您frame.add(component)时,默认约束为BorderLayout.CENTER

现在,您先frame.add(mainPanel);,然后再frame.add(scrPane);。由于要在其后添加scrPane,因此要删除主面板。

执行JScrollPane scrPane = new JScrollPane(card1);意味着您将滚动窗格添加到card1,而不是在内容窗格中。我想您希望它显示在内容窗格(整个框架)中。因此,解决方法是删除frame.add(mainPanel);并执行以下操作:

JScrollPane scrPane = new JScrollPane(mainPanel);
frame.add(scrPane);

现在,将主面板添加到scrPane,并将scrPane添加到框架。

但是,在那之后,您的GUI将为空,因为在完成向其添加组件之前您frame.setVisible(true);。看看Why shouldn't I call setVisible(true) before adding components?

最终,完整代码为:

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        JFrame frame = new JFrame("App");

        frame.setSize(1200, 800);//Give it a size
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel mainPanel = new JPanel(new CardLayout());

        JPanel menu = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel card1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel card2 = new JPanel(new FlowLayout(FlowLayout.LEFT));

        mainPanel.add(menu, "menu");
        mainPanel.add(card1, "card1");
        mainPanel.add(card2, "card2");

        JLabel l1 = new JLabel("label 1");
        JLabel l2 = new JLabel("label 2");
        card1.add(l1);
        card2.add(l2);
        JButton click = new JButton("Click!");
        menu.add(click);

        JScrollPane scrPane = new JScrollPane(mainPanel);
        frame.add(scrPane);

        click.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
                cardLayout.show(mainPanel, "card1");
            }
        });
        frame.setVisible(true);
    });
}

我建议您阅读一些不错的链接,例如the Initial ThreadsWhat does .pack() do?