import java.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;public public class main extends JFrame{ JButton jb = new JButton("新窗口");
JPanel jp = new JPanel(); private void JBInit(){ jp.add(jb); this.add("Center", jp); this.setSize(400, 130); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public main() { try { addListener(); JBInit(); } catch (Exception exception) { exception.printStackTrace(); } } public static void main(String[] args) { main m = new main(); m.setVisible(true); } private void addListener(){ jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { showFrame(); } }); } private void showFrame(){ this.setVisible(false); newFrame nf = new newFrame(); nf.setVisible(true); } }//新窗体 import java.swing.*;public public class newFrame extends JFrame{ private void JBInit(){ this.setSize(400, 130); this.setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public newFrame() { JBInit(); } }
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;public class TestDialog extends Dialog{
private Button mButton = null;
public TestDialog(Shell shell){
super(shell);
}
protected Control createDialogArea(Composite parent) { Composite result = (Composite) super.createDialogArea(parent);
mButton = new Button(result, SWT.PUSH);
mButton.setText("Click");
mButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
cancelPressed(); 先关闭父窗口
(new NewDialog(getParentShell())).open(); 弹出新窗口
}
});
return result;
}
protected void cancelPressed() {
super.cancelPressed();
}
class NewDialog extends Dialog{
public NewDialog(Shell shell){
super(shell);
}
} public static void main(String args[]){
Display display = new Display();
Shell shell= new Shell(display);
shell.setText("Test");
shell.open();
TestDialog dialog = new TestDialog(shell);
dialog.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}
后面再接要调用的新窗口就好了
import java.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public public class main extends JFrame{
JButton jb = new JButton("新窗口");
JPanel jp = new JPanel(); private void JBInit(){
jp.add(jb);
this.add("Center", jp);
this.setSize(400, 130);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public main() {
try {
addListener();
JBInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} public static void main(String[] args) {
main m = new main();
m.setVisible(true);
} private void addListener(){
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showFrame();
}
});
} private void showFrame(){
this.setVisible(false);
newFrame nf = new newFrame();
nf.setVisible(true);
}
}//新窗体
import java.swing.*;public public class newFrame extends JFrame{ private void JBInit(){
this.setSize(400, 130);
this.setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public newFrame() {
JBInit();
}
}