我一直关注Oracle Java文档网站上的How to Print Text教程。但是,当我尝试将一些代码实现到我的代码中时,我收到了一个错误。
javac PrintableForms.java
PrintableForms.java:165: error: incompatible types: PrintableForms cannot be converted to Component
JOptionPane.showMessageDialog(this, msg, "Printing", type);
我的代码是
import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.print.PrinterException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.*;
import java.text.MessageFormat;
import javax.xml.transform.Source;
public class PrintableForms
{
JFrame myMainWindow = new JFrame("This is my title");
JPanel firstPanel = new JPanel();
JButton btnDoc1 = new JButton();
JButton btnP1 = new JButton();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String []fontFamilies = ge.getAvailableFontFamilyNames();
Font FontT5 = new Font("SansSerif", Font.BOLD, 50);
///////////
JCheckBox backgroundCheck = new JCheckBox();
JCheckBox interactiveCheck = new JCheckBox();
JTextArea text = new JTextArea();
JTextField headerField = new JTextField();
JTextField footerField = new JTextField();
public void runGUI()
{
myMainWindow.setBounds(10, 10, 1200, 500);
myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setLayout(new GridLayout(1,1));
createFirstPanel();
myMainWindow.getContentPane().add(firstPanel);
myMainWindow.setVisible(true);
load(text, "Athlete.txt");
}
public void createFirstPanel()
{
firstPanel.setLayout(null);
btnDoc1.setLocation(10,120);
btnDoc1.setSize(900,50);
btnDoc1.setText("Update Personal Information");
btnDoc1.setFont(FontT5);
firstPanel.add(btnDoc1);
btnP1.setLocation(910,120);
btnP1.setSize(250,50);
btnP1.setText("Print");
btnP1.setFont(FontT5);
btnP1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
print(evt);
}
});
firstPanel.add(btnP1);
footerField.setText("Page {0}");
headerField.setText("Athlete Form");
backgroundCheck.setSelected(true);
interactiveCheck.setSelected(true);
}
private void load(JTextArea comp, String fileName) {
try {
comp.read(
new InputStreamReader(
getClass().getResourceAsStream(fileName)),
null);
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void print(java.awt.event.ActionEvent evt)
{
MessageFormat header = createFormat(headerField);
MessageFormat footer = createFormat(footerField);
boolean interactive = interactiveCheck.isSelected();
boolean background = backgroundCheck.isSelected();
PrintingTask task = new PrintingTask(header, footer, interactive);
if (background)
{
task.execute();
}
else
{
task.run();
}
}
private class PrintingTask extends SwingWorker<Object, Object>
{
private final MessageFormat headerFormat;
private final MessageFormat footerFormat;
private final boolean interactive;
private volatile boolean complete = false;
private volatile String message;
JTextArea text = new JTextArea();
public PrintingTask(MessageFormat header, MessageFormat footer, boolean interactive)
{
this.headerFormat = header;
this.footerFormat = footer;
this.interactive = interactive;
}
@Override
protected Object doInBackground() {
try {
complete = text.print(headerFormat, footerFormat,
true, null, null, interactive);
message = "Printing " + (complete ? "complete" : "canceled");
} catch (PrinterException ex) {
message = "Sorry, a printer error occurred";
} catch (SecurityException ex) {
message =
"Sorry, cannot access the printer due to security reasons";
}
return null;
}
@Override
protected void done() {
message(!complete, message);
}
}
private MessageFormat createFormat(JTextField source) {
//String text = new Scanner( new File("Athlete.txt") ).useDelimiter("\\A").next();
String text = source.getText();
if (text != null && text.length() > 0) {
try {
return new MessageFormat(text);
} catch (IllegalArgumentException e) {
error("Sorry, this format is invalid.");
}
}
return null;
}
private void message(boolean error, String msg)
{
int type = (error ? JOptionPane.ERROR_MESSAGE :
JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(this, msg, "Printing", type);
}
private void error(String msg)
{
message(true, msg);
}
public static void main(String[] args)
{
PrintableForms pf = new PrintableForms();
pf.runGUI();
}
}
我试图这样做,当我点击一个按钮时,btnP1
在这种情况下,它会打开一个对话框,然后允许您打印预设文档。在此代码中,该文档是Athlete.txt,所有内容都是This is a test
我会帮助解决这个问题,谢谢
答案 0 :(得分:2)
您应该扩展组件类或创建组件类型对象并作为参数传递而不是 此 ,因为
JOptionPane.showMessageDialog(this, msg, "Printing", type);
第一个参数是组件类型,您将类型 PrintableForms 的参数传递为 此 。
答案 1 :(得分:2)
您可以传递框架对象,该对象是您想要显示对话框窗口时创建的主窗口。
如下所示。
JOptionPane.showMessageDialog(myMainWindow, msg, "Printing", type);
答案 2 :(得分:0)
尝试实施Printable
界面。