JButton将不会显示

多纳吉

我使用Robot类创建了一个简单的程序,该程序将生成您计算机的屏幕截图。当您单击JButton时,它将创建屏幕截图。

我试图使截屏时JFrame消失。不幸的是,JButton无法显示...您能告诉我我的代码有什么问题吗?

package Threads;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ScreenCapture extends JFrame implements ActionListener{

private JButton b;

public ScreenCapture() throws Exception{
    this.setTitle("Etfeld");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setVisible(true);
    this.pack();
    this.setResizable(false);
    this.setLocation(1366/2-100,678/2);

    ImageIcon jamil=new ImageIcon("Logo.png");
    Image logo=jamil.getImage();
    this.setIconImage(logo);

    JPanel jp=new JPanel();
    b=new JButton("Capture!");
    b.addActionListener(this);
    this.add(jp);
    jp.add(b);

}

@Override
public void actionPerformed(ActionEvent ae) {
    Object obj=ae.getSource();
    if(obj instanceof JButton){
        try {
            Robot robot = new Robot();
            this.setVisible(false);
            BufferedImage im=robot.createScreenCapture(new Rectangle(0,0,1366,768));

                    Toolkit.getDefaultToolkit().beep();
                    this.setVisible(true);
                    File outputfile = new File("saved.png");
                    ImageIO.write(im, "png", outputfile);
                } catch (Exception v) {v.printStackTrace();}
    }

}

public static void main(String []args) throws Exception{
    ScreenCapture sc=new ScreenCapture();
}
}
SO用户

您需要重新安排对方法的调用。您应该pack()在使框架可见之前致电添加所有UI组件后。
然后,使用setVisible(true)使其JFrame可见。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

SVG将不会显示

来自分类Dev

UIPickerView将不会显示

来自分类Dev

QWinTaskbarProgress将不会显示

来自分类Dev

JButton将不显示背景色

来自分类Dev

Winforms控件将不会显示

来自分类Dev

UIActivity将不会显示图像?

来自分类Dev

Winforms控件将不会显示

来自分类Dev

轮播Glyphicons将不会显示

来自分类Dev

背景色将不会应用于JButton

来自分类Dev

背景色将不会应用于JButton

来自分类Dev

空div(样式:高度)将不会显示

来自分类Dev

Django表单字段将不会显示

来自分类Dev

类库中的ImageSource将不会显示

来自分类Dev

div的内部包装div将不会显示

来自分类Dev

按钮将不会显示在FlowLayout中

来自分类Dev

JScrollPane中的JTextArea将不会显示

来自分类Dev

JPanels将不会显示在Cardlayout容器中

来自分类Dev

JScrollPane将不会显示在JTextArea中

来自分类Dev

UIAlertController将不会显示-在Swift中

来自分类Dev

Dygraph将不会显示在Bootstrap卡中

来自分类Dev

CSS将不会显示背景图像?

来自分类Dev

Ajax将不会显示在网页上

来自分类Dev

ProgressDialog将不会显示-多线程

来自分类Dev

AlertDialog将不会显示在onCreate方法中

来自分类Dev

JScrollPane中的JTextArea将不会显示

来自分类Dev

AngularJS HTTP Get JSON将不会显示

来自分类Dev

Django表单字段将不会显示

来自分类Dev

Android-AlertDialog将不会显示BitmapDrawable

来自分类Dev

UITableViewCell中的ImageView将不会显示