简体   繁体   English

为什么我在输入中得到这样的null

[英]Why i'm getting such null in my input

I need to code simple calculator with UI in one class and model in another, but how can I right access data from another class? 我需要在一个类中编写带有UI的简单计算器,在另一个类中编写模型,但是如何正确访问另一类的数据呢? for example? 例如? if i press 123 i'll get in text such output null123 ? 如果我按123,我会输入这样的文本null123? Help me please? 请帮帮我? also how can i optimise my code, where I have mistakes? 还有我该如何在错误的地方优化我的代码?

View class: 查看课程:

package calc;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.SWTResourceManager;

public class View extends ViewPart {
    public Text inputText;
    public Text inputText2;
    public Calculation calcul;

    public View()
    {
        calcul = new Calculation();
    }

    public void createPartControl(Composite parent) {
        Composite mainComposite = new Composite(parent, SWT.NONE);
        GridLayout gltop = new GridLayout(1, false);        
        gltop.numColumns = 1;       
        mainComposite.setLayout(gltop);
        //mainComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
        //ВЫзов верхнего и нижнего композита
        createTop(mainComposite);
        createBott(mainComposite);      
    }

    private void createTop(Composite parent)
    {
        GridLayout gltop = new GridLayout(1, false);        
        gltop.numColumns = 1;
        GridData data = new GridData(GridData.FILL_HORIZONTAL);     
        Composite topComposite = new Composite(parent, SWT.NONE);       
        topComposite.setLayout(gltop);
        topComposite.setLayoutData(data);
        //topComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));

        inputText = new Text(topComposite, SWT.NONE);
        inputText.setLayoutData(data);
        inputText.setFont(SWTResourceManager.getFont("", 18, SWT.BOLD));
    }

    private void createBott(Composite parent)
    {
        GridLayout glbot = new GridLayout(1, false);        
        glbot.numColumns = 2;
        GridData data = new GridData(GridData.FILL_BOTH);       
        Composite botComposite = new Composite(parent, SWT.NONE);       
        botComposite.setLayout(glbot);
        botComposite.setLayoutData(data);
        //botComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));    


        createLeft(botComposite);       
        createRight(botComposite);      
    }

    private void createLeft(Composite parent)
    {           
        Composite leftComposite = new Composite(parent, SWT.NONE);  
        GridLayout glleft = new GridLayout(1, false);       
        GridData data = new GridData(GridData.FILL_HORIZONTAL); 
        glleft.marginTop = 26;
        leftComposite.setLayout(glleft);
        leftComposite.setLayoutData(data);
        //leftComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN)); 

        inputText2 = new Text(leftComposite, SWT.BORDER_SOLID);
        inputText2.setText("Hi");
        Button buttonMC = createFuncDigButtons(leftComposite, data, "MC", 'M');
        Button buttonMR = createFuncDigButtons(leftComposite, data, "MR", 'R');
        Button buttonMS = createFuncDigButtons(leftComposite, data, "MS", 'S');
        Button buttonMpl = createFuncDigButtons(leftComposite, data, "MC", 'P');
    }

    private void createRight(Composite parent)
    {
        GridLayout glright = new GridLayout(1, false);      
        GridData data = new GridData(GridData.FILL_BOTH);       
        Composite rightComposite = new Composite(parent, SWT.NONE);     
        glright.numColumns = 1;
        rightComposite.setLayout(glright);
        rightComposite.setLayoutData(data);
        //rightComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));  

        createRightTop(rightComposite);
        createRightBot(rightComposite);
    }

    private void createRightTop(Composite parent)
    {
        Composite rightTopComposite = new Composite(parent, SWT.NONE);
        GridLayout glltop = new GridLayout(1, false);       
        GridData data = new GridData(GridData.FILL_BOTH);   
        glltop.numColumns = 3;
        glltop.marginBottom = 0;
        glltop.marginTop = 0;
        rightTopComposite.setLayout(glltop);
        rightTopComposite.setLayoutData(data);
        //rightTopComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_MAGENTA)); 

        Button buttonBack = createFuncDigButtons(rightTopComposite, data, "Backspace", 'B');
        Button buttonC = createFuncDigButtons(rightTopComposite, data, "C", 'C');
        Button buttonCE = createFuncDigButtons(rightTopComposite, data, "CE", 'E');
    }

    private void createRightBot(Composite parent)
    {
        GridLayout glright = new GridLayout(1, false);      
        GridData data = new GridData(GridData.FILL_BOTH);       
        Composite rightBotComposite = new Composite(parent, SWT.NONE);      
        glright.numColumns = 5;
        rightBotComposite.setLayout(glright);
        rightBotComposite.setLayoutData(data);
        //rightBotComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));       
        Button button7 = createFuncDigButtons(rightBotComposite, data, "7", '7');
        Button button8 = createFuncDigButtons(rightBotComposite, data, "8", '8');
        Button button9 = createFuncDigButtons(rightBotComposite, data, "9", '9');
        Button buttonDev = createFuncDigButtons(rightBotComposite, data, "/", '/');
        Button buttonSQRT = createFuncDigButtons(rightBotComposite, data, "sqrt", 'R');
        Button button4 = createFuncDigButtons(rightBotComposite, data, "4", '4');
        Button button5 = createFuncDigButtons(rightBotComposite, data, "5", '5');
        Button button6 = createFuncDigButtons(rightBotComposite, data, "6", '6');
        Button buttonMult = createFuncDigButtons(rightBotComposite, data, "*", '*');
        Button buttonPer = createFuncDigButtons(rightBotComposite, data, "%", '%');
        Button button1 = createFuncDigButtons(rightBotComposite, data, "1", '1');
        Button button2 = createFuncDigButtons(rightBotComposite, data, "2", '2');
        Button button3 = createFuncDigButtons(rightBotComposite, data, "3", '3');
        Button buttonMinus = createFuncDigButtons(rightBotComposite, data, "-", '-');
        Button buttonDev1 = createFuncDigButtons(rightBotComposite, data, "1/x", '1');
        Button button0 = createFuncDigButtons(rightBotComposite, data, "0", '0');
        Button buttonPM = createFuncDigButtons(rightBotComposite, data, "+/-", 'p');
        Button buttonD = createFuncDigButtons(rightBotComposite, data, ".", '.');       
        Button buttonPlus = createFuncDigButtons(rightBotComposite, data, "+", '+');        
        Button buttonR = createFuncDigButtons(rightBotComposite, data, "=", '=');
    }

    public Button createFuncDigButtons(Composite parent, GridData gridData, final String digit, final char formethod)
    {
        Button button = new Button(parent, SWT.PUSH);
          button.setLayoutData(gridData);
          button.setText(String.valueOf(digit));
          button.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent arg0) {
             // update(formethod);
                calcul.getDigit(formethod);
                inputText.setText(calcul.num1);
                System.out.println("f " + calcul.num1);
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent arg0) {
              /* do nothing */
            }
          });
          button.addKeyListener(new KeyListener() {

            @Override
            public void keyReleased(KeyEvent arg0) {
            }

            @Override
            public void keyPressed(KeyEvent arg0) {
                calcul.getDigit(arg0.character);
            }
        });
        return button;
    }

    public void setFocus() {
    }
}

And pseudomodel class: 和伪模型类:

package calc;

public class Calculation {

    public String num1;
    public String num2;

    public Calculation()
    {
    }

    public char getDigit(char a)
    {
        char value = a;
        switch (a) {
        case 'q':

            break;

        default:
            num1 += value;
            break;
        }
        System.out.println("getbyclass" + a + "bb" +num1);
        return value;       
    }
}

Help me please, becouse i need help to understand 请帮助我,因为我需要帮助来了解

initialize the num1 and num2 strings to "" 将num1和num2字符串初始化为""

otherwise the strings are set to null and when you concatenate that with another string it gets parsed to "null" 否则,将字符串设置为null,并在将其与另一个字符串连接时将其解析为"null"

This will help: 这将有助于:

in your function createFuncDigButtons change the 3rd argument from final String digit to final char digit and pass in a character. 在您的函数中, createFuncDigButtons将第三个参数从final String digit更改为final char digit并传入一个字符。

in your keyPressed handler change this line 在您的keyPressed处理程序中更改此行

calcul.getDigit(arg0.character);

to this 对此

calcul.getDigit(digit);

You also need to initialize your string variables in your Calculation constructor. 您还需要在Calculation构造函数中初始化字符串变量。

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

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