Java 将文本框的标签置于文本框上方,而不是侧面
如标题所述,我需要将文本框的标签移到文本框上方,而不是移到侧面。附件我有一张我的意思的照片。vs我试着搜索它,但我似乎找不到我要找的答案/不确定要查找什么。我尝试过使用JFrame,但它提供了一个单独的窗口,除非我需要将整个GUI设置为JFrame,以获得我想要的结果 actionPerformed方法也有一些东西,但与问题无关,但仍然正确显示Java 将文本框的标签置于文本框上方,而不是侧面,java,swing,user-interface,Java,Swing,User Interface,如标题所述,我需要将文本框的标签移到文本框上方,而不是移到侧面。附件我有一张我的意思的照片。vs我试着搜索它,但我似乎找不到我要找的答案/不确定要查找什么。我尝试过使用JFrame,但它提供了一个单独的窗口,除非我需要将整个GUI设置为JFrame,以获得我想要的结果 actionPerformed方法也有一些东西,但与问题无关,但仍然正确显示 import java.awt.event.\*; import javax.swing.\*; import java.text.DecimalFor
import java.awt.event.\*;
import javax.swing.\*;
import java.text.DecimalFormat;
public class Project4 extends JFrame implements ActionListener {
private JTextArea taArea = new JTextArea("", 30, 20);
ButtonGroup group = new ButtonGroup();
JTextField name = new JTextField(20);
boolean ch = false;
boolean pep = false;
boolean sup = false;
boolean veg = false;
DecimalFormat df = new DecimalFormat("##.00");
double cost = 0.0;
public Project4() {
initUI();
}
public final void initUI() {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
getContentPane().add(panel1, "North");
getContentPane().add(panel2, "West");
getContentPane().add(panel3, "Center");
getContentPane().add(panel4, "East");
panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS));
getContentPane().add(panel5, "South");
JButton button = new JButton("Place Order");
button.addActionListener(this);
panel5.add(button);
JButton button2 = new JButton("Clear");
button2.addActionListener(this);
panel5.add(button2);
panel3.add(taArea);
JCheckBox checkBox1 = new JCheckBox("Cheese Pizza") ;
checkBox1.addActionListener(this);
panel4.add(checkBox1);
JCheckBox checkBox2 = new JCheckBox("Pepperoni Pizza");
checkBox2.addActionListener(this);
panel4.add(checkBox2);
JCheckBox checkBox3 = new JCheckBox("Supreme Pizza");
checkBox3.addActionListener(this);
panel4.add(checkBox3);
JCheckBox checkBox4 = new JCheckBox("Vegetarian Pizza");
checkBox4.addActionListener(this);
panel4.add(checkBox4);
JRadioButton radioButton1 = new JRadioButton("Pick Up");
group.add(radioButton1);
radioButton1.addActionListener(this);
panel1.add(radioButton1);
JRadioButton radioButton2 = new JRadioButton("Delivery");
group.add(radioButton2);
radioButton2.addActionListener(this);
panel1.add(radioButton2);
JLabel name_label = new JLabel("Name on Order");
name.addActionListener(this);
panel5.add(name_label);
panel5.add(name);
setSize(600, 300);
setTitle("Pizza to Order");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent action) {
}
public static void main(String[] args) {
Project4 ex = new Project4();
ex.setVisible(true);
}
}为了实现这一点,您可以将嵌套的
JPanel
与其他布局一起使用。我会选择这里的BorderLayout
。您还可以选择其他允许垂直方向的布局。参观会帮助你发现它们
JLabel name_label = new JLabel("Name on Order");
name.addActionListener(this);
JPanel verticalNestedPanel = new JPanel(new BorderLayout());
verticalNestedPanel.add(name_label, BorderLayout.PAGE_START);
verticalNestedPanel.add(name, BorderLayout.PAGE_END);
panel5.add(verticalNestedPanel);
按照您自己的示例,
panel5.setLayout(新的BoxLayout(panel5,BoxLayout.Y_轴))代码>谢谢你,我不太确定该怎么做,得到了我想要的东西