如标题中所述,我需要将文本框的标签移到框的上方而不是侧面。附上我的意思是我的头像。 what i have vs what 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);


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

10-06 03:19