JFrame未保持显示状态-java

JFrame未保持显示状态-java,java,swing,jframe,swingutilities,Java,Swing,Jframe,Swingutilities,我有一个程序,它可以做几件事,并在几个动作后显示不同的JFrame。当我从main启动第一个JFrame时,一切正常,但是当我从另一个不同于main类的类启动它时,它不会显示 重点是什么?我做错了什么 下面是一些代码: 这是从主界面调用的: SwingUtilities.invokeLater(new Runnable() { PdfFileUtils pfu = new PdfFileUtils(path); public vo

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

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

下面是一些代码:

这是从主界面调用的:

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 SignedFileDisplay(PdfFileUtils-pfutils,生物特征数据bd){
数据=bd;
pfu=pfutils;
//比例尺寸
宽度=(int)(比例*pfu.getWidth());
高度=(int)(比例*pfu.getHeight());
//设置框架外观
sfd.设置尺寸(宽度+8,高度+68);
sfd.setVisible(真);
sfd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sfd.setLocationRelativeTo(空);
//添加图像库面板
添加(imageGallery,BorderLayout.PAGE_START);
//为每个按钮创建JButtons对象
JButton FIRST=新JButton(“|>|”);
//将按钮添加到按钮面板
JPanel按钮=新的JPanel();
按钮。setLayout(新的GridLayout(1,4));
按钮。添加(第一);
按钮。添加(上一个);
按钮。添加(图形);
按钮。添加(下一步);
按钮。添加(最后一个);
//在框架底部添加按钮
添加(按钮,边界布局。南部);
//寄存器侦听器
FirstButtonListener FirstButton=新的FirstButtonListener();
PreviousButtonListener PreviousButton=新的PreviousButtonListener();
GraphButtonListener GraphButton=新GraphButtonListener();
NextButtonListener NextButton=新的NextButtonListener();
LastButtonListener LastButton=新的LastButtonListener();
//将侦听器添加到相应的组件集中
addActionListener(FirstButton);
PREVIOUS.addActionListener(PreviousButton);
GRAPH.addActionListener(GraphButton);
NEXT.addActionListener(NextButton);
LAST.addActionListener(LastButton);
}

显然,这两个
都扩展了JFRAME

您是否在该框架中键入了内容?您是否为每个应用程序定义了任何快捷方式? 我的应用程序框架有时会出现类似的问题。
在我的例子中,我为每个应用程序定义了一些快捷键,其中一个是Shift+C(关闭应用程序-我知道这是错误的选择)。。所以,每当我想在字段中键入大写字母“c”时,我实际上是在调用快捷方式来关闭窗口

多帧不是一个好的做法。 您应该尝试使用JDialog而不是JFrame

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

像这样:

从主管道打开 从另一帧打开
你能展示“设置”方法吗?@GabrielC–mara我编辑过,然后提问好的,你能展示框架的构造器吗?@GabrielC–mara编辑过,请尽快提供帮助。不,框架只显示一些PDF页面,并有一些按钮可退出。没别的了。
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();
}
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);
}
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);
}