[英]How to align Buttons
Buttons are lying horizontally one after the other.按钮一个接一个地水平放置。 I need to know how to make them appear on the screen vertically.我需要知道如何让它们垂直出现在屏幕上。 I tried adding SwingConstants.xxxx.我尝试添加 SwingConstants.xxxx。 Then item.SwingConstants.xxx got the horizontal alignment error.然后 item.SwingConstants.xxx 得到了水平对齐错误。 Any help is appreciated.任何帮助表示赞赏。 (want to stick to this style of program) (想坚持这种风格的节目)
import java.awt. * ;
import java.awt.event. * ;
import javax.swing. * ;
@SuppressWarnings("serial")
public class Class_GUI_4 extends JFrame {
private JTextField tf; // textfield
private JCheckBox b1; // button 1
private JCheckBox b2; // button2
public Class_GUI_4() {
super("TITLE");
setLayout(new FlowLayout());
tf = new JTextField("A testing Sentence", 20);
tf.setFont(new Font("Serif", Font.PLAIN, 14));
add(tf);
b1 = new JCheckBox("Bold");
b2 = new JCheckBox("Italic");
add(b1);
add(b2);
thehandler handler = new thehandler();
b1.addItemListener(handler);
b2.addItemListener(handler);
}
private class thehandler implements ItemListener {
@Override
public void itemStateChanged(ItemEvent event) {
Font font = null;
if (b1.isSelected() && b2.isSelected()) font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
else if (b1.isSelected()) font = new Font("Serif", Font.BOLD, 14);
else if (b2.isSelected()) font = new Font("Serif", Font.ITALIC, 14);
else font = new Font("Serif", Font.PLAIN, 14);
tf.setFont(font);
}
}
public static void main(String[] args) {
Class_GUI_4 gui = new Class_GUI_4();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(200, 350);
gui.setVisible(true);
}
}
how to make them come on screen vertically?如何让它们垂直出现在屏幕上?
Try with BoxLayout尝试使用BoxLayout
setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
instead of FlowLayout in this case that arranges components in a directional flow, much like lines of text in a paragraph.而不是FlowLayout在这种情况下以定向流排列组件,就像段落中的文本行一样。
setLayout(new FlowLayout());
It's worth reading about How to Use BoxLayout关于 如何使用 BoxLayout值得一读
Don't use GridLayout
in this case that adds the components in equal size as shown below:在这种情况下,不要使用GridLayout
以相同大小添加组件,如下所示:
Instead you can try with GridBagLayout .相反,您可以尝试使用GridBagLayout 。 Read more about How to Use GridBagLayout阅读有关如何使用 GridBagLayout 的更多信息
Sample code: (Change the height as per your requirement in percentage)示例代码:(根据您的要求以百分比更改高度)
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.BOTH;
//Set the margin between components top, left, bottom, right
//gc.insets=new Insets(5, 5, 5, 5);
tf = new JTextField("A testing Sentence", 20);
tf.setFont(new Font("Serif", Font.PLAIN, 14));
gc.gridx = 0; // 1st row
gc.gridy = 0; // 1st column
gc.weighty = 0.9; // 90%
add(tf, gc);
b1 = new JCheckBox("Bold");
b2 = new JCheckBox("Italic");
gc.gridy = 1; // 2nd column
gc.weighty = 0.05;// 5%
add(b1, gc);
gc.gridy = 2; // 3rd column
gc.weighty = 0.05;// 5%
add(b2, gc);
This is because the layout manager you are using is FlowLayout().这是因为您使用的布局管理器是 FlowLayout()。
setLayout(new FlowLayout());
Use GridLayout manager instead by replacing the code with:使用 GridLayout 管理器,将代码替换为:
setLayout(new GridLayout(1,3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.