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 回の実行ごとに)、割り込み例外が発生します。

また、この方法で 2 番目のフレームを起動しようとしました。

 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)ます。メインによって起動された場合は両方とも機能し、2 回目は機能しません。

コンストラクターは次のとおりです。

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

そのフレームに何かを入力していますか?アプリケーションごとに定義されたショートカットはありますか? アプリケーションフレームが時々消えるという同じ問題がありました。私の場合、アプリケーションごとに定義されたいくつかのキーショートカットがあり、そのうちの1つはShift + C(アプリケーションを閉じる-悪い選択です、私は知っています)..フィールドに「c」を大文字で入力したいときはいつでも、私は実際には、ウィンドウを閉じるためのショートカットを呼び出していました。

于 2013-07-04T14:26:33.083 に答える
0

多くのフレームを持つことは良い習慣ではありません。JFrame の代わりに JDialog を使用するようにしてください。

このようにして、メイン フレームを他のダイアログに渡し、それらをモーダルにすることができます。

このような:

メインから開く

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 に答える