我有一个程序可以执行多项操作,并在多次操作后显示不同的JFrame。当我从main启动第一个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();
}
JFrame由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
答案 0 :(得分:1)
你在那个框架里打字吗?您是否为每个应用程序定义了快捷方式? 我的应用程序框架不时出现同样的问题。 在我的情况下,我有一些关键的快捷方式定义每个应用程序,其中一个是Shift + C(关闭应用程序 - 糟糕的选择,我知道)..所以每当我想在字段中键入大写“c”,我实际上是调用关闭窗口的快捷方式。
答案 1 :(得分: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);
}