如何为多个 Jbutton 编写动作侦听器

How to write an action listener for multiple Jbuttons

    //Created a composite with label and text field
    Label label = new Label(composite, SWT.NONE);
    label.setText("Test:");        
    labelTxt = new Text(composite, SWT.BORDER);
    labelTxt.setText("56");         //value here might be empty or might be already set with 
                                  // any value from demo image
    
    labelTxt.addMouseListener(new MouseListener() {
        @Override
        public void mouseUp(MouseEvent arg0) {
        }

        @Override
        public void mouseDown(MouseEvent arg0) {
        }

        @Override
        public void mouseDoubleClick(MouseEvent arg0) {
            System.out.println(" mouse double clicked");
            // I created a pop up on double click with buttons here. 
            ///The pop up image is attached for reference
        }
    });  

演示图片:

现在我需要的是,如果我单击弹出窗口中的任何按钮,按钮值应保存到“labelTxt”。 也就是说,如果我点击按钮 R6,那么 R6 应该保存在 labelTxt 中。

此代码实现您在 SWT 中显示的弹出窗口并设置 labelTxt 字段。由于都是 SWT,因此 AWT/SWT 交互没有问题。它假定 labelTxt 是包含 class.

中的一个字段

该代码使用 JFace GridDataFactoryGridLayoutFactory,但如果需要可以将其转换为 SWT。它还使用 var,因此需要 Java 10 或更高版本(同样可以更改为 Java 8)。

private void openPopup(final Shell parentShell)
{
  final var shell = new Shell(parentShell, SWT.ON_TOP | SWT.TOOL);

  shell.setLayout(new GridLayout());

  // Composite for the main set of buttons

  final var body = new Composite(shell, SWT.NONE);

  GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, false).applyTo(body);

  GridLayoutFactory.swtDefaults().numColumns(4).equalWidth(true).margins(15, 5).spacing(1, 1).applyTo(body);

  new Label(body, SWT.LEAD);
  addButton(body, "RR");
  addButton(body, "3");
  new Label(body, SWT.LEAD);

  addButton(body, "7");
  addButton(body, "5");
  addButton(body, "R6");
  addButton(body, "1");

  addButton(body, "74");
  addButton(body, "we");
  addButton(body, "FL");
  addButton(body, "23");

  addButton(body, "R4");
  addButton(body, "R3");
  addButton(body, "R2");
  addButton(body, "R1");

  // Horizontal spacer

  final var spacer1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
  GridDataFactory.fillDefaults().applyTo(spacer1);

  // Right aligned close button

  final var closeButton = new Button(shell, SWT.PUSH);
  closeButton.setText("Cancel");

  closeButton.addListener(SWT.Selection, event -> shell.close());

  GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).grab(true, false).applyTo(closeButton);

  // Layout and open shell

  shell.layout();
  shell.pack();

  shell.open();

  // Block until closed

  final var display  = shell.getDisplay();

  while (!shell.isDisposed())
   {
     if (!display.readAndDispatch())
       display.sleep();
   }

  display.update();
}


private static void addButton(final Composite body, final String text)
{
  final var button = new Button(body, SWT.PUSH | SWT.FLAT);   // Flat style is optional here

  button.setText(text);

  GridDataFactory.fillDefaults().hint(75, 40).applyTo(button); // Adjust hints to change button size to suit

  button.addListener(SWT.Selection, event ->
    {
      labelTxt.setText(text);

      button.getShell().close();
    });
}