【问题标题】:How do I right align the check box in my Java SWT application如何右对齐 Java SWT 应用程序中的复选框
【发布时间】:2015-11-06 20:59:29
【问题描述】:

如何在Java SWT 应用程序中右对齐复选框?这是我的代码 sn-p:

    Button checkFullECR = new Button(scrolledForm.getBody(), SWT.CHECK);
    checkFullECR.setAlignment(SWT.RIGHT);
    checkFullECR.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
    checkFullECR.setText("I need the complete ECR/ECN Process:");

如下所示:

[ ] I need the complete ECR/ECN Process:

我希望它看起来像:

I need the complete ECR/ECN Process:       [ ]

【问题讨论】:

    标签: java checkbox alignment swt


    【解决方案1】:

    您不能只使用Button 控件来执行此操作,您需要使用Label 作为文本并使用复选框Button,两列中没有文本。

    类似:

    Composite body = new Composite(parent, SWT.NONE);
    
    body.setLayout(new GridLayout(2, false));
    
    Label label = new Label(body, SWT.LEFT);
    label.setText("I need the complete ECR/ECN Process:");
    
    Button checkFullECR = new Button(body, SWT.CHECK);
    
    ... more label / button pairs ....
    

    【讨论】:

      【解决方案2】:

      这一次我不得不不同意格雷格的观点。

      您可以使用ButtonsetOrientation(int) 方法强制从右到左布局:

      public static void main(String[] args)
      {
          Display display = new Display();
          final Shell shell = new Shell(display);
          shell.setLayout(new GridLayout());
      
          Button button = new Button(shell, SWT.CHECK);
          button.setText("TEST");
      
          button = new Button(shell, SWT.CHECK);
          button.setOrientation(SWT.RIGHT_TO_LEFT);
          button.setText("TEST");
      
          shell.pack();
          shell.open();
      
          while (!shell.isDisposed()) {
              if (!display.readAndDispatch())
              {
                  display.sleep();
              }
          }
      
          display.dispose();
      }
      

      看起来像这样:

      在 Linux Mint 和 Windows 上测试。不适用于 Mac。

      【讨论】:

      • 从无答案转换:button.setOrientation() 方法在 Eclipse 3.6.2 中不可用...使用了按钮-标签组合
      • @Baz 很好,但是在编写 I18N 应用程序时,SWT.RIGHT_TO_LEFT 用于特定于区域设置的格式。在切换到从右到左的语言(区域设置)时,必须专门排除复选框来切换文本方向。
      猜你喜欢
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 2017-04-12
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      相关资源
      最近更新 更多