java - 使用Java,尝试显示带有图像的可滚动jbutton列表,但显示文本而不是按钮

标签 java swing

让我更详细地描述我的问题。我已经搜索了很多并尝试了许多不同的方法,使用 validate() 并使用 JLabels 而不是 JButtons,但我的错误一定是在其他地方因为无论我尝试哪种方法,每次都会遇到相同的错误。控制台没有显示错误,程序运行正常,但 JButtonJLabel 显示为文本。从我读到的内容来看,这应该工作正常。我肯定错过了一些东西..请帮助我,谢谢! :)

以下是显示为文本的按钮:/image/faK3Y.png

这是代码的重要部分...我已经剪掉了其中的一些部分,因为代码很多...

public class MasterViewport implements MouseListener, ActionListener, ItemListener, ListSelectionListener {
    JScrollPane tileSelectorScrollPane;
    JFrame tMapEditor;
    ArrayList<Tile> tileArray = new ArrayList<Tile>();
    JButton[] selectorTiles = new JButton[255];
    DefaultListModel<JButton> tileList;
    JList<JButton> tileSelector;
    ...

// lower in the code...
public Container createContentPane() {

    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setOpaque(true);

    tileList = new DefaultListModel<JButton>();
    tileSelector = new JList<JButton>(tileList);
    tileSelector.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    tileSelector.setOpaque(true);
    tileSelector.setVisible(true);
    tileSelectorScrollPane = new JScrollPane(tileSelector);
    contentPane.add(tileSelectorScrollPane, BorderLayout.EAST);

    return contentPane;
}

//Now the method where the user selects a file in the select file dialog
//It's where it happens... As i confirm file selection, buttons show up as text.

...
while (!mapCreated) {
    createMap = newMapFile.showDialog(tMapEditor, "Create");
    if (createMap == JFileChooser.APPROVE_OPTION) {
        newMapFileName = newMapFile.getSelectedFile();
            if (!newMapFileName.getName().endsWith(".map")) {
                newMapFileName = new File(newMapFileName.getAbsolutePath() + ".map");
            }
            File[] dirMapFileNames = newMapFile.getCurrentDirectory().listFiles();
            BufferedImage bimg = null;
            String currentFileName = "";
            int width = 0, height = 0;
            String legendStr = "";
            char ch = 0;
            for (int i = 0; i < dirMapFileNames.length; i++) {
                if (newMapFileName.equals(dirMapFileNames[i])) {
                    mapFileAlreadyExists = true;
                }
                if (ch <= 255) {
                    if (dirMapFileNames[i].getName().endsWith(".png")) {
                        try {
                            bimg = ImageIO.read(dirMapFileNames[i]);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        width = bimg.getWidth();
                        height = bimg.getHeight();
                        if (width == 32 && height == 32) {
                            tileImageFound = true;
                            ImageIcon icon = new ImageIcon(dirMapFileNames[i].toString());
                            selectorTiles[ch] = new JButton("test", icon);
                            selectorTiles[ch].setOpaque(true);
                            selectorTiles[ch].setVisible(true);
                            Tile t = new Tile(ch, selectorTiles[ch]);
                            tileArray.add(t);
                            tileList.addElement(selectorTiles[ch]);
                            ch++;
                            legendStr = legendStr + "!" + ch + dirMapFileNames[i].getName();
                        }
                    }
                }
            ...

//Creating and showing the UI..

public static void createAndShowGUI() {
    JFrame tMapEditor = new JFrame("Tile Map Editor");
    MasterViewport masterViewport = new MasterViewport();
    tMapEditor.setJMenuBar(masterViewport.createMenuBar());
    tMapEditor.setContentPane(masterViewport.createContentPane());
    tMapEditor.setLocationRelativeTo(null);
    tMapEditor.setSize(800, 480);
    tMapEditor.setVisible(true);
}

最佳答案

您无法将按钮添加到 JList。 JList 只是绘制组件的图像。

而是将Icon 添加到JList。 JList 支持图标的默认渲染器。

如果您需要自定义类,您还可以创建一个扩展 ImageIconTileIcon 来保存额外的属性。

如果您需要真正的组件,则不要使用 JList。而是使用带有 GridLayout 的 JPanel 并将按钮添加到面板中。

关于java - 使用Java,尝试显示带有图像的可滚动jbutton列表,但显示文本而不是按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34541628/

相关文章:

java - 如何在 javafx 中调整 imageview 图像的大小?

Java - 如何终止非阻塞标准输入读取循环

java - 如何设置 JMenuItem 的大小?

Java访问框架

java - 制作可在 jar 中访问的文件夹

java - 在 main 方法中设置静态成员并在 Junit 方法中访问

java - jackson 混音 : to deserialize routeId

java - 没有初始化与初始化为 null 一样吗?

java - 单击按钮时增加一个值并使用该值更新文本字段

java - 浏览 JavaScript