Lukning af popupvindue
Hvordan får jeg dette popup vindue til at lukke når jeg trykker på knappen?class Main
{
public static void main(String args[])
{
new TestKlasseSwing().show();
}
}
import javax.swing.*;
import java.awt.*;
public class JPop extends JDialog
{
public JPop(Frame owner, String title, int x, int y)
{
super(owner, title);
this.setSize(200, 200);
this.setLocation(x, y);
String tekst = "x: " + x + " y: " + y;
reol reol = new reol();
//JLabel label = new JLabel(tekst);
this.getContentPane().add(reol);
this.show();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class reol extends JPanel
{
JButton lukButton;
public reol()
{
setLayout(null);
setBorder(BorderFactory.createTitledBorder("popup"));
lukButton = new JButton("luk");
add(lukButton);
Insets insets = getInsets();
lukButton.setBounds ( 20 + insets.left, 20 + insets.top, 100, 60);
lukButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//???
}
}
);
} public Dimension getPreferredSize()
{
return new Dimension(800, 500);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TestKlasseSwing extends JFrame implements WindowListener, MouseListener
{
private int frameWidth = 640;
private int frameHeight = 480;
public TestKlasseSwing()
{
createFrame();
}
private void createFrame()
{
setTitle("TestKlasseSwing");
setResizable(false);
addWindowListener(this);
addMouseListener(this);
//Center Frame
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setSize(new java.awt.Dimension(frameWidth, frameHeight));
//setLocation( (screenSize.width-this.getWidth()) / 2, (screenSize.height - this.getHeight()) / 2);
}
public void windowOpened(WindowEvent e){}
public void windowClosing(WindowEvent e){ dispose(); System.exit(0);}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{
new JPop(this, "MyPop", e.getX(), e.getY());
}
}
