我想在面板中央放置一些按钮,但不起作用。这是我的代码:
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.RootPanel;
private void addStartupBox() {
VerticalPanel boxPanel = setBoxSize();
com.google.gwt.user.client.ui.Label title = createBoxTitle("Prüfungen");
// Create second panel for the content of a box
com.google.gwt.user.client.ui.VerticalPanel boxContent = new com.google.gwt.user.client.ui.VerticalPanel();
boxContent.setSpacing(10);
boxContent.setHorizontalAlignment(com.google.gwt.user.client.ui.VerticalPanel.ALIGN_CENTER);
// Create content for the box
Button but1 = new Button("A");
but1.setPixelSize(280, 25);
Button but2 = new Button("B");
but2.setPixelSize(280, 25);
Button but3 = new Button("C");
but3.setPixelSize(280, 25);
// Add buttons to boxContent
boxContent.add(but1);
boxContent.add(but2);
boxContent.add(but3);
// Add title and boxContent to boxPanel
boxPanel.add(title);
boxPanel.add(boxContent);
RootPanel.get("boxPanelContainer").add(boxPanel);
}
结果是:
here http://tinyurl.com/ycs2q4o
并且按钮左对齐。之后我就这样试试了:
boxContent.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
但按钮仍然左对齐。我的最后一个想法是使用ExtGWT垂直面板:
boxContent.setHorizontalAlign(HorizontalAlignment.CENTER);
但遗憾的是也行不通。谁能告诉我,我做错了什么?
答案 0 :(得分:3)
问题解决了....我添加了另一个面板,它是左对齐的。代码如下:
private void addStartupBox() {
VerticalPanel boxPanel = setBoxSize();
com.google.gwt.user.client.ui.Label title = createBoxTitle("Prüfungen");
boxPanel.setHorizontalAlign(HorizontalAlignment.CENTER);
// Create second panel for the content of a box
com.google.gwt.user.client.ui.VerticalPanel boxContent = new com.google.gwt.user.client.ui.VerticalPanel();
boxContent.setSpacing(10);
// Create third panel, which contains the label
VerticalPanel labelPanel = new VerticalPanel();
labelPanel.setHorizontalAlign(HorizontalAlignment.LEFT);
// Create content for the box
Button but1 = new Button("A");
but1.setPixelSize(280, 25);
Button but2 = new Button("B");
but2.setPixelSize(280, 25);
Button but3 = new Button("C");
but3.setPixelSize(280, 25);
// Add buttons to boxContent
boxContent.add(but1);
boxContent.add(but2);
boxContent.add(but3);
// Add title to third panel
labelPanel.add(title);
// Add title and boxContent to boxPanel
boxPanel.add(labelPanel);
boxPanel.add(boxContent);
RootPanel.get("boxPanelContainer").add(boxPanel);
}
答案 1 :(得分:2)
添加以下代码并尝试
boxContent.setCellHorizontalAlignment(but1, HasHorizontalAlignment.ALIGN_CENTER);
boxContent.setCellHorizontalAlignment(but2, HasHorizontalAlignment.ALIGN_CENTER);
boxContent.setCellHorizontalAlignment(but3, HasHorizontalAlignment.ALIGN_CENTER);
上面的代码应该在按钮添加到框内容之后添加(在“RootPanel.get(”boxPanelContainer“)之前.add(boxPanel);”此行)。