gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:42:39 27 4
gpt4 key购买 nike

我不知道为什么,但是当我将项目导出到可执行 jar 文件时,有些东西并不像从 eclipse 运行程序一样。一帧不会打开,其中只包含文本。我有另一个内部框架,它是一个分割 Pane ,包含右侧面板上的问题和左侧面板上的信息。但在可执行 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);
}
}

该文件仅包含文本信息。

分割 Pane 也有同样的问题。这是它的代码。

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);
}
}

最佳答案

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

Getting Resources from a jar: classloader vs class resourceasstream

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

关于java - 可执行 jar 文件打开时不显示文本或面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24965015/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com