JAVA SWT遇到的问题

2025-05-09 05:34:10
推荐回答(5个)
回答1:

文件一:

public class ChildShellExample {

@SuppressWarnings("unused")
ChildShellExample() {
Display d = new Display();
final Shell s = new Shell(d);
s.setSize(500, 500);

Button button = new Button(s, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
ChildShell cs = new ChildShell(s);
}
});
button.setBounds(163, 204, 68, 23);
button.setText("打开");
// s.setMaximized(false);
s.open();

while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}

public static void main(String[] args) {
new ChildShellExample();
}
}

文件二:

import org.eclipse.swt.widgets.Shell;

public class ChildShell {
ChildShell(Shell parent) {
Shell child = new Shell(parent);
child.setSize(200, 200);
child.open();
}
}

楼主自己试试吧。。

其实也不难。。。

祝你好运。。。。

回答2:

//看了我下面的代码你就知道了:
import java.awt.event.*;
import javax.swing.*;
public class google extends JFrame implements ActionListener{
JButton jButton=new JButton("显示新窗口");
public google() {
jButton.addActionListener(this);
this.add(jButton);
this.setBounds(200, 200, 300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
JFrame jFrame=new JFrame("我是新窗口");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setBounds(200, 200, 200, 200);
jFrame.setVisible(true);

}
public static void main(String[] args) {
new google();
}

}

回答3:

是不是窗体没有setVisible(true)的

回答4:

我说下思路,你要给按扭 增加监听器,然后在监听器实现的方法中去new你的新窗口,然后设置新窗口显示就可以了,如果你的思路是这样,只能说明你的代码有bug

回答5:

SWT是用的windows的窗口机制,不可以用java的思想来理解,搞清楚了消息循环机制,就知道为什么要用监听的方式了