使用 CardLayout 更改 JPanel 的大小

发布于 2024-10-25 13:16:46 字数 46 浏览 7 评论 0原文

使用 Java CardLayout 时是否可以更改 Jpanels 的大小?

is it possible to change the size of Jpanels when using Java CardLayout?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2024-11-01 13:16:46

拍摄,类似这样的组件(这里是 JLabel 而不是 JPanel)设置了它的 PreferredSize,然后将其放置在另一个使用适当布局的 JPanel 中,这里的 GridBagLayout 使用默认设置将使组件居中,并使用添加 GridBagLayout使用 panel:: JPanel 到 CardLayout

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;

public class MultiSizedPanels {

   private static void createAndShowUI() {
      final CardLayout cardLayout = new CardLayout();
      final JPanel cardHolder = new JPanel(cardLayout);

      JLabel[] labels = {
         new JLabel("Small Label", SwingConstants.CENTER),
         new JLabel("Medium Label", SwingConstants.CENTER),
         new JLabel("Large Label", SwingConstants.CENTER)};

      for (int i = 0; i < labels.length; i++) {
         int padding = 50;
         Dimension size = labels[i].getPreferredSize();
         size = new Dimension(size.width + 2 * (i + 1) * padding, size.height + 2 * (i + 1) * padding);
         labels[i].setPreferredSize(size);
         Border lineBorder = BorderFactory.createLineBorder(Color.blue);
         labels[i].setBorder(lineBorder);
         JPanel containerPanel = new JPanel(new GridBagLayout());
         containerPanel.add(labels[i]);
         cardHolder.add(containerPanel, String.valueOf(i));
      }

      JButton nextButton = new JButton("Next");
      nextButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            cardLayout.next(cardHolder);
         }
      });
      JPanel btnHolder = new JPanel();
      btnHolder.add(nextButton);

      JFrame frame = new JFrame("MultiSizedPanels");
      frame.getContentPane().add(cardHolder, BorderLayout.CENTER);
      frame.getContentPane().add(btnHolder, BorderLayout.SOUTH);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

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

shoot, something like this where the component (here a JLabel rather than a JPanel) has it's preferredSize set, then place it in another JPanel that uses an appropriate layout, here GridBagLayout which with default settings will center the component, and add the GridBagLayout using JPanel to the CardLayout using panel::

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;

public class MultiSizedPanels {

   private static void createAndShowUI() {
      final CardLayout cardLayout = new CardLayout();
      final JPanel cardHolder = new JPanel(cardLayout);

      JLabel[] labels = {
         new JLabel("Small Label", SwingConstants.CENTER),
         new JLabel("Medium Label", SwingConstants.CENTER),
         new JLabel("Large Label", SwingConstants.CENTER)};

      for (int i = 0; i < labels.length; i++) {
         int padding = 50;
         Dimension size = labels[i].getPreferredSize();
         size = new Dimension(size.width + 2 * (i + 1) * padding, size.height + 2 * (i + 1) * padding);
         labels[i].setPreferredSize(size);
         Border lineBorder = BorderFactory.createLineBorder(Color.blue);
         labels[i].setBorder(lineBorder);
         JPanel containerPanel = new JPanel(new GridBagLayout());
         containerPanel.add(labels[i]);
         cardHolder.add(containerPanel, String.valueOf(i));
      }

      JButton nextButton = new JButton("Next");
      nextButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            cardLayout.next(cardHolder);
         }
      });
      JPanel btnHolder = new JPanel();
      btnHolder.add(nextButton);

      JFrame frame = new JFrame("MultiSizedPanels");
      frame.getContentPane().add(cardHolder, BorderLayout.CENTER);
      frame.getContentPane().add(btnHolder, BorderLayout.SOUTH);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文