gpt4 book ai didi

java - 如何设置TableLayout的单元格垂直和水平对齐方式

转载 作者:行者123 更新时间:2023-11-30 08:13:49 25 4
gpt4 key购买 nike

有没有办法在 TableLayout 的单元格中设置对齐方式

如果我想将单元格中的组件设置为例如右下角?

最佳答案

这是使用 TableLayoutConstraints 的示例:

import javax.swing.JButton;
import javax.swing.JFrame;
import layout.TableLayout;
import layout.TableLayoutConstraints;

public class TableLayoutCellAlignmentExample {

public static void main(String args[]) {

JFrame frame = new JFrame("Example of TableLayout");
frame.setSize(700, 400);

double size[][] = {{200,200,200}, // Columns
{100,100,100}}; // Rows

frame.setLayout(new TableLayout(size));


String[] labels = {
"should be TOP LEFT",
"should be TOP CENTER",
"should be TOP RIGHT",
"should be MIDDLE LEFT",
"should be MIDDLE CENTER",
"should be MIDDLE RIGHT",
"should be BOTTOM LEFT",
"should be BOTTOM CENTER",
"should be BOTTOM RIGHT",
};

frame.add(new JButton(labels[0]), new TableLayoutConstraints(0, 0, 0, 0, TableLayout.LEFT, TableLayout.TOP));
frame.add(new JButton(labels[1]), new TableLayoutConstraints(1, 0, 1, 0, TableLayout.CENTER, TableLayout.TOP));
frame.add(new JButton(labels[2]), new TableLayoutConstraints(2, 0, 2, 0, TableLayout.RIGHT, TableLayout.TOP));
frame.add(new JButton(labels[3]), new TableLayoutConstraints(0, 1, 0, 1, TableLayout.LEFT, TableLayout.CENTER));
frame.add(new JButton(labels[4]), new TableLayoutConstraints(1, 1, 1, 1, TableLayout.CENTER, TableLayout.CENTER));
frame.add(new JButton(labels[5]), new TableLayoutConstraints(2, 1, 2, 1, TableLayout.RIGHT, TableLayout.CENTER));
frame.add(new JButton(labels[6]), new TableLayoutConstraints(0, 2, 0, 2, TableLayout.LEFT, TableLayout.BOTTOM));
frame.add(new JButton(labels[7]), new TableLayoutConstraints(1, 2, 1, 2, TableLayout.CENTER, TableLayout.BOTTOM));
frame.add(new JButton(labels[8]), new TableLayoutConstraints(2, 2, 2, 2, TableLayout.RIGHT, TableLayout.BOTTOM));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
}
}

结果: enter image description here

关于java - 如何设置TableLayout的单元格垂直和水平对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29949389/

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