您好,登錄后才能下訂單哦!
使用java怎么實現(xiàn)潛艇大戰(zhàn)游戲?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
代碼如下
package com.util; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Observable; import javax.swing.JLabel; import javax.swing.JPanel; /* * 進圖游戲的主界面面板 */ public class MainPanel extends JPanel { private static final long serialVersionUID = 1L; private MyButton startButton; private MyButton exitButton; private JLabel helpLabel; private JLabel helpLabel1; private JLabel helpLabel2; private JLabel helpLabel3; private Image image; private JLabel centerlabel; private boolean isStart; private boolean isExit; private Observable obs; public MainPanel(Observable ob) { obs = ob; startButton = new MyButton("進入游戲"); exitButton = new MyButton("退出游戲"); helpLabel = new JLabel(); helpLabel1 = new JLabel(); helpLabel2 = new JLabel(); helpLabel3 = new JLabel(); centerlabel = new JLabel(); this.setLayout(new BorderLayout()); this.helpLabel.setPreferredSize(new Dimension(645,291)); this.helpLabel1.setPreferredSize(new Dimension(180,80)); this.helpLabel2.setPreferredSize(new Dimension(215,80)); this.helpLabel3.setPreferredSize(new Dimension(645,80)); this.centerlabel.setPreferredSize(new Dimension(460,80)); centerlabel.setLayout(new GridLayout(2,1)); centerlabel.add(this.startButton); centerlabel.add(this.exitButton); this.centerlabel.setBackground(new Color(255,255,0)); this.add(helpLabel,BorderLayout.NORTH); this.add(helpLabel1,BorderLayout.EAST); this.add(helpLabel2,BorderLayout.WEST); this.add(helpLabel3,BorderLayout.SOUTH); this.add(centerlabel,BorderLayout.CENTER); image = Toolkit.getDefaultToolkit().getImage("imgs/主界面112.png"); // image = new ImageIcon(image).getImage(); this.startButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { boolean flag = true; MainPanel.this.setIsStart(flag); MainPanel.this.obs.notifyObservers(MainPanel.this); } } ); this.exitButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { MainPanel.this.setExit(true); } } ); } public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D) g; g2.drawImage(image,0,0, this.getWidth(), this.getHeight(),this); super.paintComponents(g); } public boolean getIsStart() { return this.isStart; } public void setIsStart(boolean isStart) { this.isStart = isStart; } public boolean isExit() { return isExit; } public void setExit(boolean isExit) { this.isExit = isExit; } }
關(guān)于使用java怎么實現(xiàn)潛艇大戰(zhàn)游戲問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。