java吃豆豆游戏-1控制按钮编写

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sir_jun/article/details/8974500

个人习惯,先写按钮

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;


public class GameController extends JPanel implements ActionListener {
	
	private JButton exitButton = new JButton("Exit");
	private JButton settingsButton = new JButton ("Settings");
	private JButton aboutButton = new JButton ("About");
	private JButton keysButton = new JButton ("Keys");
	private JLabel label = new JLabel ("     0     ");
	
	private Background bg;
		
	public GameController (Background bg){
		super(new FlowLayout(FlowLayout.CENTER));
		this.bg = bg;
		setOpaque(false);//设置组件透明
		
		exitButton.addActionListener(this);
		settingsButton.addActionListener(this);
		aboutButton.addActionListener(this);
		keysButton.addActionListener(this);
		
		//setBorder() 是使用Border 边框 ,Border 是特殊的Swing组件,为Swing组件提供不同的边框修饰
		label.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
		
		add(label);				
		add(exitButton);
		add(aboutButton);
		add(settingsButton);
		add(keysButton);
		
			
			
	}
	
	public JLabel getLabel(){
		return label;
	}
	
	public void actionPerformed (ActionEvent e){
		
		if (e.getSource()==exitButton){
			System.exit(0); 
						
		}				
		
		else if (e.getSource()==settingsButton){
				
					
		}
	
		else if (e.getSource()==aboutButton){
			JOptionPane.showMessageDialog(getParent(),  "Eater Version 1.1" +
														"\nProgrammed by K.I.K");
														
		}
		
		else if (e.getSource()==keysButton){
			JOptionPane.showMessageDialog(getParent(),  "Use arrow keys to move the eater");
		}
		
		else return;
	}
}


展开阅读全文

没有更多推荐了,返回首页