gpt4 book ai didi

Swing:在框架中放置一组组件

转载 作者:行者123 更新时间:2023-12-04 05:24:09 27 4
gpt4 key购买 nike

准确而简短:
是否可以使用一组组件(如复选框、单选按钮等)来布局框架容器,而不是将它们一个一个地添加到框架中?因此,将它们放置在框架中会容易得多。

private void initializaUI(){
setSize(700, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Panel container to wrap checkboxes and radio buttons
JPanel panel = new JPanel(null);
JPanel checkBoxPanel = new JPanel(new BoxLayout(panel, defaultCloseOperation, null));
JPanel radioPanel_1 = new JPanel(new GridLayout());
JPanel radioPanel_2 = new JPanel(new GridLayout());

//Text field to display order
JTextField orderField = new JTextField(20);
orderField.setBounds(100, 100, 100, 20);

//Button to process place the order
JButton button = new JButton("Process Selection");
button.setBounds(300, 100, 100, 40);

//toppings check boxes
checkBoxPanel.setVisible(true);
checkBoxPanel.setBounds(100, 200, 100, 50);
String Topping[] = {"Tomato", "Green Pepper", "Black Olives", "Mushrooms", "Extra Cheese", "Pepperoni", "Sausage"};
checkBoxPanel.add(new JCheckBox("Tomato"));
checkBoxPanel.add(new JCheckBox(Topping[1]));
checkBoxPanel.add(new JCheckBox(Topping[2]));
checkBoxPanel.add(new JCheckBox(Topping[3]));
checkBoxPanel.add(new JCheckBox(Topping[4]));
checkBoxPanel.add(new JCheckBox(Topping[5]));
checkBoxPanel.add(new JCheckBox(Topping[6]));
//sizes radio buttons
String size[] = {"Small:$6.50", "Medium:$8.50", "Large:$10.00"};
JRadioButton radio = new JRadioButton(size[0]);
radio.setBounds(100, 50, 100, 20);
//
panel.add(checkBoxPanel);
//
setContentPane(panel);

这是应该根据用户输入执行一些操作的代码。请帮助我使其清晰易读。
这是错误:“该字段 Component.x 不可见”

最佳答案

是的,这就是 JPanel 是为了。它是一个空容器,有内部布局管理器,可以放置在 JFrame 中的任何位置。 (或在另一个 JPanel 内)。所以,只是给你一个例子,你可以:

JPanel checkBoxPanel = new JPanel(new GridLayout(..));
JPanel fieldsPanel = new JPanel(new BoxLayout(..));

checkBoxPanel.add(new JCheckBox(..));

fieldsPanel.add(new JTextField(..));

frame.setLayout(new BorderLayout());
frame.add(checkBoxPanel, BorderLayout.NORTH);
frame.add(..)

关于Swing:在框架中放置一组组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13400444/

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