首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >垂直延伸面板内的面板

垂直延伸面板内的面板
EN

Stack Overflow用户
提问于 2018-05-16 03:24:14
回答 1查看 68关注 0票数 0

我正在使用Java中的Swing GUI小部件开发一个应用程序,并且遇到了在面板中垂直拉伸面板的问题。我的主面板使用的是BorderLayout,问题出在中心区域。我正在使用一个带有BoxLayout.X_AXIS的面板,在它里面插入了带有BoxLayout.Y_AXIS的附加面板。我不能从上到下垂直拉伸它。我还希望内容从上到下开始。我已经尝试过使用GridLayout,它做的正是我想要的,但是我受到宽度方面的限制,因为所有列的大小都是相等的,并且我希望一些面板的宽度更小。我已经查看了API、教程,并在谷歌搜索中找到了答案。任何帮助都是最好的!

附件是代码和屏幕截图与布局。

代码语言:javascript
复制
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.*;
import java.awt.Color;

public class Colors
{
private JFrame frame;
private JPanel contentPane;

public static void main (String[] args){
    Colors gui = new Colors();
    gui.start();
}

public void start(){
    frame = new JFrame("Words");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    contentPane = (JPanel)frame.getContentPane();
    contentPane.setLayout(new BorderLayout(8,8));
    center();
    east();
    frame.pack();
    frame.setVisible(true);
}

private void center(){
    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(new BoxLayout(centerPanel,BoxLayout.X_AXIS));
    //centerPanel.setLayout(new GridLayout(1,3));
    centerPanel.setPreferredSize(new Dimension(300,0));
    centerPanel.setBackground(Color.BLUE);

    JLabel oneLabel,twoLabel,threeLabel;

    JPanel panelOne = new JPanel();
    panelOne.setLayout(new BoxLayout(panelOne,BoxLayout.Y_AXIS));
    panelOne.setBackground(Color.CYAN);
    oneLabel = new JLabel("  First Label First Column  ");
    panelOne.add(oneLabel);
    twoLabel = new JLabel("  Second Label First Column  ");
    panelOne.add(twoLabel);
    threeLabel = new JLabel("  Third Label First Column  ");
    panelOne.add(threeLabel);
    centerPanel.add(panelOne);

    JPanel panelTwo = new JPanel();
    panelTwo.setLayout(new BoxLayout(panelTwo,BoxLayout.Y_AXIS));
    panelTwo.setBackground(Color.YELLOW);
    oneLabel = new JLabel("  10  ");
    panelTwo.add(oneLabel);
    twoLabel = new JLabel("  20  ");
    panelTwo.add(twoLabel);
    threeLabel = new JLabel("  30  ");
    panelTwo.add(threeLabel);
    centerPanel.add(panelTwo);

    JPanel panelThree = new JPanel();
    panelThree.setLayout(new BoxLayout(panelThree,BoxLayout.Y_AXIS));
    panelThree.setBackground(Color.GREEN);
    oneLabel = new JLabel("  10  ");
    panelThree.add(oneLabel);
    twoLabel = new JLabel("  20  ");
    panelThree.add(twoLabel);
    threeLabel = new JLabel("  30  ");
    panelThree.add(threeLabel);
    centerPanel.add(panelThree);

    contentPane.add(centerPanel, BorderLayout.CENTER);
}

private void east(){
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    panel.setPreferredSize(new Dimension(100,150));
    panel.setBackground(Color.RED);

    JPanel labelPanel = new JPanel();
    labelPanel.setBackground(Color.WHITE);
    labelPanel.setMaximumSize(new Dimension(26,24));
    panel.add(labelPanel);

    labelPanel = new JPanel();
    labelPanel.setBackground(Color.GREEN);
    labelPanel.setMaximumSize(new Dimension(26,24));
    panel.add(labelPanel);

    contentPane.add(panel, BorderLayout.EAST);
}      
}
EN

回答 1

Stack Overflow用户

发布于 2018-05-16 04:02:21

我已经尝试过使用GridLayout,它做的正是我想要的,但是我受到宽度的限制,因为所有列的大小都是相等的,并且我希望一些面板的宽度更小

您可以尝试使用GridBagLayout。每列将是列中最大组件的宽度。

有关更多信息和工作示例,请阅读How to Use GridBagLayout上的Swing教程中的部分。

另一种选择可能是使用JTable。JTable允许您以行/列格式显示数据。上面的教程也有一个关于How to Use Tables的部分。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50357790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档