【问题标题】:Java BorderLayout: How to set Preferred size of componentsJava BorderLayout:如何设置组件的首选大小
【发布时间】:2021-02-21 01:05:03
【问题描述】:

我有一个带有两个面板的框架,一个名为 ScreenPanel,一个名为 MenuPanel,两者都有边框布局管理器。 ScreenPanel 是 JFrame 的内容面板。菜单面板位于 ScreenPanel 的西边。但是,当我运行时,MenuPanel 的宽度太小,我尝试添加 setSize();到 MainPanel 及其所有组件,但这不起作用,有没有办法可以在 BorderLayout 中设置 MenuPanel 的大小?

菜单面板:

DefaultListModel<String> MenuModel = new DefaultListModel<>();
JList<String> MenuList = new JList<>(MenuModel);
JLabel MenuLabel = new JLabel();
MenuPanel(){
    setBackground(new Color(0,168,243));
    setLayout(new BorderLayout());

    MenuList.setBackground(new Color(0,200,250));
    MenuList.setForeground(new Color(80,80,80));
    MenuList.setFont(new Font("Arial",Font.BOLD,20));

    MenuModel.addElement("Page 1");
    MenuModel.addElement("Page 2");
    MenuModel.addElement("Page 3");
    MenuModel.addElement("Page 4");
    MenuModel.addElement("Page 5");

    MenuLabel.setText("Menu");
    MenuLabel.setFont(new Font("Arial",Font.BOLD,50));
    MenuLabel.setForeground(new Color(50,50,50));

    add(MenuList,BorderLayout.CENTER);
    add(MenuLabel,BorderLayout.NORTH);

屏幕面板:

public class ScreenPanel extends JPanel{
     ScreenPanel(){
         setLayout(new BorderLayout());
         setBackground(new Color(230,230,230));
         add(new MenuPanel(),BorderLayout.WEST);
     }
 }

框架:

 public class ScreenFrame extends JFrame{
     ScreenFrame(){
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setExtendedState(Frame.MAXIMIZED_BOTH);
         setContentPane(new ScreenPanel());
        setTitle("Random Project");
     }
 }

主要方法:

 public class Main {
     public static void main(String[] args) {
         ScreenFrame screen = new ScreenFrame();
         screen.pack();
         screen.setVisible(true);
     }
 }

期望:

现实:

【问题讨论】:

  • 你为什么要删除你最后一个问题:stackoverflow.com/questions/64718018/…?如果该建议对您有所帮助,那么它们可能会使搜索该论坛的其他人受益。
  • 其实我是 Stack Overflow 的新手,所以 stack overflow 建议我删除问题,所以我想我应该,我现在可以恢复它吗?

标签: java swing border-layout


【解决方案1】:

首先,变量名不应以大写字符开头。学习并遵循 Java 约定。

我尝试添加 setSize()

不要试图玩弄大小。布局管理器负责确定每个组件的大小和位置。

您也不应该尝试“设置”组件的首选大小。所有 Swing 组件都根据组件的属性确定自己的首选大小。

您可以通过更改组件的属性来影响组件的首选尺寸计算。

对于您可以使用的所有组件:

  1. setFont(..) - 让文字变大
  2. setBorder( new EmptyBorder(...) ) - 在 4 个边框周围留出额外空间。可以为单个组件设置边框,也可以在 JPanel 上为添加到面板的所有组件设置边框。

另外,对于JList,你可以使用:

  1. setPrototypeCellValue( "Page 0000" ) - 根据您指定的文本计算更大的首选宽度

阅读 API,您可能会发现可以更改的其他属性。

【讨论】:

  • 你能告诉我我们在 Component.setBorder.(new EmotyBorder(...?....)) 中需要什么参数;
  • @meetprakashshah,阅读 API !!!不使用 API 就无法编程。此外,您的问题是关于设置组件的首选大小。这通过显示您如何执行此操作的各种选项来回答该问题。无论您决定使用 JButtons 的 JList,此答案都适用,因此实际上它回答了您提出的问题。为什么它没有被“接受”。
  • 嗯,你的回答很完美@camickr,我没有任何问题,事实上,我真的很感谢你到目前为止的所有帮助
  • @meetprakashshah 你的答案是完美的 - 在你提出问题 15 分钟后得到了回答,但即使它直接回答了你也没有“接受”这个答案你的问题。另一个答案与调整组件的首选大小无关。
  • 好吧,在看到你的回答后,我无法弄清楚我应该做什么,所以我需要更多的澄清,所以我在 cmets 中问了你。我不知道你为什么说我不接受你的回答,我已经打勾了。
【解决方案2】:

JList 不是您正在执行的操作的正确控件。 JList 用于可变数量的数据项,并且总是应该放置在 JScrollPane 中。

您可能需要一组 JButton,它们放置在使用 GridLayout 配置为只有一列的 JPanel 中。 GridLayout 将强制其所有子级足够大以填充其相应的容器。

MenuPanel() {
    setBackground(new Color(0,168,243));
    setLayout(new BorderLayout());

    JPanel menuList = new JPanel(new GridLayout(0, 1));

    menuList.setBackground(new Color(0,200,250));
    menuList.setForeground(new Color(80,80,80));

    Font menuFont = new Font("Arial",Font.BOLD,20);

    JButton menuButton;

    menuButton = new JButton("Page 1");
    menuButton.setHorizontalAlignment(SwingConstants.LEADING);
    menuButton.setFont(menuFont);
    menuButton.setBorderPainted(false);
    menuButton.setContentAreaFilled(false);
    menuList.add(menuButton);

    menuButton = new JButton("Page 2");
    menuButton.setHorizontalAlignment(SwingConstants.LEADING);
    menuButton.setFont(menuFont);
    menuButton.setBorderPainted(false);
    menuButton.setContentAreaFilled(false);
    menuList.add(menuButton);

    menuButton = new JButton("Page 3");
    menuButton.setHorizontalAlignment(SwingConstants.LEADING);
    menuButton.setFont(menuFont);
    menuButton.setBorderPainted(false);
    menuButton.setContentAreaFilled(false);
    menuList.add(menuButton);

    menuButton = new JButton("Page 4");
    menuButton.setHorizontalAlignment(SwingConstants.LEADING);
    menuButton.setFont(menuFont);
    menuButton.setBorderPainted(false);
    menuButton.setContentAreaFilled(false);
    menuList.add(menuButton);

    menuButton = new JButton("Page 5");
    menuButton.setHorizontalAlignment(SwingConstants.LEADING);
    menuButton.setFont(menuFont);
    menuButton.setBorderPainted(false);
    menuButton.setContentAreaFilled(false);
    menuList.add(menuButton);

    JLabel menuLabel = new JLabel();
    menuLabel.setText("Menu");
    menuLabel.setHorizontalAlignment(SwingConstants.LEADING);
    menuLabel.setFont(new Font("Arial",Font.BOLD,50));
    menuLabel.setForeground(new Color(50,50,50));
    menuLabel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 48));

    add(menuList, BorderLayout.CENTER);
    add(menuLabel, BorderLayout.NORTH);
}

当然,使用循环可以让上面的代码短很多:

MenuPanel() {
    setBackground(new Color(0,168,243));
    setLayout(new BorderLayout());

    JPanel menuList = new JPanel(new GridLayout(0, 1));

    menuList.setBackground(new Color(0,200,250));
    menuList.setForeground(new Color(80,80,80));

    Font menuFont = new Font("Arial",Font.BOLD,20);

    String[] pageLabels = {
        "Page 1", "Page 2", "Page 3", "Page 4", "Page 5",
    };

    JButton menuButton;

    for (String pageLabel : pageLabels) {
        menuButton = new JButton(pageLabel);
        menuButton.setHorizontalAlignment(SwingConstants.LEADING);
        menuButton.setFont(menuFont);
        menuButton.setBorderPainted(false);
        menuButton.setContentAreaFilled(false);
        menuList.add(menuButton);
    }

    JLabel menuLabel = new JLabel();
    menuLabel.setText("Menu");
    menuLabel.setHorizontalAlignment(SwingConstants.LEADING);
    menuLabel.setFont(new Font("Arial",Font.BOLD,50));
    menuLabel.setForeground(new Color(50,50,50));
    menuLabel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 48));

    add(menuList, BorderLayout.CENTER);
    add(menuLabel, BorderLayout.NORTH);
}

如果您的按钮标签确实是第 1 页到第 5 页,则可以只使用数字循环:

MenuPanel() {
    setBackground(new Color(0,168,243));
    setLayout(new BorderLayout());

    JPanel menuList = new JPanel(new GridLayout(0, 1));

    menuList.setBackground(new Color(0,200,250));
    menuList.setForeground(new Color(80,80,80));

    Font menuFont = new Font("Arial",Font.BOLD,20);

    JButton menuButton;

    for (int i = 1; i <= 5; i++) {
        menuButton = new JButton("Page " + i);
        menuButton.setHorizontalAlignment(SwingConstants.LEADING);
        menuButton.setFont(menuFont);
        menuButton.setBorderPainted(false);
        menuButton.setContentAreaFilled(false);
        menuList.add(menuButton);
    }

    JLabel menuLabel = new JLabel();
    menuLabel.setText("Menu");
    menuLabel.setHorizontalAlignment(SwingConstants.LEADING);
    menuLabel.setFont(new Font("Arial",Font.BOLD,50));
    menuLabel.setForeground(new Color(50,50,50));
    menuLabel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 48));

    add(menuList, BorderLayout.CENTER);
    add(menuLabel, BorderLayout.NORTH);
}

请注意,在 Java 中,变量名称始终以小写字母开头(除非它们是静态最终字段,在这种情况下,名称应为 ALL_CAPS)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2011-01-15
    • 2011-05-07
    • 1970-01-01
    • 2020-08-11
    • 2016-08-10
    相关资源
    最近更新 更多