English 中文(简体)
如何安排与另一个构成部分有关的部分?
原标题:How to layout component relative to another component?

我想作以下阐述:

></p>

<p>(i) jbutton和jlabel被安置在jpanel,jbutton被集中,jlabel紧随其后。 退约应当能够转作。 <><>Edit>:</strong> 溶解期间,吉布顿和支拉布的面积保持不变。</p>

<p>我想到以下几点:</p>

<ol>
<li><p>使用适当的排位管理人。 我不知道。 我可以把jbutton和jlabel添加到另一个支架上,并将这一新的支架添加到大吉普勒的中心,但在这种情况下,jbutton获胜的帽子被集中起来。</p>

<p><strong>Edit:</strong> I tried to use <a rel=this proposal: (proposal was edited and code is correct now there)

public class MainFrame extends JFrame {
    public static void main(String[] args) {
        new MainFrame();
    }

public MainFrame() {
    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 1;
    add(new JPanel(), c);
    c.gridx = 1;
    c.weightx = 0;
    c.anchor = GridBagConstraints.CENTER;
    add(new JButton("Button"), c);
    c.gridx = 2;
    c.weightx = 1;
    c.anchor = GridBagConstraints.WEST;
    add(new JLabel("Label"), c);
    pack();
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
} }

但是,它产生以下产出,但纽特吨为核心:

></p></li>
<li><p>Add jbutton and jlabel to jpanel. Add <code>ComponentListener</code> to jpanel and override <code>componentResized</code> there. Overriden method will look like:  </p>

<pre><code>public void componentResized(ComponentEvent e) {
    int x = button.getX() + button.getWidth() + 3;
    int y = button.getY();
    label.setLocation(new Point(x, y));
}  
</code></pre>

<p>JButton职位将在通过这种方法自动转业经理和jlabel职位时选定。 但是,只有在完成转播后方可援引<条码>部分复读>。 因此,正在重新确定支离破碎的地位,将由预想的排外管理人员确定。</p></li>
<li>Add jbutton to content pane and jlabel to glass pane. But I ll have the same problem with resizing as in previous case.</li>
</ol>

<p>How can I achieve this layout?</p>
    </div>

           </div>
   
      <div class=

最佳回答

就像你一样,可以使用3栏的电网包布。 第一和第三列必须具有同等的权重,因此同样如此。 中列不会因此坐在中间。 我将尝试给你,但我打上“一刀!” HTH

见评论,我对你的样本代码表示怀疑:

public class MainFrame extends JFrame {
    public static void main(String[] args) {
        new MainFrame();
    }

    public MainFrame() {
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        Component panel = new JPanel();
        panel.setBackground(Color.blue);
        add(panel, c);
        c.gridx = 1;
        c.weightx = 0;
        c.anchor = GridBagConstraints.CENTER;
        add(new JButton("Button"), c);
        c.gridx = 2;
        c.weightx = 1;
        c.anchor = GridBagConstraints.WEST;
        JLabel label = new JLabel("Label");
        add(label, c);

        panel.setPreferredSize(label.getPreferredSize());

        pack();
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}
问题回答

使用支持你需要的“LayoutManager”是前进的道路。 高层管理人员可以确定(一栏/行)大小到相等的宽度/宽度的群体。 如果核心管理人员不允许这种调制性规范,则由第三方管理人负责,即:MigLayout、 FormLayout或设计GridLayout。 在学习方面投入的时间是:

在MigLayout(目前为男性个人利益),反政府分子是“集体团体Name”或“sg groupName”(如果只有集团成员,Name可以空出)。

    JButton button = new JButton("somesize");
    JLabel label = new JLabel("--outer--");
    MigLayout layout = new MigLayout("debug, wrap 3", 
           "[sizegroup, grow][center][sizegroup, grow]");
    JComponent comp = new JPanel(layout);
    // start adding component in second column 
    comp.add(button, "cell 1 0");
    comp.add(label);
    // add something spanning all column in second row for comparison
    comp.add(new JButton("somesize"), "span 3, center");




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...