gpt4 book ai didi

java - 如何将 JButtons 合并到图形中?

转载 作者:行者123 更新时间:2023-11-29 07:15:21 25 4
gpt4 key购买 nike

我正在尝试编写 RISK 棋盘游戏的在线版本。我想将 JButtons 放在板上。所以基本上我想要 JButtons 在一些图像之上,但是我无法让它工作。这是我的代码:

 public void main(String[] args){
JFrame frame = new JFrame("RISK");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


JPanel panel = new JPanel();
panel.setLayout(null);
frame.add(panel);
JButton button = new JButton("test");
button.setBounds(100, 100, 150, 150);
panel.add(button);

frame.setVisible(true);
frame.add(new graphics());
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
try{
BufferedImage board = ImageIO.read(new File("board.jpg"));
}catch(IOException e){}
g.drawImage(board, 0, 0, null);
}

非常感谢您。 - 你的答案不需要引用这个特定的代码,只是一个带有图形和 JButtons 的一般例子就很棒了!

最佳答案

如果您愿意,可以在按钮上添加图像。这是一个示例代码。


BufferedImage myPictur =ImageIO.read(getClass().getResourceAsStream("/resources/shoppingicon.png"));
JButton shoppingButton = new JButton("Shopping",new ImageIcon(myPicture));

关于java - 如何将 JButtons 合并到图形中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10034028/

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