我正在尝试向 GUI 添加图像按钮,但得到的只是空白屏幕。谁能告诉我我的代码有什么问题吗?这就是我到目前为止所拥有的。
package fallingStars;
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI extends JFrame
{
private JLabel label;
private JButton button;
private JPanel buttonPanel;
public static void startButton()
{
ImageIcon start = new ImageIcon("Start.png");
JButton play = new JButton(start);
play.setBounds(150,100,100,50);
}
public static void main(String args[])
{
GUI gui = new GUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(300, 500);
gui.setVisible(true);
gui.setResizable(false);
gui.setTitle("Falling Stars");
JPanel panel = new JPanel();
startButton();
}
}
最佳答案
您必须使用container.add(JButton);将JButton添加到容器中
如果您不实际添加它,您将得到一个空白屏幕:)
关于java - 使用图像作为按钮,获得空白屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23668508/