将两个 JLabel 放在彼此上方,并在它们旁边放置 JTextfields
Place two JLabels above each other with JTextfields next to them
我有这个代码:
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.add(label, BorderLayout.LINE_START);
panel.add(label2, BorderLayout.LINE_START);
panel.add(textfield, BorderLayout.LINE_END);
panel.add(textfield2, BorderLayout.LINE_END);
panel.add(button5);
panel.revalidate();
panel.repaint();
label.setText("Geef de basis van de driehoek in cm: ");
label2.setText("Geef de hoogte van de driehoek in cm: ");
}
});
对应于此屏幕截图:
我希望它看起来像这样:
我是 Java 的新手,我知道这可能是个愚蠢的问题,但我无法通过 Internet 上的信息弄清楚。
提前致谢!
你正在使用 BorderLayout 为此你必须使用 GridBagLayout。
更多:https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
查看 visual guide to layout managers。您可能正在寻找的是 GridBagLayout。以下可能是您想要的内容:
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.setLayout(new GridBagLayout()) // This should be set earlier
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
panel.add(label, c);
c.gridx = 0;
c.gridy = 2;
panel.add(label2, c);
c.gridx = 1;
c.gridy = 1;
panel.add(textfield, c);
c.gridx = 1;
c.gridy = 2;
panel.add(textfield2, c);
c.gridx = 2;
c.gridy = 1;
panel.add(button5, c);
panel.revalidate();
panel.repaint();
label.setText("Geef de basis van de driehoek in cm: ");
label2.setText("Geef de hoogte van de driehoek in cm: ");
}
});
查看您提供的代码,然后我发现此方法中未添加前三个按钮,所以我想它们已经存在了:
在那种情况下,让我们将您的 GUI 分解成位,如下所示:
我们现在可以让这三个剩余的 rechtangles。这些矩形将是 JPanels
,每个矩形中都有相应的组件:
所以:
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//panel with label and textield
JPanel base = new JPanel(new BorderLayout());
base.add(label, Borderlayout.LINE_START);
base.add(textfield, BorderLayout.LINE_END)
panel.add(base, BorderLayout.PAGE_START);//top of the page this may interfere with your three buttons I don't know where you are in your layout structure
//panel with label2 and textfield2
JPanel height = new JPanel(new BorderLayout());
height.add(label2, BorderLayout.LINE_START);
height.add(textfield2, BorderLayout.LINE_END);
panel.add(height, BorderLayout.CENTER);
//button5 doesn't need a panel of it's own as it's only one component
panel.add(button5, BorderLayout.PAGE_END);
panel.revalidate();
panel.repaint();
label.setText("Geef de basis van de driehoek in cm: ");
label2.setText("Geef de hoogte van de driehoek in cm: ");
}
});
这应该可以解决您的问题,如果您对答案有任何疑问,请告诉我。
希望对您有所帮助:)
编辑#1:
代码版本 2:
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel content = new JPanel(new BorderLayout());
//panel with label and textield
JPanel base = new JPanel(new BorderLayout());
base.add(label, Borderlayout.LINE_START);
base.add(textfield, BorderLayout.LINE_END)
content.add(base, BorderLayout.PAGE_START
//panel with label2 and textfield2
JPanel height = new JPanel(new BorderLayout());
height.add(label2, BorderLayout.LINE_START);
height.add(textfield2, BorderLayout.LINE_END);
content.add(height, BorderLayout.CENTER);
JPanel forbutton5 = new JPanel();
forbutton5.add(button5);
content.add(forbutton5, BorderLayout.PAGE_END);
panel.add(content);
panel.revalidate();
panel.repaint();
label.setText("Geef de basis van de driehoek in cm: ");
label2.setText("Geef de hoogte van de driehoek in cm: ");
}
});
添加到 Roan 的回答中:
我不确定您使用的是什么布局管理器。 Panel 有一个 FlowLayout 作为默认布局管理器,它将所有组件放在 1 行上。我认为你必须使用 GridLayout 或 GridbagLayout,然后你的组件将显示在另一个之上
我有这个代码:
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.add(label, BorderLayout.LINE_START);
panel.add(label2, BorderLayout.LINE_START);
panel.add(textfield, BorderLayout.LINE_END);
panel.add(textfield2, BorderLayout.LINE_END);
panel.add(button5);
panel.revalidate();
panel.repaint();
label.setText("Geef de basis van de driehoek in cm: ");
label2.setText("Geef de hoogte van de driehoek in cm: ");
}
});
对应于此屏幕截图:
我希望它看起来像这样:
我是 Java 的新手,我知道这可能是个愚蠢的问题,但我无法通过 Internet 上的信息弄清楚。
提前致谢!
你正在使用 BorderLayout 为此你必须使用 GridBagLayout。
更多:https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
查看 visual guide to layout managers。您可能正在寻找的是 GridBagLayout。以下可能是您想要的内容:
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.setLayout(new GridBagLayout()) // This should be set earlier
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
panel.add(label, c);
c.gridx = 0;
c.gridy = 2;
panel.add(label2, c);
c.gridx = 1;
c.gridy = 1;
panel.add(textfield, c);
c.gridx = 1;
c.gridy = 2;
panel.add(textfield2, c);
c.gridx = 2;
c.gridy = 1;
panel.add(button5, c);
panel.revalidate();
panel.repaint();
label.setText("Geef de basis van de driehoek in cm: ");
label2.setText("Geef de hoogte van de driehoek in cm: ");
}
});
查看您提供的代码,然后我发现此方法中未添加前三个按钮,所以我想它们已经存在了:
在那种情况下,让我们将您的 GUI 分解成位,如下所示:
我们现在可以让这三个剩余的 rechtangles。这些矩形将是 JPanels
,每个矩形中都有相应的组件:
所以:
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//panel with label and textield
JPanel base = new JPanel(new BorderLayout());
base.add(label, Borderlayout.LINE_START);
base.add(textfield, BorderLayout.LINE_END)
panel.add(base, BorderLayout.PAGE_START);//top of the page this may interfere with your three buttons I don't know where you are in your layout structure
//panel with label2 and textfield2
JPanel height = new JPanel(new BorderLayout());
height.add(label2, BorderLayout.LINE_START);
height.add(textfield2, BorderLayout.LINE_END);
panel.add(height, BorderLayout.CENTER);
//button5 doesn't need a panel of it's own as it's only one component
panel.add(button5, BorderLayout.PAGE_END);
panel.revalidate();
panel.repaint();
label.setText("Geef de basis van de driehoek in cm: ");
label2.setText("Geef de hoogte van de driehoek in cm: ");
}
});
这应该可以解决您的问题,如果您对答案有任何疑问,请告诉我。
希望对您有所帮助:)
编辑#1:
代码版本 2:
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel content = new JPanel(new BorderLayout());
//panel with label and textield
JPanel base = new JPanel(new BorderLayout());
base.add(label, Borderlayout.LINE_START);
base.add(textfield, BorderLayout.LINE_END)
content.add(base, BorderLayout.PAGE_START
//panel with label2 and textfield2
JPanel height = new JPanel(new BorderLayout());
height.add(label2, BorderLayout.LINE_START);
height.add(textfield2, BorderLayout.LINE_END);
content.add(height, BorderLayout.CENTER);
JPanel forbutton5 = new JPanel();
forbutton5.add(button5);
content.add(forbutton5, BorderLayout.PAGE_END);
panel.add(content);
panel.revalidate();
panel.repaint();
label.setText("Geef de basis van de driehoek in cm: ");
label2.setText("Geef de hoogte van de driehoek in cm: ");
}
});
添加到 Roan 的回答中: 我不确定您使用的是什么布局管理器。 Panel 有一个 FlowLayout 作为默认布局管理器,它将所有组件放在 1 行上。我认为你必须使用 GridLayout 或 GridbagLayout,然后你的组件将显示在另一个之上