我实际上正在制作一个基于 swt 的程序。通过尝试清理我的代码,我遇到了一个问题。我想将“组和按钮的声明”放入一个新类中,但因此我需要将shell
对象发送到我的新类中。我怎样才能做到这一点?(如果我将shell
对象发送到构造函数中,它将不起作用)。这是我的问题的SSCCE:
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
public class Class1
{
static Shell shell;
protected void createContents()
{
shell = new Shell();
shell.setMinimumSize(new Point(800, 600));
shell.setBackground(SWTResourceManager.getColor(242, 242, 242));
shell.setSize(815, 600);
shell.setText("Test");
/*
* Declaration of the groups and buttons
*/
Group areaA = new Group(shell, SWT.NONE);
Button btn1 = new Button(areaA, SWT.CHECK);
Button btn2 = new Button(areaA, SWT.CHECK);
Button btn3 = new Button(areaA, SWT.CHECK);
Group areaB = new Group(shell, SWT.NONE);
Spinner spin1 = new Spinner(areaB, SWT.BORDER);
Spinner spin2 = new Spinner(areaB, SWT.BORDER);
Spinner spin3 = new Spinner(areaB, SWT.BORDER);
Spinner spin4 = new Spinner(areaB, SWT.BORDER);
Button btnA = new Button(areaB, SWT.CHECK);
/*
* Processing of the buttons (short example)
*/
areaA.setBounds(52, 46, 383, 210);
btn1.setBounds(10, 10, 85, 16);
btn1.setText("Button1");
btn2.setBounds(10, 56, 85, 16);
btn2.setText("Button2");
btn3.setBounds(10, 111, 85, 16);
btn3.setText("Button3");
areaB.setBounds(52, 274, 383, 192);
spin1.setBounds(193, 34, 47, 21);
spin2.setBounds(282, 34, 47, 21);
spin3.setBounds(193, 82, 47, 21);
spin4.setBounds(282, 82, 47, 21);
btnA.addSelectionListener(new SelectionAdapter()
{
@Override
public void widgetSelected(SelectionEvent arg0)
{
}
});
btnA.setBounds(10, 34, 85, 16);
btnA.setText("Select range a");
Button btnB = new Button(areaB, SWT.CHECK);
btnB.setBounds(10, 87, 85, 16);
btnB.setText("Select range b");
}
public static void main(String[] args)
{
/*
* Creating and disposing of the window
*/
Display display = Display.getDefault();
try
{
Class1 window = new Class1();
window.createContents();
shell.layout();
shell.setVisible(true);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
你知道如何正确地做到这一点吗?
编辑:首先尝试将整体放在两个不同的类中:
package test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import test.Class2;
public class Class2
{
public Class2(Shell shell)
{
/*
* Declaration of the groups and buttons
*/
Group areaA = new Group(shell, SWT.NONE);
Button btn1 = new Button(areaA, SWT.CHECK);
Button btn2 = new Button(areaA, SWT.CHECK);
Button btn3 = new Button(areaA, SWT.CHECK);
Group areaB = new Group(shell, SWT.NONE);
Spinner spin1 = new Spinner(areaB, SWT.BORDER);
Spinner spin2 = new Spinner(areaB, SWT.BORDER);
Spinner spin3 = new Spinner(areaB, SWT.BORDER);
Spinner spin4 = new Spinner(areaB, SWT.BORDER);
Button btnA = new Button(areaB, SWT.CHECK);
}
}
现在调用和使用 Class2(来自 SSCCE 的修改示例):
protected void createContents()
{
shell = new Shell();
shell.setMinimumSize(new Point(800, 600));
shell.setBackground(SWTResourceManager.getColor(242, 242, 242));
shell.setSize(815, 600);
shell.setText("Test");
Class2 buttons = new Class2(shell);
/*
* Processing of the buttons (short example)
*/
buttons.areaA.setBounds(52, 46, 383, 210);
buttons.btn1.setBounds(10, 10, 85, 16);
buttons.btn1.setText("Button1");
buttons.btn2.setBounds(10, 56, 85, 16);
buttons.btn2.setText("Button2");
buttons.btn3.setBounds(10, 111, 85, 16);
buttons.btn3.setText("Button3");
buttons.areaB.setBounds(52, 274, 383, 192);
buttons.spin1.setBounds(193, 34, 47, 21);
buttons.spin2.setBounds(282, 34, 47, 21);
buttons.spin3.setBounds(193, 82, 47, 21);
buttons.spin4.setBounds(282, 82, 47, 21);
buttons.btnA.addSelectionListener(new SelectionAdapter()
{
@Override
public void widgetSelected(SelectionEvent arg0)
{
}
});
buttons.btnA.setBounds(10, 34, 85, 16);
buttons.btnA.setText("Select range a");
Button btnB = new Button(buttons.areaB, SWT.CHECK);
btnB.setBounds(10, 87, 85, 16);
btnB.setText("Select range b");
}
这个例子不起作用
我想到的另一个解决方案是将声明性部分放入 class2 的主体中,但是Shell
对象丢失了,我不知道如何在不通过构造函数获取对象的情况下将对象赋予类。
这是我的问题的 SSCCE:我想将“组和按钮的声明”放入一个新类中,但因此我需要将shell
对象发送到我的新类中。我怎样才能做到这一点?(如果我将shell
对象发送到构造函数中,它将不起作用)。
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
public class Class1
{
static Shell shell;
protected void createContents()
{
shell = new Shell();
shell.setMinimumSize(new Point(800, 600));
shell.setBackground(SWTResourceManager.getColor(242, 242, 242));
shell.setSize(815, 600);
shell.setText("Test");
/*
* Declaration of the groups and buttons
*/
Group areaA = new Group(shell, SWT.NONE);
Button btn1 = new Button(areaA, SWT.CHECK);
Button btn2 = new Button(areaA, SWT.CHECK);
Button btn3 = new Button(areaA, SWT.CHECK);
Group areaB = new Group(shell, SWT.NONE);
Spinner spin1 = new Spinner(areaB, SWT.BORDER);
Spinner spin2 = new Spinner(areaB, SWT.BORDER);
Spinner spin3 = new Spinner(areaB, SWT.BORDER);
Spinner spin4 = new Spinner(areaB, SWT.BORDER);
Button btnA = new Button(areaB, SWT.CHECK);
/*
* Processing of the buttons (short example)
*/
areaA.setBounds(52, 46, 383, 210);
btn1.setBounds(10, 10, 85, 16);
btn1.setText("Button1");
btn2.setBounds(10, 56, 85, 16);
btn2.setText("Button2");
btn3.setBounds(10, 111, 85, 16);
btn3.setText("Button3");
areaB.setBounds(52, 274, 383, 192);
spin1.setBounds(193, 34, 47, 21);
spin2.setBounds(282, 34, 47, 21);
spin3.setBounds(193, 82, 47, 21);
spin4.setBounds(282, 82, 47, 21);
btnA.addSelectionListener(new SelectionAdapter()
{
@Override
public void widgetSelected(SelectionEvent arg0)
{
}
});
btnA.setBounds(10, 34, 85, 16);
btnA.setText("Select range a");
Button btnB = new Button(areaB, SWT.CHECK);
btnB.setBounds(10, 87, 85, 16);
btnB.setText("Select range b");
}
public static void main(String[] args)
{
/*
* Creating and disposing of the window
*/
Display display = Display.getDefault();
try
{
Class1 window = new Class1();
window.createContents();
shell.layout();
shell.setVisible(true);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}