gpt4 book ai didi

java - 打印预览(打包不起作用)

转载 作者:行者123 更新时间:2023-12-01 09:35:29 30 4
gpt4 key购买 nike

我有一个由 JPanelJDialog 制作的打印预览,运行时没有错误,但在 pack(); 之后不显示。

我知道它有点长,但你可以跳到 PrintPreview 方法,其他人只是为了了解我如何控制的更多细节。

请帮忙,我不知道为什么它没有显示在屏幕上。 任何人都请帮忙,非常感谢。 ^_^

private PageFormat pf;
private Book pg;
private int pageIndex;
private int totalPages;
private double scaler;

private String title;
private JTextField pageInput;
private JTextField zoomInput;
private PreviewPanel pp;
private JLabel statusBar;

从这里开始

public void PrintPreview(String title, Book pg, int pageIndex) {
this.title = title;
setTitle(title + " - Print Preview");
scaler = .5;
this.pg = pg;
this.pageIndex = pageIndex;
this.totalPages = pg.getNumberOfPages();
pf = pg.getPageFormat(pageIndex);
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
setLayout(new BorderLayout());
pp = new PreviewPanel();
add(new JScrollPane(pp), BorderLayout.CENTER);

JPanel topBar = new JPanel();
topBar.setLayout(new BorderLayout());
add(topBar, BorderLayout.NORTH);
JPanel leftTop = new JPanel();
JPanel rightTop = new JPanel();
topBar.add(leftTop, BorderLayout.WEST);
topBar.add(rightTop, BorderLayout.EAST);

leftTop.add(new JLabel("Page:"));
Insets pageButtonInsets = new Insets(0, 2, 0, 2);
JButton pageBack = new JButton("<");
pageBack.setMargin(pageButtonInsets);
pageBack.setActionCommand("back");
pageBack.addActionListener(this);
JButton pageForward = new JButton(">");
pageForward.setMargin(pageButtonInsets);
pageForward.setActionCommand("forward");
pageForward.addActionListener(this);
pageInput = new JTextField(Integer.toString(pageIndex+1));
pageInput.setPreferredSize(new Dimension(30, 20));
pageInput.setActionCommand("page");
pageInput.addActionListener(this);
leftTop.add(pageBack);
leftTop.add(pageInput);
leftTop.add(pageForward);
rightTop.add(new JLabel("Zoom:"));
zoomInput = new JTextField((int)(scaler*100)+"%");
zoomInput.setPreferredSize(new Dimension(30, 20));
rightTop.add(zoomInput);
JButton zoomSet = new JButton("Set");
zoomSet.setMargin(new Insets(0, 0, 0, 0));
zoomSet.setPreferredSize(new Dimension(30, 20));
rightTop.add(zoomSet);
zoomSet.setActionCommand("zoom");
zoomSet.addActionListener(this);

JPanel lowerBar = new JPanel();
lowerBar.setLayout(new BorderLayout());
lowerBar.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
statusBar = new JLabel("Ag"); // temporary label for setting size of status bar
lowerBar.add(statusBar, BorderLayout.CENTER);
add(lowerBar, BorderLayout.SOUTH);

JButton license = new JButton("<html>&copy;</html>");
license.setActionCommand("license");
license.setMargin(new Insets(0, 0, 0, 0));
license.addActionListener(this);
lowerBar.add(license, BorderLayout.WEST);

JButton print = new JButton("Print...");
print.setActionCommand("print");
print.setMargin(new Insets(4, 0, 4, 0));
print.addActionListener(this);
lowerBar.add(print, BorderLayout.EAST);
pack();
//setVisible(true);
}

到此结束

private void updateStatusBar() {
updateStatusBar("Page " + (pageIndex+1) + " of " + totalPages);
}
private void updateStatusBar(String s) {
updateStatusBar(s, Color.black);
}
private void updateStatusBar(String s, Color c) {
statusBar.setText(s);
statusBar.setForeground(c);
}

private void print() {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPageable(pg);
pj.setJobName(title);
if (pj.printDialog()) {
try {
pj.print();
} catch (PrinterException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Could not print.\nSee the Java error console for details.", "error", JOptionPane.ERROR_MESSAGE);
}
}
}

private class PreviewPanel extends JPanel {
private static final long serialVersionUID = 1L;

public PreviewPanel() {
resetPreferredSize();
}

public void resetPreferredSize() {
setPreferredSize(new Dimension((int)(pf.getWidth()*scaler), (int)(pf.getHeight()*scaler)));
}

public void paintComponent(Graphics g1) {
Graphics2D g = (Graphics2D)g1;
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.white);
g.fillRect(0, 0, (int)(pf.getWidth()*scaler), (int)(pf.getHeight()*scaler));
g.setColor(Color.black);
try {
g.scale(scaler, scaler);
pg.getPrintable(pageIndex).print(g, pf, pageIndex);
updateStatusBar();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
g.setFont(new Font("Helvetica", Font.ITALIC, 30));
g.drawString("Page index out of range", 10, 30);
updateStatusBar("Page index out of range", Color.red);
} catch (PrinterException e) {
e.printStackTrace();
g.setFont(new Font("Helvetica", Font.ITALIC, 30));
g.drawString("Printer error", 10, 30);
updateStatusBar("Printer error", Color.red);
}
}
}

@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getActionCommand().equals("page")) {
// change page
try {
pageIndex = Integer.parseInt(pageInput.getText())-1;
pp.repaint();
} catch (NumberFormatException e) {
// do nothing if the entry is not a number
}
} else if (arg0.getActionCommand().equals("back")) {
if (pageIndex > 0) {
pageIndex--;
pageInput.setText(Integer.toString(pageIndex+1));
pp.repaint();
}
} else if (arg0.getActionCommand().equals("forward")) {
if (pageIndex < totalPages - 1) {
pageIndex++;
pageInput.setText(Integer.toString(pageIndex+1));
pp.repaint();
}
} else if (arg0.getActionCommand().equals("zoom")) {
// change zoom setting
try {
scaler = Double.parseDouble(zoomInput.getText().replaceAll("%", ""))// 100.0;
pp.resetPreferredSize();
pp.revalidate();
pp.repaint();
} catch (NumberFormatException e) {
// do nothing if the entry is not a number
}
} else if (arg0.getActionCommand().equals("license")) {
JOptionPane.showMessageDialog(this, LICENSE);
} else if (arg0.getActionCommand().equals("print")) {
print();
}
}

最佳答案

是的,我明白了。哈哈哈哈实际上它确实弹出了,但是在其他前台应用程序后面......哈哈哈终于完成了。

关于java - 打印预览(打包不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38989955/

30 4 0
文章推荐: java - sbt 在启动时抛出错误
文章推荐: stringr 从字符串中提取完整数字
文章推荐: actionscript-3 - 使用 Vector. 代替标准数组有什么优势吗?