【问题标题】:center vertically jbutton inside jpanel -javajpanel -java中的垂直居中jbutton
【发布时间】:2011-06-18 04:28:42
【问题描述】:

我的边框布局有 4 个面板,北、南、东、西。例如,在东边,我有一个 jpanel,它有四个按钮。我的问题是所有按钮都对齐到顶部,我想对齐到中心。在 CSS 中,例如 margin top 或 top:50%。

有什么想法吗?

    JPanel contentor3 = new JPanel();
  contentor.add(contentor3, BorderLayout.EAST);
  contentor3.setBackground(Color.green);
  contentor3.setPreferredSize(new Dimension(120, 750));

  contentor3.add(btn[1]);
  btn[1].setText("btn1");

【问题讨论】:

    标签: java swing center jbutton


    【解决方案1】:

    您需要更改contentor3 的布局。尝试类似:

    contentor3.setLayout (new GridBagLayout());
    
    GridBagConstraints gbc = new GridBagConstraints ();
    
    // next two lines will place the components top-to-bottom, rather than left-to-right
    gbc.gridx = 0;
    gbc.gridy = GridBagConstraints.RELATIVE;
    
    // add as many buttons as you want in a column
    contentor3.add (new JButton ("btn1"), gbc);
    
    contentor.add (contentor3, BorderLayout.EAST);
    

    【讨论】:

    • 我已经尝试过类似的东西,但是按钮并没有断线,所以他们创建了一条所有按钮和部分隐藏的线
    • 你的意思是你想要一列中的按钮,从上到下?我已经改变了我的答案来做到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2020-09-01
    • 2013-10-15
    • 2012-12-29
    • 2011-07-07
    • 2014-02-27
    相关资源
    最近更新 更多