从文件中读取并附加到jframe中

问题描述 投票:0回答:1

我一直在尝试将我的 txt 文件的输入(即图像和文本)显示到 jframe 中,但我仍然不能。我已经有 jpanel 作为显示它的地方。也许我必须在属性中编辑一些内容?我的框架集布局为空,我有 3 个 jpanel 来显示三个数据及其各自的图像。

private void loadCandidates() {
    // Open the candidates file
    File file = new File("C:\\Users\\nurul\\Desktop\\votingjava\\prescandidate.txt");
    if (!file.exists()) {
        System.out.println("File does not exist: " + file.getAbsolutePath());
        return;  // Exit if the file doesn't exist
    }

    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        String line;
        
        // Using panels for each candidate
        int candidateIndex = 0;  // To decide which panel to add to

        while ((line = br.readLine()) != null) {
            line = line.trim();
            String[] parts = line.split("\\s*:\\s*");

            if (parts.length == 5) {
                // Extract candidate details
                String id = parts[0].trim();
                String name = parts[1].trim();
                String year = parts[2].trim();
                String motto = parts[3].trim();
                String imgpath = parts[4].trim();

                // Debug print to confirm loaded candidate info
                System.out.println("Loaded candidate: " + name);

                // Image path check
                File imageFile = new File(imgpath);
                if (!imageFile.exists()) {
                    System.out.println("Image not found: " + imgpath);
                }

                // Create candidate panel
                JPanel candidatePanel = new JPanel();
                candidatePanel.setLayout(new BoxLayout(candidatePanel, BoxLayout.Y_AXIS));
                candidatePanel.setBorder(BorderFactory.createTitledBorder("Candidate"));

                JLabel nameLabel = new JLabel("Name: " + name);
                JLabel yearLabel = new JLabel("Year: " + year);
                JLabel mottoLabel = new JLabel("Slogan: " + motto);

                // Load and scale the image
                ImageIcon candidateImageIcon = new ImageIcon(imgpath);
                Image scaledImage = candidateImageIcon.getImage().getScaledInstance(150, 150, Image.SCALE_SMOOTH);
                candidateImageIcon = new ImageIcon(scaledImage);

                JLabel imageLabel = new JLabel(candidateImageIcon);

                // Add components to the candidate panel
                candidatePanel.add(imageLabel);
                candidatePanel.add(nameLabel);
                candidatePanel.add(yearLabel);
                candidatePanel.add(mottoLabel);

                // Ensure the layout of the panel is refreshed
                candidatePanel.revalidate();
                candidatePanel.repaint();
                
                // Add the candidate panel to the appropriate panel (panel1, panel2, or panel3)
                switch (candidateIndex) {
                    case 0:
                        jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.Y_AXIS));
                        jPanel1.add(candidatePanel);
                        break;
                    case 1:
                        jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.Y_AXIS));
                        jPanel2.add(candidatePanel);
                        break;
                    case 2:
                        jPanel3.setLayout(new BoxLayout(jPanel3, BoxLayout.Y_AXIS));
                        jPanel3.add(candidatePanel);
                        break;
                    default:
                        break;
                }

                // Revalidate and repaint to update the UI
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        jPanel1.revalidate();
                        jPanel1.repaint();
                        jPanel2.revalidate();
                        jPanel2.repaint();
                        jPanel3.revalidate();
                        jPanel3.repaint();
                    }
                });

                // Increment candidate index to assign the next candidate to the next panel
                candidateIndex++;
            } else {
                System.out.println("Invalid line format: " + line);
            }
        }
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, "Error reading prescandidate.txt: " + e.getMessage(),
                                      "File Error", JOptionPane.ERROR_MESSAGE);
    }

我最初尝试在框架中内置jlabel然后编写代码,但它不起作用。所以现在我只是手动编写该部分的代码。我想显示从txt文件读取的图像和数据。我确实尝试过调试,我的路径都是正确的。它只是不会显示在框架中。

java user-interface text-files jlabel
1个回答
0
投票

在调用 loadCandidates 函数后尝试 myFrame.setVisible(true)

如果运气不好,请尝试在初始化 JFrame 和面板之后将 invokeLater 提升到。

© www.soinside.com 2019 - 2024. All rights reserved.