简体   繁体   English

更改java swt中的标签文本

[英]changing label text in java swt

Working on my first java swt program and I am having trouble, possibly a stupidity issue! 在我的第一个Java swt程序上工作时,我遇到了麻烦,可能是一个愚蠢的问题! I am trying to create a basic network program to listen for connections, i have a button and I want to start the socket then change a label to display " server listening" or whatever 我正在尝试创建一个基本的网络程序来侦听连接,我有一个按钮,我想启动套接字,然后更改标签以显示“服务器侦听”或其他内容

Here is the button 这是按钮

Button startServerBtn = new Button(shlChattybox, SWT.NONE);
        startServerBtn.addSelectionListener(new SelectionAdapter() {

            ServerSocket serversocket = new ServerSocket(PORT); 

Can I not change the text of a label here ?? 我可以在这里更改标签的文本吗?

            serverStatusLbl2.setText("listening!"); 

            public void widgetSelected(SelectionEvent e) {
            }
        });
    startServerBtn.setFont(SWTResourceManager.getFont("Segoe UI", 16, SWT.BOLD));
    startServerBtn.setBounds(53, 63, 260, 75);
    startServerBtn.setText("Start Server");

    listeningPortTxt = new Text(shlChattybox, SWT.BORDER);
    listeningPortTxt.setBounds(143, 26, 76, 21);

    Label listeningPortLbl = new Label(shlChattybox, SWT.NONE);
    listeningPortLbl.setBounds(53, 29, 84, 15);
    listeningPortLbl.setText("Listening Port: ");

    Label serverStatusLbl1 = new Label(shlChattybox, SWT.NONE);
    serverStatusLbl1.setBounds(53, 157, 84, 15);
    serverStatusLbl1.setText("Server Status:");

There are two ways to do this: 有两种方法可以做到这一点:

  1. Make the Label a field of the class 使Label成为课程的一个字段
  2. Make the Label final 使Label final

Here is an example: 这是一个例子:

private Label fieldLabel = new Label(shell, SWT.NONE);

public void testMethod()
{
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Print");

    final Label finalLabel = new Label(shell, SWT.NONE);

    button.addListener(SWT.Selection, new Listener(){
        @Override
        public void handleEvent(Event e)
        {
            fieldLabel.setText("TEXT");
            finalLabel.setTexT("TEXT");
        }
    })
}

This is nothing specific to SWT, so you might want to read something about closure in programming. 这不是SWT特有的,因此您可能需要阅读一些有关闭包编程的知识。

Here is a related question on SO where the infamous Jon Skeet posted an answer... 是一个有关SO的相关问题,臭名昭著的Jon Skeet发表了答案...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM