【问题标题】:SWT text output and new linesSWT 文本输出和换行
【发布时间】:2013-09-19 22:31:11
【问题描述】:

我有一个程序,我用它来做一个习惯 GUI 的实验。它基本上采用 f(x)=ax^2+bx+c 形式的二次方,并找到实数零点、y 截距和对称轴。它在计算和所有方面都运行良好。我的问题是我创建了一个不可编辑的文本框(使用窗口构建器 SWT 应用程序),无论我做什么,它总是在一行上打印所有内容! \n 不工作,\r\n 不工作...请帮助。

Button btnNewButton = new Button(shlParacalc, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {

        String aval = alphabox.getText();
        double a = Double.parseDouble(aval);

        String bval = betabox.getText();
        double b = Double.parseDouble(bval);

        String cval = gammabox.getText();
        double c = Double.parseDouble(cval);


        CalcLib mathObj = new CalcLib();    

        double yint = mathObj.yIntercept(a, b, c);
        double axis = mathObj.Axis(a, b);
        double zero[] = mathObj.Zero(a, b, c);


        outputbox.append("y-intercept = " + yint); // these four lines need
        outputbox.append("axis of symmetry = " + axis); //to be printed
        outputbox.append("1st zero = " + zero[0]); //on individual lines
        outputbox.append("2nd zero = " + zero[1]);

【问题讨论】:

  • 输出框的类型是什么?
  • 它的类型是 Text 而不是 TextArea 或 Text Field。
  • Text 构造函数中指定了哪些样式位?
  • 嗯,我会尝试让它多行,也许 \r\n 会起作用。

标签: java user-interface swt newline


【解决方案1】:

您可能使用了错误的style bits。此代码有效:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);

    Button button = new Button(shell, SWT.NONE);
    button.setText("Add text");
    button.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {

            text.append("y-intercept = \n");
            text.append("axis of symmetry = \n");
            text.append("1st zero = \n");
            text.append("2nd zero = \n");
        }
    });

    shell.pack();
    shell.setSize(400, 200);
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

【讨论】:

  • 完美。我所要做的就是用 SWT.MULTI 替换 SWT.NONE...谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
相关资源
最近更新 更多