Java Reference
In-Depth Information
public static void main(String[] args) throws Exception {
new MyFrame().setVisible(true);
}
public MyFrame() throws Exception {
setDefaultCloseOperation(EXIT_ON_CLOSE);
ActionListener buttonHandler = new MyFrameActionListener();
JButton exitButton = new JButton("Exit");
exitButton.setActionCommand(EXIT_COMMAND);
exitButton.addActionListener(buttonHandler);
JRadioButton serverButton = new JRadioButton("Server");
serverButton.setActionCommand(SERVER_COMMAND);
serverButton.addActionListener(buttonHandler);
JRadioButton clientButton = new JRadioButton("Client");
clientButton.setActionCommand(CLIENT_COMMAND);
clientButton.addActionListener(buttonHandler);
ButtonGroup clientServerGroup = new ButtonGroup();
clientServerGroup.add(serverButton);
clientServerGroup.add(clientButton);
JPanel clientServerPanel = new JPanel();
clientServerPanel.add(serverButton, BorderLayout.NORTH);
clientServerPanel.add(clientButton, BorderLayout.SOUTH);
this.add(clientServerPanel, BorderLayout.CENTER);
this.add(exitButton, BorderLayout.SOUTH);
pack();
}
private class MyFrameActionListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
if (EXIT_COMMAND.equals(ae.getActionCommand())) {
System.exit(0);
} else if (SERVER_COMMAND.equals(ae.getActionCommand())) {
System.out.println("Server selected");
} else if (CLIENT_COMMAND.equals(ae.getActionCommand())) {
System.out.println("Client selected");
}
}
}
}
Search WWH ::




Custom Search