Java 尝试创建打印函数时出错

Java 尝试创建打印函数时出错,java,swing,printing,Java,Swing,Printing,我一直在关注Oracle Java文档网站上的教程。然而,当我试图在代码中实现一些代码时,我得到了一个错误 javac PrintableForms.java PrintableForms.java:165: error: incompatible types: PrintableForms cannot be converted to Component JOptionPane.showMessageDialog(this, msg, "Printing", type); 我的代码是 imp

我一直在关注Oracle Java文档网站上的教程。然而,当我试图在代码中实现一些代码时,我得到了一个错误

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();
    }
}
import javax.swing.*;
导入java.awt.*;
导入java.io.*;
导入javax.swing.*;
导入java.awt.print.PrinterException;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入javax.swing.*;
导入java.text.MessageFormat;
导入javax.xml.transform.Source;
公共类可打印表单
{
JFrame myMainWindow=新JFrame(“这是我的标题”);
JPanel firstPanel=新的JPanel();
JButton btnDoc1=新JButton();
JButton btnP1=新JButton();
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
字符串[]fontFamilies=ge.getAvailableFontFamilyNames();
Font FONT5=新字体(“SansSerif”,Font.BOLD,50);
///////////
JCheckBox backgroundCheck=新的JCheckBox();
JCheckBox interactiveCheck=新的JCheckBox();
JTextArea text=新的JTextArea();
JTextField headerField=新的JTextField();
JTextField footerField=新的JTextField();
公营机构
{
myMainWindow.setBounds(10,10,1200,500);
myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(新的GridLayout(1,1));
createFirstPanel();
myMainWindow.getContentPane().add(第一个面板);
myMainWindow.setVisible(true);
加载(文本,“atternate.txt”);
}
public void createFirstPanel()
{
firstPanel.setLayout(null);
btnDoc1.设置位置(10120);
btnDoc1.设置尺寸(900,50);
btnDoc1.setText(“更新个人信息”);
btnDoc1.setFont(FontT5);
第一个面板。添加(btnDoc1);
btnP1.设置位置(910120);
btnP1.设置尺寸(250,50);
btnP1.setText(“打印”);
btnP1.setFont(FontT5);
btnP1.addActionListener(新的java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
打印(evt);
}
});
第一个面板。添加(btnP1);
setText(“第{0}页”);
headerField.setText(“运动员形态”);
背景检查。选择设置(正确);
interactiveCheck.setSelected(true);
}
私有无效加载(JTextArea comp,字符串文件名){
试一试{
综合阅读(
新的InputStreamReader(
getClass().getResourceAsStream(文件名)),
无效);
}捕获(IOEX异常){
例如printStackTrace();
}
}
私有无效打印(java.awt.event.ActionEvent evt)
{
MessageFormat header=createFormat(headerField);
MessageFormat footer=createFormat(footer字段);
boolean interactive=interactiveCheck.isSelected();
布尔背景=backgroundCheck.isSelected();
PrintingTask任务=新的PrintingTask(页眉、页脚、交互);
如果(背景)
{
task.execute();
} 
其他的
{
task.run();
}
}
私有类PrintingTask扩展SwingWorker
{
私人最终消息格式:headerFormat;
私有最终消息格式;
私人最终布尔交互;
private volatile boolean complete=false;
私有易失性字符串消息;
JTextArea text=新的JTextArea();
公共打印任务(MessageFormat页眉、MessageFormat页脚、布尔交互式)
{
this.headerFormat=收割台;
this.footerFormat=页脚;
这是交互式的;
}
@凌驾
受保护对象doInBackground(){
试一试{
完成=文本。打印(页眉格式、页脚格式、,
true、null、null、交互);
message=“打印”+(完成?“完成”:“已取消”);
}捕获(打印例外){
message=“对不起,发生打印机错误”;
}catch(SecurityException-ex){
信息=
“对不起,由于安全原因,无法访问打印机”;
}
返回null;
}
@凌驾
受保护的void done(){
消息(!完成,消息);
}
}
private MessageFormat createFormat(JTextField源){
//字符串文本=新扫描仪(新文件(“atternate.txt”)。使用分隔符(\\A”)。下一步();
String text=source.getText();
if(text!=null&&text.length()>0){
试一试{
返回新的消息格式(文本);
}捕获(IllegalArgumentException e){
错误(“对不起,此格式无效。”);
}
}
返回null;
}
私有无效消息(布尔错误,字符串消息)
{
int type=(错误?JOptionPane.error\u消息:
JOptionPane.INFORMATION(信息和消息);
showMessageDialog(这个,消息,“打印”,类型);
}
私有无效错误(字符串消息)
{
消息(true,msg);
}
公共静态void main(字符串[]args)
{
PrintableForms pf=新的PrintableForms();
pf.runGUI();
}
}
我正在尝试获取它,因此当我单击按钮时,
btnP1
在本例中,它会打开一个对话框,然后允许您打印预设文档。在这段代码中,该文档是athletor.txt,它读取的是
这是一个测试


我想帮你解决这个问题,谢谢

你应该扩展组件类或者创建组件类型对象并作为参数传递,而不是这个,因为

JOptionPane.showMessageDialog(这个,消息,“打印”,类型)

第一个参数为组件类型和y
JOptionPane.showMessageDialog(myMainWindow, msg, "Printing", type);