Hej
Man kan bruge dispose() til at lukker vinduer (frames) med. jeg har lavet et
lille eks. hvor det virker. Håber det kan hjælpe.
mvh
Tommy
//*************************************
// main class
//*************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class gui1 implements ActionListener {
JFrame gui1Frame;
JButton b1;
JPanel gui1Panel;
// constructor
public gui1() {
gui1Frame = new JFrame("Main Gui");
gui1Panel = new JPanel();
gui1Panel.setLayout(null);// minus layout
addWidgets();
gui1Frame.getContentPane().add(gui1Panel, BorderLayout.CENTER);
gui1Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui1Frame.pack();
gui1Frame.setBounds(150, 150, 345, 175);
gui1Frame.setVisible(true);
}
private void addWidgets() {
b1 = new JButton("Open GUI 2");
b1.addActionListener(this);
gui1Panel.add(b1);
b1.setBounds(25,90,150,40);
}
public void actionPerformed(ActionEvent event){
Object source = event.getSource();
if(source == b1){
gui2 newWindow = new gui2();
}
}
public static void main(String[] args){
gui1 test = new gui1();
}
}
//******************************************
// gui 2 class
//******************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class gui2 implements ActionListener {
JFrame gui2Frame;
JButton b2;
JPanel gui2Panel;
// constructor
public gui2() {
gui2Frame = new JFrame("GUI 2");
gui2Panel = new JPanel();
gui2Panel.setLayout(null);// minus layout
addWidgets();
gui2Frame.getContentPane().add(gui2Panel, BorderLayout.CENTER);
gui2Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui2Frame.pack();
gui2Frame.setBounds(250, 250, 345, 175);
gui2Frame.setVisible(true);
}
private void addWidgets() {
b2 = new JButton("Close gui 2");
b2.addActionListener(this);
gui2Panel.add(b2);
b2.setBounds(25,90,150,40);
}
public void actionPerformed(ActionEvent event){
Object source = event.getSource();
// Lukker vinduet
if(source == b2)
gui2Frame.dispose();
}
}
|