Java 组件的网格布局动态定位
我有一个组合有5列的gridlayout,它包含 一些按钮。现在是否可以将任何其他控件添加到 GridLayout的特定位置?例如,我想添加 第三列第一行的一个文本字段,即代替按钮3 按钮3应下降到最后一行,即按钮7之后。 GridLayout中是否可以进行任何类型的动态定位Java 组件的网格布局动态定位,java,layout,swt,Java,Layout,Swt,我有一个组合有5列的gridlayout,它包含 一些按钮。现在是否可以将任何其他控件添加到 GridLayout的特定位置?例如,我想添加 第三列第一行的一个文本字段,即代替按钮3 按钮3应下降到最后一行,即按钮7之后。 GridLayout中是否可以进行任何类型的动态定位 package zrcp; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.wid
package zrcp;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class LayoutExample {
protected Shell shell;
/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
try {
LayoutExample window = new LayoutExample();
window.open();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
shell.setLayout(new GridLayout(5, false));
Button btnNewButton = new Button(shell, SWT.NONE);
btnNewButton.setText("Button1");
Button btnNewButton_1 = new Button(shell, SWT.NONE);
btnNewButton_1.setText("Button2");
Button btnNewButton_2 = new Button(shell, SWT.NONE);
btnNewButton_2.setText("Button3");
Button btnNewButton_3 = new Button(shell, SWT.NONE);
btnNewButton_3.setText("Button4");
Button btnNewButton_4 = new Button(shell, SWT.NONE);
btnNewButton_4.setText("Button5");
Button btnNewButton_5 = new Button(shell, SWT.NONE);
btnNewButton_5.setText("Button6");
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
Button btnNewButton_6 = new Button(shell, SWT.NONE);
btnNewButton_6.setText("Button7");
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
}
}
GridLayout不会立即执行此操作,但它会按照父组合返回按钮的顺序呈现按钮,因此您可以创建一个自定义组合,对按钮进行排序并将其添加到该组合中,而不是直接添加到Shell中:
class SortedComposite extends Composite {
private Comparator<Control> comparator; // TODO Create this
public SortedComposite(Composite parent, int style) {
super(parent, style);
}
@Override
public Control[] getChildren() {
Control[] children = super.getChildren();
Arrays.sort(children, comparator);
return children;
}
}
class SortedComposite扩展了复合{
private Comparator Comparator;//创建此
公共分类复合(复合父级,int样式){
超级(父母,风格);
}
@凌驾
公共控件[]getChildren(){
Control[]children=super.getChildren();
数组。排序(子级、比较级);
返回儿童;
}
}
按钮上的setData
方法将提供一种很好的方式将数据附加到按钮上,比较器可以使用该方法对其进行排序