gpt4 book ai didi

java - JButton 中不显示图标

转载 作者:行者123 更新时间:2023-11-29 09:02:32 24 4
gpt4 key购买 nike

为什么在下面的代码中 JFrame 中的按钮没有出现?一切正常,因为它与我正在学习的教程相同。即使我已经复制并粘贴了教程中的源代码,但它不起作用

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;


public class BorderLayoutExampleII extends JFrame {


public BorderLayoutExampleII() {

initUI();
}

public final void initUI() {

JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("File");

menubar.add(file);
setJMenuBar(menubar);

JToolBar toolbar = new JToolBar();
toolbar.setFloatable(false);

ImageIcon exit = new ImageIcon("exit.png");
JButton bexit = new JButton(exit);
bexit.setBorder(new EmptyBorder(0 ,0, 0, 0));
toolbar.add(bexit);

add(toolbar, BorderLayout.NORTH);

JToolBar vertical = new JToolBar(JToolBar.VERTICAL);
vertical.setFloatable(false);
vertical.setMargin(new Insets(10, 5, 5, 5));

ImageIcon select = new ImageIcon("drive.png");
ImageIcon freehand = new ImageIcon("computer.png");
ImageIcon shapeed = new ImageIcon("printer.png");

JButton selectb = new JButton(select);
selectb.setBorder(new EmptyBorder(3, 0, 3, 0));

JButton freehandb = new JButton(freehand);
freehandb.setBorder(new EmptyBorder(3, 0, 3, 0));
JButton shapeedb = new JButton(shapeed);
shapeedb.setBorder(new EmptyBorder(3, 0, 3, 0));

vertical.add(selectb);
vertical.add(freehandb);
vertical.add(shapeedb);

add(vertical, BorderLayout.WEST);

add(new JTextArea(), BorderLayout.CENTER);

JLabel statusbar = new JLabel(" Statusbar");
statusbar.setPreferredSize(new Dimension(-1, 22));
statusbar.setBorder(LineBorder.createGrayLineBorder());
add(statusbar, BorderLayout.SOUTH);

setSize(350, 300);
setTitle("BorderLayout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
BorderLayoutExampleII ex = new BorderLayoutExampleII();
ex.setVisible(true);
}
});
}
}

最佳答案

检查文件 exit.png 是否与您的程序位于同一文件夹中。您可以通过从按钮中删除 imageicon 参数来确保加载图像时存在问题,如果显示按钮则检查 exit.png 文件是否存在

关于java - JButton 中不显示图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16508864/

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