gpt4 book ai didi

Java Swing (FileReader) 读取 text.file 并写入 JList - 随机工作 Oo

转载 作者:行者123 更新时间:2023-11-30 11:56:52 26 4
gpt4 key购买 nike

嗨我正在开发一个 JavaSwing 应用程序,但有一个问题......我不完全知道,但我认为这可能是一个(重新)绘制问题 :S - 无论如何这是我的代码:

主要内容:

public class QickSort {

protected static ArrayList<String> input;
private static File file = new File("C:/Users/save.txt");

public static void main(String[] args) throws Exception {

BufferedReader reader;
String line = null;

try {
input = new ArrayList<String>();

reader = new BufferedReader(new FileReader(file));

while ((line = reader.readLine()) != null) {
input.add(line);
}
reader.close();

} catch (Exception ex) {
System.out.println("Can't load file on path.. - " + ex);
ex.printStackTrace();
}
System.out.println(input.size());
JFrame.setDefaultLookAndFeelDecorated(false);
MasterWin win = new MasterWin(input);
}

用户界面:

public class MasterWin {

private JFrame frame;
private JTextField txtFieldPath;
private JButton btnBrowse;
private JButton btnAddPathTo;
private JLabel lblChosenFolderpaths;
private JButton btnRemove;
private JButton btnNext;
protected static ImageIcon icon = new ImageIcon(MasterWin.class.getResource("/View/logo_sml.gif"));
private JScrollPane scrollPane;
private JList linkList;
private List<String> test;

/**
* Create the application.
*/
public MasterWin(ArrayList<String> fileInput) {
test = fileInput;
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setIconImage(icon.getImage());
frame.setTitle("QickSort - Start");
frame.setResizable(false);
frame.setBounds(100, 100, 645, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{480, 127, 0};
gridBagLayout.rowHeights = new int[]{135, 60, 0, 0, 0, 115, 0};
gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
frame.setVisible(true);

JLabel logo = new JLabel("");
logo.setIcon(new ImageIcon(MasterWin.class.getResource("/View/Logo.gif")));
GridBagConstraints gbc_logo = new GridBagConstraints();
gbc_logo.fill = GridBagConstraints.HORIZONTAL;
gbc_logo.gridwidth = 2;
gbc_logo.insets = new Insets(0, 0, 5, 0);
gbc_logo.anchor = GridBagConstraints.SOUTH;
gbc_logo.gridx = 0;
gbc_logo.gridy = 0;
frame.getContentPane().add(logo, gbc_logo);

JLabel lblChosePathYou = new JLabel("Choose paths you want to use:");
GridBagConstraints gbc_lblChosePathYou = new GridBagConstraints();
gbc_lblChosePathYou.anchor = GridBagConstraints.SOUTHWEST;
gbc_lblChosePathYou.insets = new Insets(0, 60, 5, 5);
gbc_lblChosePathYou.gridx = 0;
gbc_lblChosePathYou.gridy = 1;
frame.getContentPane().add(lblChosePathYou, gbc_lblChosePathYou);

txtFieldPath = new JTextField();
txtFieldPath.setEditable(false);
GridBagConstraints gbc_txtFieldPath = new GridBagConstraints();
gbc_txtFieldPath.fill = GridBagConstraints.HORIZONTAL;
gbc_txtFieldPath.insets = new Insets(0, 60, 5, 5);
gbc_txtFieldPath.gridx = 0;
gbc_txtFieldPath.gridy = 2;
frame.getContentPane().add(txtFieldPath, gbc_txtFieldPath);
txtFieldPath.setColumns(10);

btnBrowse = new JButton("Browse...");
btnBrowse.setMinimumSize(new Dimension(89, 25));
btnBrowse.setMaximumSize(new Dimension(89, 25));
GridBagConstraints gbc_btnBrowse = new GridBagConstraints();
gbc_btnBrowse.anchor = GridBagConstraints.WEST;
gbc_btnBrowse.insets = new Insets(0, 10, 5, 0);
gbc_btnBrowse.gridx = 1;
gbc_btnBrowse.gridy = 2;
frame.getContentPane().add(btnBrowse, gbc_btnBrowse);

lblChosenFolderpaths = new JLabel("Chosen folderpaths:");
GridBagConstraints gbc_lblChosenFolderpaths = new GridBagConstraints();
gbc_lblChosenFolderpaths.anchor = GridBagConstraints.SOUTHWEST;
gbc_lblChosenFolderpaths.insets = new Insets(0, 60, 5, 5);
gbc_lblChosenFolderpaths.gridx = 0;
gbc_lblChosenFolderpaths.gridy = 3;
frame.getContentPane().add(lblChosenFolderpaths, gbc_lblChosenFolderpaths);

btnAddPathTo = new JButton("Add to list");
GridBagConstraints gbc_btnAddPathTo = new GridBagConstraints();
gbc_btnAddPathTo.anchor = GridBagConstraints.WEST;
gbc_btnAddPathTo.insets = new Insets(5, 10, 5, 0);
gbc_btnAddPathTo.gridx = 1;
gbc_btnAddPathTo.gridy = 3;
frame.getContentPane().add(btnAddPathTo, gbc_btnAddPathTo);

btnRemove = new JButton("Remove");
btnRemove.setToolTipText("Delete selected path");
btnRemove.setPreferredSize(new Dimension(89, 25));
btnRemove.setMinimumSize(new Dimension(89, 25));
btnRemove.setMaximumSize(new Dimension(89, 25));
GridBagConstraints gbc_btnRemove = new GridBagConstraints();
gbc_btnRemove.anchor = GridBagConstraints.NORTHWEST;
gbc_btnRemove.insets = new Insets(5, 10, 5, 0);
gbc_btnRemove.gridx = 1;
gbc_btnRemove.gridy = 4;
frame.getContentPane().add(btnRemove, gbc_btnRemove);

btnNext = new JButton("Accept");
btnNext.setPreferredSize(new Dimension(89, 25));
btnNext.setMinimumSize(new Dimension(89, 25));
btnNext.setMaximumSize(new Dimension(89, 25));
GridBagConstraints gbc_btnNext = new GridBagConstraints();
gbc_btnNext.anchor = GridBagConstraints.SOUTHWEST;
gbc_btnNext.insets = new Insets(0, 10, 25, 0);
gbc_btnNext.gridx = 1;
gbc_btnNext.gridy = 5;
frame.getContentPane().add(btnNext, gbc_btnNext);

scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.gridheight = 2;
gbc_scrollPane.insets = new Insets(0, 60, 25, 5);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 4;
frame.getContentPane().add(scrollPane, gbc_scrollPane);
//JList I copy the array
linkList = new JList(test.toArray());
scrollPane.setViewportView(linkList);
}

问题:真是奇怪!有时文本显示在我的 JList 上 - 但如果我再次启动程序,则只有一个空的 ScrollPane,没有 JList 或输入。文本的出现或多或少是随机的。

我尝试了各种数组(列表)来实现。 - 使用 AbstractModel() 或 toArray()。总是相同的结果..

有人知道这个问题吗?

最佳答案

改变:

protected static ArrayList<String> input;

收件人:

protected static final ArrayList<String> input = new ArrayList<String>();

在 main 中删除对 input 的分配。

invokeLater 中包装对 swing 代码的调用:

SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame.setDefaultLookAndFeelDecorated(false);
MasterWin win = new MasterWin(input);
};
});

关于Java Swing (FileReader) 读取 text.file 并写入 JList - 随机工作 Oo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4146996/

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