gpt4 book ai didi

java - 尝试创建打印功能时出错

转载 作者:行者123 更新时间:2023-12-01 18:16:06 25 4
gpt4 key购买 nike

我一直关注How to Print Text 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();
}
}

我试图获取它,因此当我单击按钮(在本例中为btnP1)时,它会打开一个对话框,然后允许您打印预设文档。在此代码中,该文档是 Athlete.txt,它读取的所有内容是 This is a test

我会帮助解决这个问题,谢谢

最佳答案

当您想要显示对话框窗口时,您可以传递框架对象,它是您创建的主窗口。

如下所示。

JOptionPane.showMessageDialog(myMainWindow, msg, "Printing", type);

关于java - 尝试创建打印功能时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29413235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com