java事件监听

风吹得好舒服 2013-05-24 12:34:24
为什么在一个Jpanel上添加了两个Jpanel以后,键盘事件监听器就没有作用了呢?求大家赐教。。。
...全文
127 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
风吹得好舒服 2013-05-24
  • 打赏
  • 举报
回复
引用 2 楼 CHANGXIAOTING 的回复:
给代码!!!
引用 1 楼 confirmAname 的回复:
代码呢,不贴出源码怎么看
引用 4 楼 fangmingshijie 的回复:
重叠了吧。。。
class gameCtrlPanel extends JPanel
{
	JButton musicButton=new JButton();//控制音乐开关按钮
	JButton helpButton=new JButton();//帮助
	JButton toMenuButton=new JButton();//返回主菜单按钮
	JButton exitGameButton=new JButton();//游戏退出按钮
	JButton playAgainButton=new JButton();//重玩按钮
	JButton poseGameButton=new JButton();//暂停游戏按钮
	JButton upButton=new JButton();//上一关
	JButton downButton=new JButton();//下一关
	JPanel previewPanel=new JPanel();//方块预览框
	JLabel showStation=new JLabel();//显示等级和分数
	
	GridBagLayout gameCtrlGbl=new GridBagLayout();
	GridBagConstraints gbc2=new GridBagConstraints();

	
	gameCtrlPanel(){
		setLayout(gameCtrlGbl);
		gbc2.insets=new Insets(10,10,10,10);
		gbc2.fill=GridBagConstraints.BOTH;
		/*.......构造musicButton...........*/
		gbc2.weightx=1;
		gbc2.weighty=1;
		gbc2.gridwidth=1;
		gbc2.gridheight=1;
//		musicButton.setBorder(null);
//		musicButton.setOpaque(false);
//		musicButton.setContentAreaFilled(false);
		addComponent(musicButton,0,0);
	
		/*....*/
		JLabel lblA=new JLabel();
		gbc2.weightx=1.5;
		addComponent(lblA,1,0);
		
		/*构造helpButton*/
		
		gbc2.weightx=1;
		gbc2.gridwidth=GridBagConstraints.REMAINDER;
		helpButton.setBorder(null);
		helpButton.setOpaque(false);
		helpButton.setContentAreaFilled(false);
		addComponent(helpButton,2,0);
		
//		/*....*/
//		JLabel lblB=new JLabel();
//		gbc2.weightx=1;
//		gbc2.weighty=4;
//		addComponent(lblB,0,1);
		
		/*构造previewPanel*/
		gbc2.weightx=1;
		gbc2.weighty=4;
		gbc2.gridwidth=GridBagConstraints.REMAINDER;
		previewPanel.setOpaque(false);		
		addComponent(previewPanel,0,1);
		
//		/*...*/
//		JLabel lblC=new JLabel();
//		
//		addComponent(lblC,2,1);
//		
		/*构造showStation*/
		gbc2.weighty=3.5;
		addComponent(showStation,0,2);
		
		/*构造upButton、poseGameButton、downButton*/
		gbc2.weightx=1;
		gbc2.weighty=1.5;
		gbc2.gridwidth=1;
		upButton.setBorder(null);
		upButton.setOpaque(false);
		upButton.setContentAreaFilled(false);
		addComponent(upButton,0,3);
		poseGameButton.setBorder(null);
   	    poseGameButton.setOpaque(false);
		poseGameButton.setContentAreaFilled(false);
		addComponent(poseGameButton,1,3);
		downButton.setBorder(null);
		downButton.setOpaque(false);
		downButton.setContentAreaFilled(false);
		addComponent(downButton,2,3);
		JLabel lbl_1=new JLabel();
		gbc2.gridwidth=GridBagConstraints.REMAINDER;
		addComponent(lbl_1,3,3);
		
		/*构造palyAgainButton、toMenuButton、exitGameButton*/
		gbc2.weightx=1;
		gbc2.weighty=1.5;
		gbc2.gridwidth=1;
		gbc2.gridheight=1;
		playAgainButton.setBorder(null);
		playAgainButton.setOpaque(false);
		playAgainButton.setContentAreaFilled(false);
		addComponent(playAgainButton,0,4);
		toMenuButton.setBorder(null);
		toMenuButton.setOpaque(false);
		toMenuButton.setContentAreaFilled(false);
		addComponent(toMenuButton,1,4);
		exitGameButton.setBorder(null);
		exitGameButton.setOpaque(false);
		exitGameButton.setContentAreaFilled(false);
		addComponent(exitGameButton,2,4);
		JLabel lbl_2=new JLabel();
		gbc2.gridwidth=GridBagConstraints.REMAINDER;
		addComponent(lbl_2,3,4);
		}
	public void addComponent(Component c,int col,int row ){
		gbc2.gridx=col;//组件定位
		gbc2.gridy=row;
		gameCtrlGbl.setConstraints(c,gbc2);
		add(c);		
	}

}


//游戏界面
class panelTwo extends JPanel implements KeyListener {

	elspanel  gameArea = new elspanel();// 游戏区
	gameCtrlPanel gameCtrl = new gameCtrlPanel();// 游戏控制区
	
	
	GridBagLayout panelTwoLayout = new GridBagLayout();
	GridBagConstraints gbc1 = new GridBagConstraints();

	panelTwo() {
		addKeyListener(this);
		setLayout(panelTwoLayout);
		gbc1.insets=new Insets(1,10,1,5);
		gbc1.fill=GridBagConstraints.BOTH;
		
		
		JLabel lbl1=new JLabel();
		gbc1.weightx=1;
		gbc1.weighty=1;
		gbc1.gridwidth=GridBagConstraints.REMAINDER;
		gbc1.gridheight=1;
		addComponent(lbl1,0,0);
		
		JLabel lbl4=new JLabel();
		gbc1.weightx=1;
		gbc1.weighty=7.5;
		gbc1.gridwidth=1;
		gbc1.gridheight=2;
		addComponent(lbl4,0,1);
		/*............构造gameArea游戏区面板.........*/
		gbc1.weightx=10.5;
		gbc1.weighty=7.5;
		gbc1.gridwidth=1;
		gbc1.gridheight=1;
		addComponent(gameArea,1,1);
		Thread thread=new Thread(gameArea);
		thread.start();
//		gameArea.setOpaque(false);
		//gameArea.setBackground(Color.red);
		
		gbc1.weightx=1.5;
	//	gbc1.gridx=1;
		gbc1.gridheight=1;
		JLabel lbl3=new JLabel();
		addComponent(lbl3,2,1);
		/*...........构造gameCtrl游戏区面板..........*/
	
		gbc1.weightx=5.2;
		gbc1.gridheight=1;
		addComponent(gameCtrl,3,1);
//		gameCtrl.setOpaque(false);
	//	gameCtrl.setBackground(Color.green);//
		
		JLabel lbl5=new JLabel();
		gbc1.weightx=5.6;
		gbc1.gridwidth=GridBagConstraints.REMAINDER;
		gbc1.gridheight=1;
		addComponent(lbl5,4,1);
		
		JLabel lbl2=new JLabel();
		gbc1.weighty=0.6;
		gbc1.gridwidth=GridBagConstraints.REMAINDER;
		gbc1.gridheight=1;
		addComponent(lbl2,0,2);
	}

	public void addComponent(Component c, int col, int row) {
		gbc1.gridx = col;// 组件定位
		gbc1.gridy = row;
		panelTwoLayout.setConstraints(c, gbc1);
		add(c);
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		int key = e.getKeyCode();
		switch (key) {
		case KeyEvent.VK_RIGHT:
			if (gameArea.currentShape.canMoveRight())
				gameArea.currentShape.MoveRight();
			gameArea.repaint();
			break;
		case KeyEvent.VK_LEFT:
			if (gameArea.currentShape.canMoveLeft())
				gameArea.currentShape.MoveLeft();
			gameArea.repaint();
			break;
		case KeyEvent.VK_DOWN:
			gameArea.time = 50;
			gameArea.repaint();
			break;
		case KeyEvent.VK_UP:
			if (gameArea.currentShape.canTurn())
				gameArea.currentShape.turn();
			gameArea.repaint();
			break;
		}
		
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		int key = e.getKeyCode();
		if (key == KeyEvent.VK_DOWN)
			gameArea.time = 500;
	}
}

public class els extends JFrame implements ActionListener {

	music mymusic = null;
    panelTwo game=null;
	public els() {	
//		panelOne control=new panelOne(){
//			protected void paintComponent(Graphics g) {
//				ImageIcon icon = new ImageIcon(
//						"D:\\projects\\MyEls\\src\\panelOne.jpg");
//				Image img = icon.getImage();
//				g.drawImage(img, 0, 0, this.getSize().width,
//						this.getSize().height, this);
//			};
//		};
		panelTwo game=new panelTwo(){
			protected void paintComponent(Graphics g) {
				ImageIcon icon = new ImageIcon(
						"D:\\projects\\MyEls\\src\\panelTwo.jpg");
				Image img = icon.getImage();
				g.drawImage(img, 0, 0, this.getSize().width,
						this.getSize().height, this);
			};
		};
		mymusic = new music();
		Container container = this.getContentPane();
		mymusic.start();
		container.add(game);
		addKeyListener(game);
		game.gameCtrl.musicButton.addActionListener(this);
	}
	public static void main(String[] args) throws Exception {
		els f = new els();
		f.setSize(850, 637);
		f.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==game.gameCtrl.musicButton)
		{
			try {
				mymusic.sleep(10000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				
			}
		}
		
	}
}
这只是代码的一部分。。
地下室森林 2013-05-24
  • 打赏
  • 举报
回复
引用 4 楼 fangmingshijie 的回复:
重叠了吧。。。
大神出没
kkkddd44 2013-05-24
  • 打赏
  • 举报
回复
阻挡住了。下面的看不见
davidworld 2013-05-24
  • 打赏
  • 举报
回复
没有代码你让我们去猜?
  • 打赏
  • 举报
回复
重叠了吧。。。
跳蚤图 2013-05-24
  • 打赏
  • 举报
回复
StevenLoveMaggie 2013-05-24
  • 打赏
  • 举报
回复
给代码!!!
confirmAname 2013-05-24
  • 打赏
  • 举报
回复
代码呢,不贴出源码怎么看

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧