gpt4 book ai didi

java - FlowLayout.CENTER 不居中组件

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

我正在使用 FlowLayout.CENTER 将字符串和一些复选框居中,但并没有将它们居中。我以前用过这个,效果很好。这是代码:

import javax.swing.*;
import java.awt.*;

public class pizza extends JFrame {
//row 1
JPanel row1 = new JPanel();
JLabel select = new JLabel("Please select the size you would like");
//row 2
JPanel row2 = new JPanel();
JCheckBox ninein = new JCheckBox("9 inch, $5.00", false);
JCheckBox twelvein = new JCheckBox("12 inch, $10.00", false);
JCheckBox seventeenin = new JCheckBox("17 inch, $15.00", false);
//row 3
JPanel row3 = new JPanel();
JLabel toppingslab = new JLabel("Please select your toppings");
JCheckBox cheese = new JCheckBox("Cheese, .50");
JCheckBox pepperoni = new JCheckBox("Pepperoni, .50");
JCheckBox onions = new JCheckBox("Onions, .50");
JCheckBox peppers = new JCheckBox("Peppers, .50");
JCheckBox bacon = new JCheckBox("Bacon! Free (because it's bacon)");
//row4
JPanel row4 = new JPanel();
JLabel totallab = new JLabel("Total");
JTextField total = new JTextField(10);

public pizza(){
setTitle("Pizza Ordering");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
total.setEnabled(false);

FlowLayout one = new FlowLayout(FlowLayout.CENTER);
setLayout(one);

row1.add(select);
add(row1);

row2.add(ninein);
row2.add(twelvein);
row2.add(seventeenin);
add(row2);

GridLayout two = new GridLayout(6,2);
row3.setLayout(two);

row3.add(toppingslab);
row3.add(cheese);
row3.add(pepperoni);
row3.add(onions);
row3.add(peppers);
row3.add(bacon);
add(row3);

FlowLayout three = new FlowLayout(FlowLayout.RIGHT);
setLayout(three);

row4.add(totallab);
row4.add(total);
add(row4);
}
public static void main(String[] args) {
pizza pizz = new pizza();
}
}

RIGHT 或 LEFT 都没有区别,它只是将其与右侧对齐。

最佳答案

所有组件都右对齐的原因是您重置了 JFrame 的布局。至 FlowLayout.RIGHT在这里:

FlowLayout three = new FlowLayout(FlowLayout.RIGHT);
setLayout(three);

这取代了先前对居中对齐组件的调用。

关于java - FlowLayout.CENTER 不居中组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15858234/

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