java 项目图像和连接的文本未显示

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

在我的java项目中,相关图像也没有与它们旁边的文本一起显示

enter image description here

路径是这样的enter image description here

这是我的侧按钮类的块代码,

import Model.Database;
import Model.user;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.InputStream;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SideButton extends JPanel {

    public SideButton(String text, String pic, user user, Database database, JFrame f) {
        super(new FlowLayout(FlowLayout.LEFT, 10, 10));
        setMaximumSize(new Dimension(182, 50));
        setBackground(GUIConstants.white);
        setCursor(new Cursor(Cursor.HAND_CURSOR));

        // Corrected resource path
        //String resourcePath = "/application_socialmedia/pics/" + pic + ".png";
        // Corrected resource path
        //String resourcePath = "/resources/application_socialmedia/pics/" + pic + ".png";
        //String resourcePath1 = "/resources/application_socialmedia/pics/" + pic + ".png";
        String resourcePath2 = "/resources/application_socialmedia/pics/" + pic + ".png";
        
        
        System.out.println("Expected resource path: " + resourcePath2);

        // Loading image using getResource()
        URL imgURL = getClass().getResource(resourcePath2);
        System.out.println("Trying to load image using getResource(): " + imgURL);

        // Loading image using ClassLoader
        InputStream stream = getClass().getClassLoader().getResourceAsStream("/resources/application_socialmedia/pics/" + pic + ".png");
        System.out.println("Trying to load image using ClassLoader: " + stream);

        if (imgURL != null) {
            JLabel img = new JLabel(new ImageIcon(imgURL));
            add(img);
        } else if (stream != null) {
            try {
                ImageIcon icon = new ImageIcon(ImageIO.read(stream));
                JLabel img = new JLabel(icon);
                add(img);
            } catch (Exception e) {
                System.out.println("Error: Unable to read image from InputStream!");
                e.printStackTrace();
                add(new JLabel("Image not found"));
            }
        } else {
            System.out.println("Error: Image not found at path: " + resourcePath2);
            JLabel img = new JLabel("Image not found");
            add(img);
        }

        // Add text label
        add(new JLabel(text));

        // Mouse listener
        addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new CustomView(text, user, database);
                f.dispose();
            }

            @Override
            public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseReleased(MouseEvent e) {}

            @Override
            public void mouseEntered(MouseEvent e) {
                setBackground(GUIConstants.hover);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setBackground(GUIConstants.white);
            }
        });
    }
}

另一个班级是家庭班级,就在这里,

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package View;

import Controller.CreatePost;
import Controller.GenerateTimeline;
import Model.Database;
import Model.user;
import java.awt.BorderLayout;
import java.awt.Cursor;
import static java.awt.Cursor.HAND_CURSOR;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;


public class Home {
    
    public Home(user user, Database database){
       JFrame frame = new JFrame();
       frame.getContentPane().setLayout(new BorderLayout());
       
       
       //new GenerateTimeline(user, database);
       
        JPanel sideBar = new JPanel();
        sideBar.setBackground(GUIConstants.white);
        Dimension sideBarDim = new Dimension(182, 1000);
        sideBar.setPreferredSize(sideBarDim);
        sideBar.setMaximumSize(sideBarDim);
        //sideBar.setMaximumSize(sideBarDim);
        sideBar.setLayout(new BoxLayout(sideBar, BoxLayout.Y_AXIS));
        sideBar.add(Box.createVerticalStrut(3));
        
        
        JPanel profile = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
        //frame.getContentPane().add(sideBar, BorderLayout.WEST);
        
        //JPanel profile = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
        profile.setMaximumSize(new Dimension(182, 50));
        profile.setBackground(GUIConstants.white);
        profile.setCursor(new Cursor(HAND_CURSOR));
        profile.add(new JLabel(user.getName(), 19, GUIConstants.black, Font.BOLD));
        
        
        profile.addMouseListener(new MouseListener(){
           @Override
           public void mouseClicked(MouseEvent e) {
               new Modify(user, database);
               frame.dispose();
           }

           @Override
           public void mousePressed(MouseEvent e) {
               
           }

           @Override
           public void mouseReleased(MouseEvent e) {
               
           }

           @Override
           public void mouseEntered(MouseEvent e) {
               
           }

           @Override
           public void mouseExited(MouseEvent e) {
               
           }
       });
        
        
        sideBar.add(profile);
        
        sideBar.add(Box.createVerticalStrut(3));
        
        sideBar.add(new SideButton("Posts", "save", user, database, frame));
        
        sideBar.add(Box.createVerticalStrut(3));
        sideBar.add(new SideButton("Comments", "comment", user, database, frame));
        
        sideBar.add(Box.createVerticalStrut(3));
        sideBar.add(new SideButton("Likes", "like", user, database, frame));
        
        sideBar.add(Box.createVerticalStrut(3));
        sideBar.add(new SideButton("Friends", "friends", user, database, frame));
        /*
         sideBar.add(Box.createVerticalStrut(3));
         frame.getContentPane().add(sideBar, BorderLayout.WEST);
         
         JPanel panel = new JPanel();
         panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
         panel.setBackground(null);
         
         JPanel header = new JPanel(new BorderLayout());
         header.setBackground(GUIConstants.white);
         Dimension dimension = new Dimension(500, 159);
         header.setPreferredSize(dimension);
         header.setMinimumSize(dimension);
         header.setMaximumSize(dimension);
         header.setBorder(BorderFactory.createEmptyBorder(10, 15, 10, 15));
         
         
         
         JPanel north = new JPanel(new BorderLayout());
         north.setBackground(null);
         north.add(new JLabel("Home", 20, GUIConstants.black, Font.BOLD),BorderLayout.WEST);
         header.add(north, BorderLayout.NORTH);
         
         */
         
         
        //frame.getContentPane().add(sideBar, BorderLayout.WEST);
       
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBackground(null);
        
        JPanel header = new JPanel(new BorderLayout());
        header.setBackground(GUIConstants.white);
        Dimension demension = new Dimension(500, 159);
        header.setPreferredSize(demension);
        header.setMinimumSize(demension);
        header.setMaximumSize(demension);
        header.setBorder(BorderFactory.createEmptyBorder(10, 15, 10, 15));
        
        JPanel north = new JPanel(new BorderLayout());
        north.setBackground(null);
        north.add(new JLabel("Home", 20, GUIConstants.black, Font.BOLD), BorderLayout.WEST);
        header.add(north, BorderLayout.NORTH);
        
        JTextArea postIn = new JTextArea("Share your thoughts....", 18, 20);
        header.add(new JScrollPane(postIn), BorderLayout.CENTER);
        
        JPanel south = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        south.setBackground(null);
        
        JButton postBtn = new JButton("Post", 35, 16);
        postBtn.addMouseListener(new MouseListener(){
           @Override
           public void mouseClicked(MouseEvent e) {
                if(postIn. isEmpty()){
                   new Alert("Cannot publish empty post", frame);
                   return;
                }
                
                Model.post post = new Model.post(postIn.getText(), user);
                  if(new CreatePost(post, database).posted()){
                     new Alert("Posted Successfully", frame);
                     postIn.setText("");
                  }
           }

           @Override
           public void mousePressed(MouseEvent e) {
               
           }

           @Override
           public void mouseReleased(MouseEvent e) {
               
           }

           @Override
           public void mouseEntered(MouseEvent e) {
               
           }

           @Override
           public void mouseExited(MouseEvent e) {
               
           }
       });
        
       
        postBtn.setPreferredSize(new Dimension(81, 37));
        south.add(postBtn);
        header.add(south, BorderLayout.SOUTH);
        
        panel.add(header);
        //panel.add(Box.createVerticalStrut(7));
        //panel.add(new Post());
        
        /*
        ArrayList<Model.post> posts = new GenerateTimeline(user, database).getPosts();
        for(int i=0; i<10; i++){
            panel.add(Box.createVerticalStrut(7));
            panel.add(new Post(posts.get(i)));
        }*/
        
        ArrayList<Model.post> posts = new GenerateTimeline(user, database).getPosts();
        for (int i = 0; i < Math.min(posts.size(), 10); i++) {
                panel.add(Box.createVerticalStrut(7));
                panel.add(new Post(user, posts.get(i), database, frame));
}

        //JScrollPane scrollPane = new JScrollPane(panel);
       // scrollPane.getVerticalScrollBar().setValue(0);
        
        frame.getContentPane().add(new JScrollPane(panel), BorderLayout.CENTER);
        frame.getContentPane().add(Box.createHorizontalStrut(182), BorderLayout.EAST);
         //scrollPane.getVerticalScrollBar().setValue(0);
        
       frame.setVisible(true);
       frame.requestFocus();
       
    }
    
}

有人可以告诉我如何修复图像和文本不显示的问题吗,

我以多种方式更改了文件夹路径,但没有显示。 我希望将这些图像与文本一起显示出来。我使用 apache netbeans 以及 php 我的管理数据库。

java swing
1个回答
0
投票

根据您问题中的image,需要有一个文件夹

pics
,其父文件夹与
Views
相同的父文件夹,其中
Views
文件夹包含文件
SideButton.class
,即您创建时创建的文件compile
SideButton.java
,我建议使用以下代码(可能在类
SideButton
的构造函数中)来创建一个 icon

java.net.URL url = getClass().getResource("/pics/" + pic); // assuming value of [parameter] 'pic' is, for example, 'comment.png'
java.awt.image.BufferedImage bi = javax.imageio.ImageIO.read(url);
javax.swing.ImageIcon icon = new javax.swing.ImageIcon(bi);
JLabel img = new JLabel(icon);

请注意,如何编译 Java 项目并不重要,只需确保编译后 PNG 文件位于

pics
目录中,并且
pics
目录的父目录与包含文件
View
SideButton.class
目录。

请注意,我假设您没有将 Java 项目打包到 JAR 文件中。

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