Java SWT:正确使用小部件(按钮、标签) [英] Java SWT: Widgets(buttons, labels) used properly

查看:38
本文介绍了Java SWT:正确使用小部件(按钮、标签)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这里的第一个问题,但我真的被困住了.也许这只是我最近几天极度疲劳,但我现在用谷歌搜索了几个小时,找不到任何接近好的答案.

It's my first question here but I'm really stuck. Maybe it's just my extreme fatigue for last few days, but I looked up google for few hours now and couldn't find any close to good answer.

我知道 SWT 是事件驱动的,就像我能想到的所有 GUI 一样,在创建小部件时,我应该记住,它们需要能够访问它们应该修改/交互的部件.但我需要知道的是我的想法是否正确,如果不正确,我应该改进什么.

I know SWT is event driven like all GUI's I can think of and when creating widgets I should keep in mind that they need to be able to reach the ones they are supposed to modify/interact with. But what I need to know is if my thinking is right and if not what should I improve.

假设我从 eclipse+windowbuilder+swt/jface java 项目开始.然后我为button(SelectionListener)添加button和clabel +click监听器,所以生成的代码看起来或多或少像(只有main方法,上面只有Main类和imports)

Let's say I start with eclipse+windowbuilder+swt/jface java project. Then I add button and clabel +click listener for button(SelectionListener), so the generated code looks more or less like(only main method, above there is only Main class and imports)

public static void main(String[] args) {
    Display display = Display.getDefault();
    Shell shell = new Shell();
    shell.setSize(450, 300);
    shell.setText("SWT Application");

    Button btnNewButton = new Button(shell, SWT.NONE);
    btnNewButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
        }
    });
    btnNewButton.setBounds(51, 31, 75, 25);
    btnNewButton.setText("New Button");

    CLabel lblOneTwo = new CLabel(shell, SWT.NONE);
    lblOneTwo.setBounds(180, 119, 61, 21);
    lblOneTwo.setText("one two");

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

据我所知,对你们大多数人来说可能很明显,我不能只是去补充

As I know and it's probably obvious to most of you I can't just go and add to

btnNewButton.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
    }
});

lblOneTwo.setText("three") 之类的代码,根据我的了解和使用,有时我只是事先声明所有东西,作为 static widget widgetName 然后我可以基本上可以从任何地方访问它们,因此代码如下

code like lblOneTwo.setText("three") and from what I know and use sometimes I just declare all stuff beforehand, as static widget widgetName and then I can access them from basically everywhere, so code like

static Button btnNewButton;
static CLabel lblOneTwo;

public static void main(String[] args) {
    Display display = Display.getDefault();
    Shell shell = new Shell();
    shell.setSize(450, 300);
    shell.setText("SWT Application");

    btnNewButton = new Button(shell, SWT.NONE);
    btnNewButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            lblOneTwo.setText("three");
        }
    });
    (...)

工作正常.但我猜想这不是最好的做法和做法,不是吗?所以请帮助我,为我指明正确的方向,这样我就可以停止编码.提前致谢!带有文章/教程链接的间接答案会很棒,但我喜欢人们放在这里的例子,因为他们展示事物的方式很清晰.

works just fine. But I guess and think it's not the best practice and way of doing it, isn't it? So please, help me, point me in the right direction so I can stop coding in a sin. Thanks in advance! indirect answers with links to articles/tutorials would be great, but I like examples people are putting here because of their clear way of showing things.

推荐答案

有多种方法可以做到这一点.

There are multiple ways to do this.

  1. 制作您班级的小部件字段.除非必要,否则不要将它们设为 static.

private Label l;
private Button b;
public static void main(String[] args)
{
    ...
    b.addListener(...);

  • 首先定义所有小部件(如果您想在 Listener 中使用它们,则必须是 final),然后添加您的 Listeners.

  • Define all your widgets first (have to be final if you want to use them in the Listener), after that add your Listeners.

    final Label l = ...;
    final Button b = ....;
    b.addListener(...);
    

  • 如果您想在 Listener 中更改小部件本身,您可以使用 Event#widget 来获取事件源.

  • If you want to change the widget itself in the Listener you can use Event#widget to get the source of the event.

    b.addListener(SWT.Selection, new Listener()
    {
        public void handleEvent(Event e)
        {
            Button button = (Button) e.widget;
        }
    });
    

  • 这篇关于Java SWT:正确使用小部件(按钮、标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆