【问题标题】:My SWT label is not changing text with the selection of new item in combobox?我的 SWT 标签没有通过在组合框中选择新项目来更改文本?
【发布时间】:2019-07-05 05:56:48
【问题描述】:

我目前正在使用绝对布局,并且我有一个标签应该随着在组合框中选择一个新项目而改变我为我做这项工作......任何事情都会不胜感激...... 这是我正在谈论的代码的一部分:

    Combo comboLevels = new Combo(shellAfterCasual, SWT.NONE);
    comboLevels.setBounds(10, 40, 91, 23);
    String[] item = new String[] { "Swedish 1", "Swedish 2", "Swedish 3" };
    comboLevels.setItems(new String[] {"Swedish 1", "Swedish 2", "Swedish 3"});
    comboLevels.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
             levelStr1 = comboLevels.getItem(comboLevels.getSelectionIndex());
            System.out.println("Selection: " + comboLevels.getItem(comboLevels.getSelectionIndex()));


            Label lblvarLvlLabel = new Label(shellAfterCasual, SWT.NONE);
            lblvarLvlLabel.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.BOLD | SWT.ITALIC));
            lblvarLvlLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
            lblvarLvlLabel.setBounds(145, 5, 107, 27);
            lblvarLvlLabel.setText(comboLevels.getText());
            lblvarLvlLabel.getParent().layout();
            /*'''''''''''''.............'''''''''''''''''''''''''''''''''''''''''''*/
            System.out.println("XDDD;"+levelStr1);
        }
    });

【问题讨论】:

  • 基本上我希望标签有瑞典语 1 或 2 或 3 分别对应所选内容
  • 并且它只更新一次,例如如果选择了瑞典语 1,瑞典语 1 将出现,但如果之后选择瑞典语 2 或 3,它不会改变
  • JComboBox 在里面做什么,是 Swing 而不是 SWT。每次选择更改时,您都会创建一个新标签 - 不好。尝试将 setBounds 与 Layouts 混合使用将无法正常工作。
  • 我不认为这是一个 JcomboBox 我正在使用窗口构建器,它不是 JFace 选项...
  • 我意识到 JCombo 框不应该在那里

标签: java label swt refresh


【解决方案1】:

我终于找到了如何做到这一点,我在动作侦听器之外启动了标签,以便标签始终存在,但是当这样选择项目时它是 .setText 和 .updates:

Label lblvarLvlLabel = new Label(shellAfterCasual, SWT.NONE);
    lblvarLvlLabel.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.BOLD | SWT.ITALIC));
    lblvarLvlLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
    lblvarLvlLabel.setBounds(145, 5, 107, 27);


    Combo comboLevels = new Combo(shellAfterCasual, SWT.NONE);
    comboLevels.setBounds(10, 40, 91, 23);
    String[] item = new String[] { "Swedish 1", "Swedish 2", "Swedish 3" };
    comboLevels.setItems(new String[] {"Swedish 1", "Swedish 2", "Swedish 3"});
    comboLevels.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
             levelStr1 = comboLevels.getItem(comboLevels.getSelectionIndex());
            System.out.println("Selection: " + comboLevels.getItem(comboLevels.getSelectionIndex()));
            lblvarLvlLabel.setText(comboLevels.getText());
            lblvarLvlLabel.getParent().layout();
            lblvarLvlLabel.redraw();
            lblvarLvlLabel.update();
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 2017-09-19
    相关资源
    最近更新 更多