简体   繁体   English

在java中将文本打印到文本字段

[英]Printing text to a textfield in java

I am still working on that program where i asked a question about yesterday.我仍在研究那个我昨天问过一个问题的程序。

So i have this piece of code:所以我有这段代码:

public static void main(String[] args) {

    Display display = Display.getDefault();
    Shell shlKoffieHalenApp = new Shell();
    shlKoffieHalenApp.setMinimumSize(new Point(610, 430));
    shlKoffieHalenApp.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
    shlKoffieHalenApp.setSize(610, 430);
    shlKoffieHalenApp.setText("Koffie Halen App");
    shlKoffieHalenApp.setLayout(null);

    List list = new List(shlKoffieHalenApp, SWT.BORDER);
    list.setFont(SWTResourceManager.getFont("Sans", 13, SWT.BOLD));
    list.setItems(new String[] {"Anne", "", "Bas", "", "Daan", "", "Nick", "", "Paul", "", "Peter", "", "Sebastien"});
    list.setBackground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
    list.setBounds(10, 10, 113, 377);

    ButtonAnne = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonAnne.setBounds(129, 17, 137, 16);
    ButtonAnne.setText("Aanwezig/Afwezig");

    ButtonBas = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonBas.setBounds(129, 67, 137, 16);
    ButtonBas.setText("Aanwezig/Afwezig");

    ButtonDaan = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonDaan.setBounds(129, 120, 137, 16);
    ButtonDaan.setText("Aanwezig/Afwezig");

    ButtonNick = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonNick.setBounds(129, 173, 137, 16);
    ButtonNick.setText("Aanwezig/Afwezig");

    ButtonPaul = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonPaul.setBounds(129, 224, 137, 16);
    ButtonPaul.setText("Aanwezig/Afwezig");

    ButtonPeter = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonPeter.setBounds(129, 276, 137, 16);
    ButtonPeter.setText("Aanwezig/Afwezig");

    ButtonSebastien = new Button(shlKoffieHalenApp, SWT.CHECK);
    ButtonSebastien.setBounds(129, 329, 137, 16);
    ButtonSebastien.setText("Aanwezig/Afwezig");

    ButtonKoffie = new Button(shlKoffieHalenApp, SWT.NONE);
    ButtonKoffie.setBounds(394, 53, 121, 34);
    ButtonKoffie.setText("Klik voor koffie!");

    Answer = new Text(shlKoffieHalenApp, SWT.BORDER);
    Answer.setEditable(false);
    Answer.getText();
    Answer.setBounds(355, 106, 200, 30);

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

        Random generate = new Random();
        ArrayList<String> names = new ArrayList<String>(); {

        if(ButtonAnne.getSelection() == true) {
            names.add("Anne");
        }
        if(ButtonBas.getSelection() == true) {
            names.add("Bas");
        }
        if(ButtonDaan.getSelection() == true) {
            names.add("Daan");
        }
        if(ButtonNick.getSelection() == true) {
            names.add("Nick");
        }
        if(ButtonPaul.getSelection() == true) {
            names.add("Paul");
        }
        if(ButtonPeter.getSelection() == true) {
            names.add("Peter");
        }
        if(ButtonSebastien.getSelection() == true) {
            names.add("Sebastien");
        }   

            if(ButtonKoffie.getSelection() == true) {
                int randomIndex = generate.nextInt(names.size());

                Answer.setText(names.get(randomIndex) + " moet koffie halen!");                 

            }
        }           
    }
}

Answer is the textfield where i want to print the random name thats picked from the ArrayList.答案是我想打印从 ArrayList 中选取的随机名称的文本字段。 What am i missing here?我在这里缺少什么? I tried to creat a method but that did not work out as planned.我试图创建一种方法,但没有按计划进行。

You should not put anything in the SWT event loop, user interfaces are event driven, you should not use polling.你不应该在SWT事件循环中放置任何东西,用户界面是事件驱动的,你不应该使用轮询。

You should add event listeners to the components, for example if you want to intercept when a button is pressed, you should add a SelectionListener to it:您应该向组件添加事件侦听器,例如,如果您想在按下按钮时进行拦截,则应该向其添加一个SelectionListener

buttonKoffie = new Button(shlKoffieHalenApp, SWT.NONE);
buttonKoffie.addSelectionListener(new SelectionListener() {
    @Override
    public void widgetDefaultSelected(SelectionEvent arg0) {    
    }

    @Override
    public void widgetSelected(SelectionEvent arg0) {

        // this method is called when the button is pressed 

        int randomIndex = generate.nextInt(names.size());
        answer.setText(names.get(randomIndex) + " moet koffie halen!");  
    }
});

Read the documentation of the various components to know which listeners you can add and how they behave.阅读各种组件的文档以了解您可以添加哪些侦听器以及它们的行为方式。

The Button documentation also specifies that getSelection works only for CHECK , RADIO and TOGGLE buttons, it will always return false for PUSH buttons. Button文档还指定getSelection仅适用于CHECKRADIOTOGGLE按钮,它始终为PUSH按钮返回false

Another problem of putting code in the SWT event loop is that you are continuously re-creating the names array, so you will always find it empty.将代码放入SWT事件循环的另一个问题是您不断地重新创建names数组,因此您总是会发现它是空的。 You should create it once and use event listeners of the check buttons to fill it.您应该创建一次并使用复选按钮的事件侦听器来填充它。

Other recommendations:其他建议:

  • adopt the proper Java naming convention (for example, the name of the variables should start with a lowercase letter)采用正确的 Java 命名约定(例如,变量的名称应以小写字母开头)
  • use proper layouts instead of absolute positioning (you should never have to call setBounds )使用正确的布局而不是绝对定位(您永远不必调用setBounds

Replace代替

Answer.setText(randomIndex + " moet koffie halen!");

with

Answer.setText(names.get(randomIndex) + " moet koffie halen!");

this lines of code:这行代码:

 int randomIndex = generate.nextInt(names.size());
 Answer.setText(randomIndex + " moet koffie halen!");    

are setting in the textView only the random int you got, you need instead the random element in the list... then you need to use names.get(randomIndex)在 textView 中只设置你得到的随机整数,你需要列表中的随机元素......然后你需要使用names.get(randomIndex)

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

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