📄 pollingclientframe.java
字号:
package com.ora.rmibook.chapter21.printer.applications;
import com.ora.rmibook.chapter21.printer.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class PollingClientFrame extends JFrame implements NetworkConstants {
private JTextArea _messageBox;
private JButton _chooseFileButton;
private JButton _printFileButton;
private JFileChooser _fileChooser;
public PollingClientFrame() {
buildGUI();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
addWindowListener(new ExitOnClose());
setSize(250, 200);
}
private void buildGUI() {
JPanel mainPanel = new JPanel(new BorderLayout());
_messageBox = new JTextArea();
mainPanel.add(new JScrollPane(_messageBox), BorderLayout.CENTER);
createButtons();
JPanel buttonHolder = new JPanel(new GridLayout(1, 2));
buttonHolder.add(_chooseFileButton);
buttonHolder.add(_printFileButton);
mainPanel.add(buttonHolder, BorderLayout.SOUTH);
getContentPane().add(mainPanel);
}
private void createButtons() {
_chooseFileButton = new JButton("Choose File");
_chooseFileButton.addActionListener(new FindFile());
_printFileButton = new JButton("Print File");
_printFileButton.addActionListener(new PrintFile());
}
private class ExitOnClose extends WindowAdapter {
public void windowClosed(WindowEvent event) {
System.exit(0);
}
}
private class FindFile implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (null == _fileChooser) {
_fileChooser = new JFileChooser();
}
if (JFileChooser.APPROVE_OPTION == _fileChooser.showOpenDialog(PollingClientFrame.this)) {
_messageBox.setText((_fileChooser.getSelectedFile()).getAbsolutePath());
}
}
}
private class PrintFile implements ActionListener, Runnable {
private PollingPrinter _printer;
private String _key;
public void actionPerformed(ActionEvent event) {
new Thread(this).start();
}
public void run() {
try {
makeInitialRequest();
pollPrinter();
} catch (PrinterException printerException) {
SwingUtilities.invokeLater(new PrinterExceptionMessage(printerException));
return;
} catch (Exception exception) {
SwingUtilities.invokeLater(new ExceptionMessage(exception));
return;
}
SwingUtilities.invokeLater(new SuccessMessage());
}
private void makeInitialRequest() throws PrinterException, Exception {
FileInputStream documentStream = new FileInputStream(_fileChooser.getSelectedFile());
DocumentDescription documentDescription = new DocumentDescription(documentStream);
_printer = (PollingPrinter) Naming.lookup(DEFAULT_PRINTER_NAME);
_key = _printer.printDocument(documentDescription);
}
private void pollPrinter() throws PrinterException, Exception {
while (false == _printer.isDocumentDone(_key)) {
try {
Thread.sleep(2000);
} catch (InterruptedException ignored) {
}
}
}
}
private class PrinterExceptionMessage implements Runnable {
private PrinterException _printerException;
public PrinterExceptionMessage(PrinterException printerException) {
_printerException = printerException;
}
public void run() {
String errorMessage = "Print failed after " + _printerException.getNumberOfPagesPrinted() + " pages.";
JOptionPane.showMessageDialog(PollingClientFrame.this,
errorMessage, "Error in printing", JOptionPane.INFORMATION_MESSAGE);
_messageBox.setText("Exception attempting to print " + (_fileChooser.getSelectedFile()).getAbsolutePath() +
"\n\t Error was: " + _printerException.getHumanReadableErrorDescription());
}
}
private class ExceptionMessage implements Runnable {
private Exception _exception;
public ExceptionMessage(Exception exception) {
_exception = exception;
}
public void run() {
JOptionPane.showMessageDialog(PollingClientFrame.this,
"Print failed", "Error in printing", JOptionPane.INFORMATION_MESSAGE);
_messageBox.setText("Exception attempting to print " + (_fileChooser.getSelectedFile()).getAbsolutePath() +
"\n\t Error was: " + _exception.toString());
_exception.printStackTrace();
}
}
private class SuccessMessage implements Runnable {
public void run() {
JOptionPane.showMessageDialog(PollingClientFrame.this, "Success!", "document has been printed",
JOptionPane.INFORMATION_MESSAGE);
_messageBox.setText("Print Request succeeded.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -