Java 如何将.png添加到JButton
我正在处理一个程序的主菜单,我发现很难将.png文件应用到我的JButton,这就是我到达的地方Java 如何将.png添加到JButton,java,image,swing,jframe,jbutton,Java,Image,Swing,Jframe,Jbutton,我正在处理一个程序的主菜单,我发现很难将.png文件应用到我的JButton,这就是我到达的地方 public class Menu extends JFrame implements ActionListener { public static void main(String[] args) { Color b = new Color(0,89,255); Color t = new Color(255,0,0); Color bttn = new Color (25
public class Menu extends JFrame implements ActionListener {
public static void main(String[] args) {
Color b = new Color(0,89,255);
Color t = new Color(255,0,0);
Color bttn = new Color (255,255,0);
final JFrame frame = new JFrame ("Main Menu");
frame.setVisible(true);
frame.setSize(1000,750);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setBackground(b);
JButton start = new JButton ("Start");
start.setBounds(300, 300, 150, 75);
start.setForeground(t);
JButton exit = new JButton ("Exit");
exit.setBounds(550, 300, 150, 75);
exit.setBackground(bttn);
exit.setForeground(t);
请留下任何答案,因为我正赶时间
myButton.setIcon(new ImageIcon("/path/to/image.png"));
是在创建JButton之后您想要做什么的方式。如果要在创建JButton时添加映像,则可以使用适当的构造函数
JButton myButton = new JButton("What The Heck?", new ImageIcon("/path/to/image.png"));
请不要使用空布局。改用LayoutManager。我是否保持start.setBounds300、300、150、75;这完全取决于您希望如何布局JComponents。有时你需要一个布局管理器的组合我是否保持开始。setBounds300、300、150、75;,不,您不应该使用setBounds。Swing设计用于布局管理器。