📄 networksettingsdialog.java
字号:
/* * NetworkSettingsDialog.java * * Created on October 29, 2003, 9:08 AM */package cs101.awt;import java.net.InetAddress;import java.net.UnknownHostException;import javax.swing.JOptionPane;/** * A dialog box that will allow the user to select wether the program should * run in standalone mode, client mode or server mode. It is typically * displayed before the program does anything, and in some cases it may be the * first window that the user sees. This class is partly * based on a similar class written by Ben Vandiver in March 2003, but has * been entirely re-coded from scratch. * * @author Patrick G. Heck (gus.heck@olin.edu) * @version $Id: NetworkSettingsDialog.java,v 1.2 2003/10/29 17:04:11 gus Exp $ */public class NetworkSettingsDialog extends javax.swing.JDialog { /** Run in standalone mode */ public static final int NETWORK_NONE = 0; /** Run as a client */ public static final int NETWORK_CLIENT = 1; /** Run as a server */ public static final int NETWORK_SERVER = 2; // default state is standalone private int mode = NETWORK_NONE; // toggles debugging output private static boolean debug; /** * Creates new form NetworkSettingsDialog. * * @param parent The parent frame, or null if this dialog applies * to the entire application, not just one frame. * @param modal If true, and parent is null, the entire application * will block until ok is pressed. If true and parent * is defined, then only the parent window will be blocked. * If false, no blocking will occur. */ public NetworkSettingsDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() {//GEN-BEGIN:initComponents java.awt.GridBagConstraints gridBagConstraints; modeButtons = new javax.swing.ButtonGroup(); jLabel3 = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); leftPanel = new javax.swing.JPanel(); standaloneRadioButton = new javax.swing.JRadioButton(); clientRadioButton = new javax.swing.JRadioButton(); serverRadioButton = new javax.swing.JRadioButton(); jPanel3 = new javax.swing.JPanel(); hostLabel = new javax.swing.JLabel(); hostTextField = new javax.swing.JTextField(); portLabel = new javax.swing.JLabel(); portTextField = new javax.swing.JTextField(); okButton = new javax.swing.JButton(); jLabel3.setText("jLabel3"); setTitle("Network Settings"); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { closeDialog(evt); } }); jPanel1.setLayout(new java.awt.GridBagLayout()); jPanel1.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(10, 10, 10, 10))); leftPanel.setLayout(new javax.swing.BoxLayout(leftPanel, javax.swing.BoxLayout.Y_AXIS)); leftPanel.setBorder(new javax.swing.border.TitledBorder("Mode")); standaloneRadioButton.setText(" Standalone"); standaloneRadioButton.setToolTipText("Run with no network features enabled"); modeButtons.add(standaloneRadioButton); standaloneRadioButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { standaloneRadioButtonActionPerformed(evt); } }); leftPanel.add(standaloneRadioButton); clientRadioButton.setText("Client"); clientRadioButton.setToolTipText("Run as a networked client"); modeButtons.add(clientRadioButton); clientRadioButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clientRadioButtonActionPerformed(evt); } }); leftPanel.add(clientRadioButton); serverRadioButton.setText("Server"); serverRadioButton.setToolTipText("Run as a networked server"); modeButtons.add(serverRadioButton); serverRadioButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { serverRadioButtonActionPerformed(evt); } }); leftPanel.add(serverRadioButton); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; jPanel1.add(leftPanel, gridBagConstraints); jPanel3.setLayout(new java.awt.GridBagLayout()); hostLabel.setText("Host:"); hostLabel.setToolTipText("The host to connect to"); hostLabel.setEnabled(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); jPanel3.add(hostLabel, gridBagConstraints); hostTextField.setText("localhost"); hostTextField.setToolTipText("Enter the host name here"); hostTextField.setPreferredSize(new java.awt.Dimension(100, 19)); hostTextField.setEnabled(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); jPanel3.add(hostTextField, gridBagConstraints); portLabel.setText("Port:"); portLabel.setToolTipText("The port to listen on or connect to"); portLabel.setEnabled(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); jPanel3.add(portLabel, gridBagConstraints); portTextField.setText("4321"); portTextField.setToolTipText("Enter the port number here"); portTextField.setPreferredSize(new java.awt.Dimension(100, 19)); portTextField.setEnabled(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); jPanel3.add(portTextField, gridBagConstraints); okButton.setText("OK"); okButton.setMargin(new java.awt.Insets(2, 28, 2, 28)); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridwidth = 2; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); jPanel3.add(okButton, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0); jPanel1.add(jPanel3, gridBagConstraints); getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); pack(); }//GEN-END:initComponents private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed // Add your handling code here: if (!validateInput()) return; this.hide(); System.out.println("getMode() will return: " + getMode()); if (debug) { try { System.out.println("getHost() will return: "+ getHost()); } catch (RuntimeException re) { System.out.println("getHost() will throw " + re); } try { System.out.println("getPort() will return: " + getPort()); } catch (RuntimeException re) { System.out.println("getPort() will throw " + re); } } }//GEN-LAST:event_okButtonActionPerformed private void standaloneRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_standaloneRadioButtonActionPerformed // Add your handling code here: mode = NETWORK_NONE; hostLabel.setEnabled(false); hostTextField.setEnabled(false); portLabel.setEnabled(false); portTextField.setEnabled(false); }//GEN-LAST:event_standaloneRadioButtonActionPerformed private void clientRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clientRadioButtonActionPerformed // Add your handling code here: mode = NETWORK_CLIENT; hostLabel.setEnabled(true); hostTextField.setEnabled(true); portLabel.setEnabled(true); portTextField.setEnabled(true); }//GEN-LAST:event_clientRadioButtonActionPerformed private void serverRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverRadioButtonActionPerformed // Add your handling code here: mode = NETWORK_SERVER; hostLabel.setEnabled(false); hostTextField.setEnabled(false); portLabel.setEnabled(true); portTextField.setEnabled(true); }//GEN-LAST:event_serverRadioButtonActionPerformed /** Closes the dialog */ private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog setVisible(false); dispose(); }//GEN-LAST:event_closeDialog private boolean validateInput() { try { if (this.mode == NETWORK_CLIENT) { Integer.parseInt(portTextField.getText()); InetAddress.getByName(hostTextField.getText()); } else if (this.mode == NETWORK_SERVER) Integer.parseInt(portTextField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this,"Non-numeric value for port!"); return false; } catch (UnknownHostException e) { JOptionPane.showMessageDialog(this,"Unknown host: " + hostTextField.getText()); return false; } return true; } /** * Returns the requested state of network connectivity * * @return An integer constant drawn from NETWORK_NONE, * NETWORK_CLIENT, or NETWORK_SERVER. */ public int getMode() { return this.mode; } /** * returns the requested port. The text box the value is drawn from * depends on the state. * * @return port number to serve on / connect to. * @throws RuntimeException if the state was NETWORK_NONE. */ public int getPort() { if (mode == NETWORK_NONE) { throw new RuntimeException("Invalid Network settings for getPort()"); } return Integer.parseInt(portTextField.getText()); } /** * returns the name of the host to connect to as a client. * * @return String name of host to connect to. * @throws RuntimeException if the state was not NETWORK_CLIENT. */ public String getHost() { if (this.mode == NETWORK_CLIENT) return this.hostTextField.getText(); else throw new RuntimeException("Invalid Network settings for getHost()"); } /** * Main method for visual layout inspection. * * @param args the command line arguments */ public static void main(String args[]) { debug = true; NetworkSettingsDialog d = new NetworkSettingsDialog(new javax.swing.JFrame(), true); d.show(); System.exit(0); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JRadioButton clientRadioButton; private javax.swing.JLabel hostLabel; private javax.swing.JTextField hostTextField; private javax.swing.JLabel jLabel3; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel3; private javax.swing.JPanel leftPanel; private javax.swing.ButtonGroup modeButtons; private javax.swing.JButton okButton; private javax.swing.JLabel portLabel; private javax.swing.JTextField portTextField; private javax.swing.JRadioButton serverRadioButton; private javax.swing.JRadioButton standaloneRadioButton; // End of variables declaration//GEN-END:variables }/* * $Log: NetworkSettingsDialog.java,v $ * Revision 1.2 2003/10/29 17:04:11 gus * add a title to the dialog * * Revision 1.1 2003/10/29 15:55:50 gus * An initial version of a rewrite of the network settings dialog from breakout * spring 2003. * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -