0

我有一个程序可以做几件事并在几次操作后显示不同的 JFrame。当我从主类启动第一个 JFrame 时,一切正常,但是当我从与主类不同的另一个类启动它时,它不会出现。

重点是什么?我究竟做错了什么?

这是一些代码:

这是从主要调用的:

SwingUtilities.invokeLater(new Runnable() {
                PdfFileUtils pfu = new PdfFileUtils(path);

                public void run() {
                    try {
                        PdfToImg.setup(pfu, null);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });

它有效。

这是从另一个在某些操作后使用的类调用的:

pfu.setPath(SIGNED);

    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            try {
                PdfToImg.setup(pfu, data);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });

有时(每 4 或 5 次执行),它会启动中断异常。

我也尝试以这种方式启动第二帧:

 pfu.setPath(SIGNED);

 try {
    PdfToImg.setup(pfu, data);
 } catch (IOException ex) {
    ex.printStackTrace();
 }

但它会出现一秒钟然后消失。

编辑 :

这是 setup() 方法:

public static void setup(PdfFileUtils pfu, BiometricData data) throws IOException {

    // load a pdf from a byte buffer
    File file = new File(pfu.getPath());
    RandomAccessFile raf = new RandomAccessFile(file, "r");
    FileChannel channel = raf.getChannel();
    ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
            channel.size());
    PDFFile pdffile = new PDFFile(buf);

    int numPgs = pdffile.getNumPages();
    ImageIcon[] images = new ImageIcon[numPgs];

    for (int i = 0; i < numPgs; i++) {
        // draw the first page to an image
        PDFPage page = pdffile.getPage(i + 1);
        // get the width and height for the doc at the default zoom
        Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
                .getWidth(), (int) page.getBBox().getHeight());
        // generate the image
        Image img = page.getImage(rect.width, rect.height, rect, null,
                true, true);
        pfu.setWidth(rect.width);
        pfu.setHeight(rect.height);
        // save it on an array
        images[i] = new ImageIcon(img);
    }

    if(data != null){
        SignedFileDisplay fileDisplay = new SignedFileDisplay(pfu, data);
        fileDisplay.DisplayAndSelect(images);
    } else{
        SignPosition signPos = new SignPosition(pfu);
        signPos.DisplayAndSelect(images);
    }
    raf.close();
}

JFrames 由SignedFileDisplay(pfu, data)和启动SignPosition(pfu)。如果由主启动,它们都可以工作,而第二次则没有。

构造函数是:

public SignPosition(PdfFileUtils pfutils) {

    pfu = pfutils;

    // scale dimensions
    width = (int) (scale * pfu.getWidth());
    height = (int) (scale * pfu.getHeight());

    // sets the frame appearance
    sp.setSize(width + 8, height + 68);
    sp.setVisible(true);
    sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    sp.setLocationRelativeTo(null);

    // Add the image gallery panel
    add(imageGallery, BorderLayout.PAGE_START);

    // creates the JButtons objects for each button
    JButton FIRST = new JButton("|<<<");
    JButton PREVIOUS = new JButton("< Prev");
    JButton OK = new JButton("Ok");
    JButton NEXT = new JButton("Next >");
    JButton LAST = new JButton(">>>|");

    // adds the buttons to the button panel
    JPanel buttons = new JPanel();
    buttons.setLayout(new GridLayout(1, 4));
    buttons.add(FIRST);
    buttons.add(PREVIOUS);
    buttons.add(OK);
    buttons.add(NEXT);
    buttons.add(LAST);

    // add buttons on the bottom of the frame
    add(buttons, BorderLayout.SOUTH);

    // register listener
    FirstButtonListener FirstButton = new FirstButtonListener();
    PreviousButtonListener PreviousButton = new PreviousButtonListener();
    OkButtonListener OkButton = new OkButtonListener();
    NextButtonListener NextButton = new NextButtonListener();
    LastButtonListener LastButton = new LastButtonListener();

    // add listeners to corresponding componenets
    FIRST.addActionListener(FirstButton);
    PREVIOUS.addActionListener(PreviousButton);
    OK.addActionListener(OkButton);
    NEXT.addActionListener(NextButton);
    LAST.addActionListener(LastButton);
}

public SignedFileDisplay(PdfFileUtils pfutils, BiometricData bd) {

    data = bd;
    pfu = pfutils;

    // scale dimensions
    width = (int) (scale * pfu.getWidth());
    height = (int) (scale * pfu.getHeight());

    // sets the frame appearance
    sfd.setSize(width + 8, height + 68);
    sfd.setVisible(true);
    sfd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    sfd.setLocationRelativeTo(null);

    // Add the image gallery panel
    add(imageGallery, BorderLayout.PAGE_START);

    // creates the JButtons objects for each button
    JButton FIRST = new JButton("|<<<");
    JButton PREVIOUS = new JButton("< Prev");
    JButton GRAPH = new JButton("Gaph display");
    JButton NEXT = new JButton("Next >");
    JButton LAST = new JButton(">>>|");

    // adds the buttons to the button panel
    JPanel buttons = new JPanel();
    buttons.setLayout(new GridLayout(1, 4));
    buttons.add(FIRST);
    buttons.add(PREVIOUS);
    buttons.add(GRAPH);
    buttons.add(NEXT);
    buttons.add(LAST);

    // add buttons on the bottom of the frame
    add(buttons, BorderLayout.SOUTH);

    // register listener
    FirstButtonListener FirstButton = new FirstButtonListener();
    PreviousButtonListener PreviousButton = new PreviousButtonListener();
    GraphButtonListener GraphButton = new GraphButtonListener();
    NextButtonListener NextButton = new NextButtonListener();
    LastButtonListener LastButton = new LastButtonListener();

    // add listeners to corresponding componenets
    FIRST.addActionListener(FirstButton);
    PREVIOUS.addActionListener(PreviousButton);
    GRAPH.addActionListener(GraphButton);
    NEXT.addActionListener(NextButton);
    LAST.addActionListener(LastButton);
}

显然两者extends JFRAME

4

2 回答 2

1

你在那个框架里输入什么吗?您是否为每个应用程序定义了任何快捷方式?我的应用程序框架不时消失,我遇到了同样的问题。就我而言,我为每个应用程序定义了一些快捷键,其中一个是 Shift + C(关闭应用程序 - 我知道这是错误的选择).. 所以每当我想在字段中输入“c”的大写字母时,我实际上是调用关闭窗口的快捷方式。

于 2013-07-04T14:26:33.083 回答
0

有很多帧不是一个好习惯。您应该尝试使用 JDialog 而不是 JFrame。

这样,您可以将主框架传递给其他对话框并让它们成为模态

像这样:

从主打开

public class Test extends JDialog {
    public Test(Frame frame, String dialogName) {
        super(frame, dialogName, true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setBounds(x, y, w, h);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Test(null, "Test Dialog");
    }
}

从另一个框架打开

public void yourMethod() {
   new Test(yourMainFrame, dialogName);
}
于 2013-07-04T14:42:48.443 回答