可执行jar文件在打开时不显示文本或面板

时间:2014-07-25 21:48:07

标签: java jar jinternalframe

我不知道为什么,但是当我将项目导出到一个可执行的jar文件中时,有些事情就好像你会从eclipse运行程序一样。一个框架不会打开,其中只包含文本。我有另一个内部框架,它是一个分割窗格,包含右侧的问题和左侧面板上的信息。但是在可执行jar文件中,它并没有显示左侧的信息。我不知道为什么会发生这种情况,我在导出项目时做了很多选择(制作一个jar文件),但似乎没什么用。任何帮助将不胜感激。

以下是我没有打开的框架的代码:

public class About implements ActionListener, InternalFrameListener{

private int openFrameCount;
private JDesktopPane desk;
private JTextArea Tarea;
private JScrollPane scroll;
private BufferedReader in ;
private MyInternalFrame frame;

public About(JDesktopPane desktop) {
    // TODO Auto-generated constructor stub
    desk = desktop;
}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

    if(frame == null || frame.getParent() == null && !frame.isIconifiable()){
    frame = new MyInternalFrame("SAD Imaging");
    try {
        in = new BufferedReader(new FileReader("SADInfo.txt"));
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    String line;
    String file = ""; 
    try {
        while((line = in.readLine()) != null)
        {
            System.out.println(line);
            file += line;
            file +="\n";

        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    Tarea = new JTextArea();
    //System.out.println(file);

    Tarea.setText(file);
    Font f = new Font("TimesNewRoman", Font.ROMAN_BASELINE, 16);
    Tarea.setFont(f);
    Tarea.setBackground(Color.white);
    Tarea.setAlignmentX(SwingConstants.CENTER);
    Tarea.setEditable(false);

    JPanel panel = new JPanel();
    panel.add(Tarea);
    panel.setBackground(Color.white);

     //scroll = new JScrollPane(Tarea);
     scroll = new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

     //open the frame in the middle of the desktop. 
        Dimension desktopSize = desk.getSize();
        Dimension jInternalFrameSize = frame.getSize();
        frame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);

    frame.add(scroll);
    frame.setVisible(true);
    desk.add(frame);

     try {
            frame.setSelected(true);
    } catch (java.beans.PropertyVetoException e) {

    }
     frame.addInternalFrameListener(this);

    }

    else {
        try {
            //frame.setIcon(true);
            frame.setMaximizable(true);
            frame.setIconifiable(false);
            frame.setSelected(true);
            frame.moveToFront();
            frame.toFront();
        } catch (PropertyVetoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

private class MyInternalFrame extends JInternalFrame {

    static final int xPosition = 30, yPosition = 30;
    public MyInternalFrame(String title) {
        super(title, true,true, true, true);
        setSize(600,500);

        // Set the window's location.
        setLocation(xPosition * openFrameCount, yPosition * openFrameCount);
    }
}

此文件仅包含文本信息。

拆分窗格的问题相同。这是它的代码。

public class FrequentQuestions implements ActionListener, InternalFrameListener{

private int openFrameCount;
private JDesktopPane desk;
private JTextArea Tarea;
private JScrollPane scroll;
private BufferedReader in ;
JPanel panelQuestions = new JPanel();
JPanel panelAnswers = new JPanel();
JTextArea text = new JTextArea();
JTextPane tPane = new JTextPane();
String file ="";
//private FrequentQuestions quest;
JSplitPane pane ;
MyInternalFrame frame;


public FrequentQuestions(JDesktopPane desktop) {
    // TODO Auto-generated constructor stub
    desk = desktop;
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if(frame == null || frame.getParent() == null){
    frame = new MyInternalFrame("Frequently Asked Questions");

    String [] options = {"How to open/save images", "What formats can SAD Imaging open", "How to show information about an image",
            "Compute FFT/Inverse", "Graphs"};
    JList list = new JList(options);
    //list.setBorder(BorderFactory.createLineBorder(Color.black));
    //panelQuestions.add(list);
    list.addListSelectionListener(new ListSelectionListener(){

        @Override
        public void valueChanged(ListSelectionEvent e) {
            // TODO Auto-generated method stub
            if(e.getValueIsAdjusting() == false)
                return;

            JList list = (JList) e.getSource();


            if (list.isSelectionEmpty()) {
                System.out.println("list selection is empty!");
            }

                int index = ((JList)e.getSource()).getSelectedIndex();
                if(index == 0){
                    //panelAnswers.removeAll();
                    file = "";
                    try {
                        in = new BufferedReader(new FileReader("openSave.txt"));
                    } catch (FileNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    String line;

                    try {
                        while((line = in.readLine()) != null)
                        {
                            System.out.println(line);
                            file += line;
                            file +="\n";

                        }
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    try {
                        in.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    tPane.setText(file);
                    System.out.println("I am outputting!");
                }
                else if(index == 1){
                    //panelAnswers.removeAll();
                    file = "";
                    try {
                        in = new BufferedReader(new FileReader("format.txt"));
                    } catch (FileNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    String line;
                    //String file = ""; 
                    try {
                        while((line = in.readLine()) != null)
                        {
                            System.out.println(line);
                            file += line;
                            file +="\n";

                        }
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    try {
                        in.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    tPane.setText(file);
                    System.out.println("2nd item selected");

                }
                else{
                    //panelAnswers.removeAll();
                    file = "";
                    try {
                        in = new BufferedReader(new FileReader("showInfo.txt"));
                    } catch (FileNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    String line;

                    try {
                        while((line = in.readLine()) != null)
                        {
                            System.out.println(line);
                            file += line;
                            file +="\n";

                        }
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    try {
                        in.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    tPane.setText(file);
                    System.out.println("3rd item selected");
                }

        }

    });

    JScrollPane scroll1 = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scroll = new JScrollPane(tPane,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scroll1, scroll);
    pane.setAutoscrolls(true);
    pane.setOpaque(true);

    panelQuestions.setMinimumSize(new Dimension(259,50));
    panelQuestions.setBackground(new Color(0,0,0,0));
    panelAnswers.setMinimumSize(new Dimension(600,30));
    pane.setOneTouchExpandable(true);
    pane.setDividerLocation(290);
    scroll1.setBackground(new Color(0,0,0,0));
    //Border border = new Border();
    //scroll1.setBorder(BorderFactory.createLineBorder(Color.black));

    //open the frame in the middle of the desktop. 
    Dimension desktopSize = desk.getSize();
    Dimension jInternalFrameSize = frame.getSize();
    frame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);

    frame.add(pane);
    frame.setVisible(true);
    desk.add(frame);

     try {
            frame.setSelected(true);
    } catch (java.beans.PropertyVetoException e1) {

    }
    }
    else{
        try {
            //frame.setIcon(true);
            //frame.setMaximizable(true);

            frame.setSelected(true);
            frame.moveToFront();
            //frame.toFront();
        } catch (PropertyVetoException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

}

private class MyInternalFrame extends JInternalFrame {

    static final int xPosition = 30, yPosition = 30;
    public MyInternalFrame(String title) {
        super(title, true,true, true, true);
        setSize(800,500);

        // Set the window's location.
        setLocation(xPosition * openFrameCount, yPosition * openFrameCount);
    }
}

1 个答案:

答案 0 :(得分:1)

您正在尝试从文件系统加载文件。相反,您需要通过类加载器加载它。

Getting Resources from a jar: classloader vs class resourceasstream

编辑:我假设可执行jar包含此文本文件。是这样的吗?