[SOLVED] optionwindow
by , 12-26-2011 at 07:20 PM (2243 Views)
Hello I have created an optionwindow for my chat program, but if i open the option window and click the cross in the right corner, not only the optionwindow terminates but also the chatprogram. Can someone tell what i can do about it, and are there other ways to make a kind of optionwindow?
thanks keffie91
this is the source:
ChatProgram.java
OptionWindow.javaJava Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import java.net.*; import java.io.*; public class ChatProgram extends JFrame implements ActionListener{ JMenuBar mbar = new JMenuBar(); JMenu options = new JMenu("Options"); JMenuItem dcm = new JMenuItem("Disconnect"); JMenuItem optm = new JMenuItem("Options"); JPanel toppanel = new JPanel(); JButton disconnect = new JButton("Disconnect"); // disconnectbutton JTextField username = new JTextField("Anonymous"); // username JLabel usertxt = new JLabel("Username:"); // username label JTextArea mess = new JTextArea(); // display the messages JTextField write = new JTextField(); // write a new message MulticastSocket so; // MulticastSocket InetAddress iadr; // multicast adress int port; //port to listen to public ChatProgram(String groupAddr, int portNo) throws IOException{ // The parameters iadr = InetAddress.getByName(groupAddr); port = portNo; // create socket and start communication so = new MulticastSocket(port); so.joinGroup(iadr); new Receiver(so, mess); sendMessage("Connected"); setJMenuBar(mbar); mbar.add(options); options.add(optm); options.add(dcm); getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS)); getContentPane().add(toppanel); getContentPane().add(disconnect); getContentPane().add(mess); getContentPane().add(write); toppanel.setLayout(new GridLayout(1,2)); toppanel.add(usertxt);toppanel.add(username); mess.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); mess.setEditable(false); write.addActionListener(this); disconnect.addActionListener(this); dcm.addActionListener(this); optm.addActionListener(this); setVisible(true); setResizable(false); setSize(500,400); setTitle("ChatProgram"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e){ if(e.getSource() == optm){ // open a new OptionWindow new OptionWindow(); } if(e.getSource() == write){ // The user has written the message sendMessage(write.getText()); write.setText(""); } if(e.getSource() == disconnect || e.getSource() == dcm){ // The user has pressed the disconnect button sendMessage("Disconnected"); try{ so.leaveGroup(iadr); } catch(IOException ie){ mess.append("Cannot disconnect \n"); } so.close(); dispose(); System.exit(0); } } private void sendMessage(String s){ //convert the message to an array of bytes byte[] data = ( username.getText() + ": " + s).getBytes(); //create and send the datagram packet DatagramPacket packet = new DatagramPacket(data, data.length, iadr, port); try{so.send(packet);}catch(IOException e){ mess.append("something went wrong");} } public static void main(String[] args) throws IOException { JFrame.setDefaultLookAndFeelDecorated(true); new ChatProgram("235.110.110.1",16645); } } class Receiver implements Runnable{ Thread activity = new Thread(this); MulticastSocket so; JTextArea txt; //constructor Receiver(MulticastSocket sock, JTextArea txtAr){ so = sock; txt = txtAr; activity.start(); } public void run(){ byte[] data = new byte[1024]; while (true) try{ //create datagram for reception DatagramPacket packet = new DatagramPacket(data, data.length); //wait for next message so.receive(packet); // convert the message to a string String mess = new String(data, 0, packet.getLength()); txt.append(mess + "\n"); // dusplay the message } catch(IOException e){ break; // occurs when so is closed } } }
Java Code:import java.awt.*; import javax.swing.*; public class OptionWindow extends JFrame { //JPanels to add the other components to JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); JPanel p4 = new JPanel(); JPanel p5 = new JPanel(); public OptionWindow(){ Container c = getContentPane(); c.setLayout(new GridLayout(5,2)); c.add(p1);c.add(p2);c.add(p3);c.add(p4);c.add(p5); setVisible(true); setSize(200,300); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args){ new OptionWindow(); } }
- Categories
- Uncategorized