swing使用Java,试图用图像显示JButton的可滚动列表,但显示的是文本而不是按钮
让我更详细地描述我的问题。我搜索了很多,尝试了很多不同的方法,用validate()
和JLabels
代替JButtons
,但我的错误肯定在其他地方,因为无论我尝试哪种方法,每次都会得到相同的错误。控制台没有显示错误,程序运行正常,但JButton
或JLabel
显示为文本。据我所知,这应该很好用。我肯定错过了什么。。请帮帮我,谢谢!:)
以下是显示为文本的按钮:http://i.stack.imgur.com/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);
}
# 1 楼答案
我补充说:
# 2 楼答案
无法向JList添加按钮。JList只绘制组件的图像
相反,在
JList
中添加一个Icon
。JList支持图标的默认呈现程序如果需要自定义类,还可以创建一个
TileIcon
扩展ImageIcon
来保存额外的属性如果您需要真正的组件,那么不要使用JList。相反,使用带有
GridLayout
的JPanel并将按钮添加到面板中