【问题标题】:Trouble using Cardlayout in java在 Java 中使用 Cardlayout 时遇到问题
【发布时间】:2014-05-04 00:23:16
【问题描述】:

在卡片布局的面板上添加组件时遇到问题,它们看起来很奇怪(非常小且顶部居中),尝试了许多布局但没有得到适当的结果,我必须在不同的面板上放置按钮、拆分窗格、选项卡窗格 这是示例代码。我现在正在工作的代码有同样的问题

请看看我哪里出错了

public static void main(String[] args) {

    CardLayout cards = new CardLayout();
    JPanel cardPanel = new JPanel();
    cardPanel.setLayout(cards);


    JFrame guiFrame = new JFrame();

    //make sure the program exits when the frame closes
    guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    guiFrame.setTitle("Frame");
    guiFrame.setSize(528, 555);

    //This will center the JFrame in the middle of the screen
    guiFrame.setLocationRelativeTo(null);
    guiFrame.setVisible(true);

    JButton B_1 = new JButton("");
    JButton B_2 = new JButton("");


    JPanel firstCard = new JPanel();

    firstCard.add(B_1);

    B_1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            cards.next(cardPanel);
        }
    });


    JPanel secondCard = new JPanel();

    secondCard.add(B_2);

    B_2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            cards.previous(cardPanel);
        }
    });

    cardPanel.add(firstCard);
    cardPanel.add(secondCard);

    guiFrame.add(cardPanel);

}
}

【问题讨论】:

  • 请解释一下你的奇怪出现是什么意思
  • 非常小,在面板的最顶部,我添加的任何东西看起来都是一样的,splitpane,tabpane,按钮等
  • 如果我在第一张卡片上添加拆分窗格,它不会显示

标签: java swing actionlistener cardlayout pane


【解决方案1】:

您的代码甚至无法编译。

我重新排列了您的代码并添加了以下功能:

  1. 我通过调用 SwingUtilities invokeLater 启动了您的 GUI。这会将 Swing GUI 放在 Event Dispatch thread (EDT) 上。

  2. 我将类似的代码(JFrame、JPanel)放在一起,这样代码更容易理解。

  3. 我在每个卡片面板上都放了一个 JLabel,这样您就可以看到哪个面板是哪个。

  4. 我将文本放入 JButtons 中。它们是如此之小,因为您没有可供按钮显示的文本或标签。

  5. 我将一些 JFrame 代码移到了方法的末尾。您在 Swing GUI 中做的最后一件事是设置框架可见。在使 JFrame 窗口可见之前,您必须构建所有 Swing 组件。

这是您尝试编写的卡片布局的有效、经过测试的最小示例。

import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CardLayoutTest implements Runnable {

    @Override
    public void run() {
        JFrame guiFrame = new JFrame();

        // make sure the program exits when the frame closes
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Frame");

        final CardLayout cards = new CardLayout();
        final JPanel cardPanel = new JPanel();
        cardPanel.setLayout(cards);

        final JPanel firstCard = new JPanel();
        firstCard.setLayout(new FlowLayout());

        JLabel label1 = new JLabel("Panel 1");
        firstCard.add(label1);

        JButton b_1 = new JButton("Swap to Panel 2");
        firstCard.add(b_1);

        final JPanel secondCard = new JPanel();
        secondCard.setLayout(new FlowLayout());

        JLabel label2 = new JLabel("Panel 2");
        secondCard.add(label2);

        JButton b_2 = new JButton("Swap to Panel 1");
        secondCard.add(b_2);

        b_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                 cards.next(cardPanel);
            }
        });

        b_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                 cards.previous(cardPanel);
            }
        });

        cardPanel.add(firstCard, "First Panel");
        cardPanel.add(secondCard, "Second Panel");

        guiFrame.add(cardPanel);
        guiFrame.setSize(528, 555);
        // This will center the JFrame in the middle of the screen
        guiFrame.setLocationRelativeTo(null);
        guiFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CardLayoutTest());
    }

}

【讨论】:

    【解决方案2】:

    CarLayout 是这样工作的,默认情况下,它把对象放在水平中心和垂直顶部。如果您没有指定添加组件的大小,则将其调整为可能的最小大小。 配置布局对齐方式、设置组件大小或使用其他布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      相关资源
      最近更新 更多