java – 我可以在CardLayout中设置各个面板的大小吗?

当我创建我的GUI时,我使用cardlayout来保存我的不同面板,因为我相信很多人都知道.这会将我的屏幕设置为我最大面板的宽度和高度.这会导致我的第一个屏幕的美观问题,这个屏幕比SudokuPanel和CalkuroPanel小得多.

当我换到更大的屏幕时,我尝试设置首选大小,但无济于事.

任何帮助链接到良好的信息或任何通常只会帮助的人将非常感激:).

请在下面找到我的主要课程(我在哪里画GUI):

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

public class puzzleGUI{
  private static JPanel screens;
  public static String username;
  static public void main (String[] args){
  JFrame puzzle = new JFrame ("Sudoku/Calkuro Pro");

  ...

  PuzzleStartPanel startPanel = new PuzzleStartPanel();
  PuzzleChoosePanel choosePanel = new PuzzleChoosePanel();
  PuzzleSudokuPanel sudokuPanel = new PuzzleSudokuPanel(); 
  PuzzleCalkuroPanel calkuroPanel = new PuzzleCalkuroPanel();

  screens = new JPanel(new cardlayout()); 
  screens.add(startPanel,"start");
  screens.add(choosePanel,"choosePuzzle");
  screens.add(sudokuPanel,"sudoku");
  screens.add(calkuroPanel,"calkuro");
  screens.setPreferredSize (new Dimension(250,80));

  puzzle.setJMenuBar(menuBar);
  puzzle.getContentPane().add(screens);
  puzzle.pack();
  puzzle.setVisible(true);
  puzzle.setResizable(false);
  puzzle.setDefaultCloSEOperation (JFrame.EXIT_ON_CLOSE);
}

static public void getUsername(String str){
  username = str;
}

static public void openWindow(String newFrame){
  cardlayout cl = (cardlayout)(screens.getLayout());
  cl.show(screens,newFrame);
}
}

编辑

调用openWindow时重置首选大小后,brainblast试图打包框架,并且wolah,新框架大小:D

static public void openWindow(String newFrame,int a,int b){
  cardlayout cl = (cardlayout)(screens.getLayout());
  cl.show(screens,newFrame);
  screens.setPreferredSize (new Dimension(a,b));
  puzzle.pack();
}
最佳答案

can i set the size of individual panels in a cardlayout

当然.但布局将忽略它们并使每张卡的大小相同.您可以期望的最好的方法是将较小的面板添加到另一个允许内容缩小的面板(具有布局).

This answer显示了使用单个标签的这种技术.交换“小面板”的标签,并使用右侧的布局,它将居中.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...