gpt4 book ai didi

JAVA:JFrame 变为黑色后的屏幕截图

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:23 25 4
gpt4 key购买 nike

我编写了一个程序,通过单击JMenuItem 来制作JFrame 的屏幕截图。如果我只在 Eclipse 中运行 .java 文件,一切正常,并且屏幕截图完美地显示了 JFrame。但是,如果我将 JFrame 作为另一个 JFrame 的链接打开,则屏幕截图为黑色,而不是显示 JFrame。这是我的代码:

JFrame1.java:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;


public class JFrame1 extends JFrame {

static JFrame1 frame1 = new JFrame1();

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

public void run() {
try {
frame1.setVisible(true);

} catch (Exception e) {
e.printStackTrace();
}

}
});
}

public void CloseFrame(){
super.dispose();
}

public JFrame1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(50, 50, 800, 740);

JButton ok = new JButton("OK");
getContentPane().add(ok);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CloseFrame();
JFrame2 frame2 = new JFrame2();
frame2.setVisible(true);
}
});
}
}

通过按钮(确定)我可以转到 JFrame2.java。

JFrame2.java:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class JFrame2 extends JFrame {

static JFrame2 frame2 = new JFrame2();

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

public void run() {
try {
frame2.setVisible(true);

} catch (Exception e) {
e.printStackTrace();
}

}
});
}

private void createMenuBar() {

JMenuBar menubar = new JMenuBar();

JMenu file = new JMenu("File");

JMenuItem screen = new JMenuItem("Screenshot");
screen.addActionListener(new AbstractAction() {

@Override
public void actionPerformed (ActionEvent e)
{
Dimension size = frame2.getSize ();
BufferedImage img = new BufferedImage (size.width, size.height, BufferedImage.TYPE_3BYTE_BGR);
Graphics g = img.getGraphics ();
frame2.printAll (g);
g.dispose ();
try
{
ImageIO.write (img, "png", new File ("screenshot.png"));
}
catch (IOException ex)
{
ex.printStackTrace ();
}
}
});

file.add(screen);
menubar.add(file);
setJMenuBar(menubar);
}

public JFrame2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(50, 50, 800, 740);

createMenuBar();
}

}

如果我现在点击“屏幕截图”,它只是黑色。如果我只运行 JFrame2.java 而不运行 JFrame1.java 之前,则会保存真实图像。为什么从一个 JFrame1 转到 JFrame2 后屏幕截图是黑色的?

最佳答案

你从错误的框架中绘画......

在你的第一帧中,你正在这样做......

JFrame2 frame2 = new JFrame2();
frame2.setVisible(true);

看起来很无害,但是,在 JFrame2 中你正在这样做......

public class JFrame2 extends JFrame {

static JFrame2 frame2 = new JFrame2();

还有...

public void actionPerformed (ActionEvent e)
{
Dimension size = frame2.getSize ();
BufferedImage img = new BufferedImage (size.width, size.height, BufferedImage.TYPE_3BYTE_BGR);
Graphics g = img.getGraphics ();
frame2.printAll (g);
g.dispose ();
try
{
ImageIO.write (img, "png", new File ("screenshot.png"));
}
catch (IOException ex)
{
ex.printStackTrace ();
}
}

但是,frame2(在 JFrame2 内部)在屏幕上不可见。

这就是为什么 static 是邪恶的,应该避免。这也是为什么您不应该直接从 JFrame 之类的东西扩展。您很容易陷入不知道屏幕上实际显示的内容以及您引用的内容的困境......

例如...

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class JavaApplication254 {

public static void main(String[] args) {
new JavaApplication254();
}

public JavaApplication254() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JButton btn = new JButton("Click me away...");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

TestPane testPane = new TestPane();
SnapshotAction snapshotAction = new SnapshotAction(testPane);

JMenuBar mb = new JMenuBar();
JMenu mnuFile = new JMenu("File");
mnuFile.add(snapshotAction);
mb.add(mnuFile);

JFrame frame = new JFrame("More Testing");
frame.setJMenuBar(mb);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(testPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(btn);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setBorder(new EmptyBorder(20, 20, 20, 20));
JLabel label = new JLabel("I be a bananan");
label.setOpaque(true);
label.setBackground(Color.YELLOW);
label.setForeground(Color.RED);
label.setBorder(
new CompoundBorder(
new LineBorder(Color.RED),
new EmptyBorder(20, 20, 20, 20)));
setLayout(new GridBagLayout());
add(label);
}

}

public class SnapshotAction extends AbstractAction {

private JComponent parent;

public SnapshotAction(JComponent parent) {
this.parent = parent;
putValue(NAME, "Take Snapshot...");
}

@Override
public void actionPerformed(ActionEvent e) {
if (parent.isDisplayable()) {

BufferedImage img = new BufferedImage(parent.getWidth(), parent.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
parent.printAll(g2d);
g2d.dispose();

try {
ImageIO.write(img, "png", new File("Snapshot.png"));
Toolkit.getDefaultToolkit().beep();
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(parent, "Failed to generate snapshot: " + ex.getMessage());
}

}
}

}

}

这将输出...

enter image description here

关于JAVA:JFrame 变为黑色后的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29043568/

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