我有这段代码:
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的新手,我知道这可能是一个愚蠢的问题,但我无法通过互联网上的信息弄明白。
提前致谢!
答案 0 :(得分:3)
添加到Roan的答案: 我不确定你使用的是什么布局管理器。 Panel有一个FlowLayout作为默认布局管理器,并且将所有组件放在一行上。我认为你必须使用GridLayout或GridbagLayout,然后你的组件将出现在另一个之上
答案 1 :(得分:2)
查看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: ");
}
});
答案 2 :(得分:2)
查看您提供的代码,然后我看到此方法中未添加前三个按钮,因此我认为它们已经存在:
在这种情况下,让我们将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: ");
}
});
答案 3 :(得分:1)
你正在使用BorderLayout,你必须使用GridBagLayout。
更多信息:https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html