当前位置: 代码迷 >> J2SE >> 点击旋钮,新打开一个窗口
  详细解决方案

点击旋钮,新打开一个窗口

热度:175   发布时间:2016-04-24 12:10:44.0
点击按钮,新打开一个窗口
用到了,swt,在frame中有两个按钮,test1,test2。点test1时,会出现一个新的窗口里面显示baidu。。。但这是,我点test2 没有反应,这是什么原因,应该怎么处理,谢了。

Java code
import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import org.eclipse.swt.SWT;import org.eclipse.swt.browser.Browser;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Menu;import org.eclipse.swt.widgets.Shell;public class HelloFrame extends JFrame implements ActionListener{        private String name="";    private JButton btn;    private JButton btn2;    public HelloFrame(){        this.setLayout(new FlowLayout());        this.setSize(200,200);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        this.setTitle("Hello World!");         btn =new JButton("test1");        btn.addActionListener(this);        this.add(btn);                 btn2 =new JButton("test2");            btn2.addActionListener(this);            this.add(btn2);        this.setVisible(true);    }    public static void main(String[] args){        new HelloFrame();    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public void actionPerformed(ActionEvent e) {                        if(e.getSource()==btn){            Display display = new Display();            Shell shell = new Shell(display);            shell.setText("CNTalk-UC Manger");                        shell.setLayout(new FillLayout());            shell.setBounds(100, 100, 810, 600);            shell.setMaximized(false); // 窗口最大化            final Browser browser = new Browser(shell, SWT.NONE);            browser.setBounds(0, 0, 800, 600);            browser.setUrl("http://www.baidu.com");            browser.setMenu(new Menu(browser));            shell.open();            while (!shell.isDisposed()) {                if (!display.readAndDispatch())                    display.sleep();            }            display.dispose();        }else if(e.getSource()==btn2){            System.out.println("test2");        }    }        }


------解决方案--------------------
if(e.getSource()==btn){ //当e.getSource()==btn2时的处理没有,当然没反应了

------解决方案--------------------
点test2时只是在控制台输出数据 System.out.println("test2");
如果你是控制台启动的,可以看到控制台打印出 test2
------解决方案--------------------
探讨
引用:

点test2时只是在控制台输出数据 System.out.println("test2");
如果你是控制台启动的,可以看到控制台打印出 test2


点test2 控制台没有反应。

------解决方案--------------------
探讨
只有关闭新打开的窗口时,test2才会在控制台输出内容。

------解决方案--------------------
顶一下,呵呵,对图形界面的应用接触的不是很多
------解决方案--------------------
探讨

只有关闭新打开的窗口时,test2才会在控制台输出内容。

------解决方案--------------------
你把新窗口关了,点击btn2就会有输出的,是不能获取焦点的问题
  相关解决方案