📄 counterwindow.java
字号:
/*
* CounterWindow.java E.L. 2001-08-25
*
* This window is used as input window at the client side.
* The connection to the server has to be established already.
* It's disconnected when the user closes the window.
*/
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
import java.rmi.server.*;
import java.rmi.*;
class CounterWindow extends JFrame implements Constants {
private JTextField number = new JTextField(8);
private JRadioButton yesButton = new JRadioButton("Yes Votes", true);
private JRadioButton noButton = new JRadioButton("No Votes", false);
private JButton saveButton = new JButton("Save");
private YesNoCounterFront counter;
private Client theClient;
public CounterWindow(YesNoCounterFront initCounter, Client initClient) {
try {
setTitle("Window for Client " + initClient.getName());
addWindowListener(new WindowsClosingListener());
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
counter = initCounter;
theClient = initClient;
Container guiContainer = getContentPane();
guiContainer.add(new JLabel(), BorderLayout.NORTH);
guiContainer.add(new InputPanel(), BorderLayout.CENTER);
guiContainer.add(saveButton, BorderLayout.SOUTH);
ButtonListener buttonListener = new ButtonListener();
saveButton.setMnemonic('S');
saveButton.addActionListener(buttonListener);
number.requestFocus();
} catch (Exception e) {
System.out.println("Error in the CounterWindow constructor: " + e);
}
}
/* Describes the middle panel */
private class InputPanel extends JPanel {
public InputPanel() {
setLayout(new GridLayout(2, 2));
add(new JLabel("No. of Votes: "));
add(number);
ButtonGroup group = new ButtonGroup();
group.add(yesButton);
group.add(noButton);
add(yesButton);
add(noButton);
yesButton.setMnemonic('J');
noButton.setMnemonic('N');
SoftBevelBorder border = new SoftBevelBorder(BevelBorder.RAISED);
Border box = BorderFactory.createTitledBorder(border, "Enter Votes");
setBorder(box);
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
int noOfVotes = 0;
try {
noOfVotes = Integer.parseInt(number.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid Number");
number.requestFocus();
}
try {
if (yesButton.isSelected()) counter.increaseNumberOfYes(noOfVotes);
else counter.increaseNumberOfNo(noOfVotes);
} catch (Exception e) {
System.out.println("Error in the listener to the Save button: " + e);
}
number.setText("");
number.requestFocus();
}
}
private class WindowsClosingListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
System.out.println("Tries Resigning");
String name = "";
try {
name = theClient.getName();
counter.resignMe(name);
} catch (Exception e) {
System.out.println("Error in WindowsClosingListener: " + e);
}
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -