gpt4 book ai didi

java - 将文本框的标签放置在文本框上方而不是侧面

转载 作者:行者123 更新时间:2023-12-01 21:53:11 25 4
gpt4 key购买 nike

如标题所述,我需要将文本框的标签移动到框上方而不是侧面。附上我的一张图片来表达我的意思。 what i havewhat i want我尝试过搜索它,但我似乎找不到我正在寻找的答案/不太确定要查找什么。我尝试过使用 JFrame,但它创建了一个单独的窗口,除非我需要将整个 GUI 制作为 JFrame 以获得我想要的结果?

actionPerformed 方法也有一些东西,但它与问题无关,但仍然显示正确。

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 。您还可以使用允许垂直方向的其他布局。来访the visual guide to Layout Managers将帮助您发现它们。

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);

image

关于java - 将文本框的标签放置在文本框上方而不是侧面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58755083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com