我有一个程序,我正在将其用作习惯 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]);
最佳答案
您可能使用了错误的 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();
}
关于java - SWT 文本输出和换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18905980/