Java coder giúp em với :(

Thảo luận trong 'Lập trình & Đồ hoạ' bắt đầu bởi VLCR, 14/5/10.

  1. VLCR

    VLCR Legend of Zelda Lão Làng GVN

    Tham gia ngày:
    15/2/10
    Bài viết:
    1,011
    Nơi ở:
    Chỗ ấy ấy
    [spoil]
    Mã:
    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    
     public class MainFrame extends JFrame 
     { 
     	private JLabel numPlayer; 
     	private JLabel highScore;
     	private JButton addPlayer; 
     	private JButton addGame; 
     	private JPanel myPanel; 
    	private int player=0;
    	private int newGame=1;
    	static int gameHighest;
    	  
     	MainFrame() 
     	{ 
     		gameWindow(); 
     		this.setVisible(true); 
     	} 
    	
     	public void gameWindow() 
     	{ 
     		numPlayer = new JLabel(); 
     		highScore = new JLabel();
     		addPlayer = new JButton("New Player"); 
     		addGame = new JButton("New Game"); 
     		myPanel = (JPanel)this.getContentPane(); 
      
     		numPlayer.setText("Number of players"); 
     		highScore.setText("Game highest");
     		
     		addPlayer.addActionListener(new ActionListener() 
    		{ 
     			public void actionPerformed(ActionEvent e) 
     			{ 
     				player++; 
    				new PlayerWindow(player,getGameNum());
    				highScore.setText("Game highest      "+ gameHighest);
    				numPlayer.setText("Number of players   " + player); 
    				
    			} 
    		}); 
     		
     		addGame.addActionListener(new ActionListener() 
    		{ 
     			public void actionPerformed(ActionEvent e) 
     			{ 
    				newGame++;
    				new MainFrame();
    				highScore.setText("Game highest      "+ gameHighest);
    				
    			} 
    		}); 
     	
     		myPanel.setLayout(null); 
     		addComponent(myPanel, numPlayer, 17,70,125,19); 
     		addComponent(myPanel, highScore, 17,100,150,19);
     		addComponent(myPanel, addPlayer, 99,23,100,28); 
     		addComponent(myPanel, addGame, 99,129,100,28); 
     		
     		this.setTitle("Game"+ newGame ); 
    		if(newGame >1)
    			this.setLocation(((int)(Math.random()*600)),((int)(Math.random()*600)));
    		else
    			this.setLocation(312, 65); 
     		this.setSize(280, 250); 
    		this.addWindowListener(new WindowAdapter() 
    		{
    			public void windowClosing(WindowEvent e)
    			{
    				if(newGame > 1)
    				{
    					MainFrame.this.setVisible(false);
    					newGame--;
    				}
    				else
    					System.exit(0);
    			}
    		} ) ;
     	} 
      
     	public void addComponent(Container container,Component c,int x,int y,int width,int height) 
     	{ 
     		c.setBounds(x,y,width,height); 
     		container.add(c); 
     	} 
    	public int getGameNum()
    	{
    		return newGame;
    	}
    	public static void main(String[] args) 
     	{ 
     		new MainFrame(); 
     	} 
    	
    } 
      
     
    Mã:
    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    
    public class PlayerWindow extends JFrame
    {
    	private JLabel playerHigh; 
     	private JLabel score;
    	private JLabel gameHighest;
     	private JButton spin; 
     	private JPanel myPanel1; 
    	private int currentScore, currentHigh, playerNum, gameNum;	
    	
    	PlayerWindow(int playerNum, int gameNum)
    	{
    		this.playerNum = playerNum;
    		this.gameNum= gameNum;
    		createGame();
    		this.setVisible(true); 
    		
    	}
    	public void createGame() 
     	{ 
     		playerHigh = new JLabel(); 
    		score= new JLabel();
    		gameHighest = new JLabel();
    		spin = new JButton("Spin"); 
    		myPanel1 = (JPanel)this.getContentPane(); 
    	  
     		playerHigh.setText("Player highest"); 
     		gameHighest.setText("Game highest    "+ MainFrame.gameHighest);
    		score.setText("Score");
     		
    		currentHigh =0;
     		spin.addActionListener(new ActionListener() 
    		{ 
     			public void actionPerformed(ActionEvent e) 
     			{ 
     				currentScore = (int)(Math.random()* 1000);
    				gameHighest.setText("Game highest     " + MainFrame.gameHighest);
    				if( currentScore > MainFrame.gameHighest)
    				{
    					MainFrame.gameHighest = currentScore;
    					gameHighest.setText("Game highest     " + MainFrame.gameHighest);
    				}
    				if(currentHigh < currentScore)
    					currentHigh = currentScore;
    				playerHigh.setText("Player highest   "+ currentHigh);
    				score.setText("Score    " + currentScore);
     			} 
    		}); 
     		
     		 	
     		myPanel1.setLayout(null); 
     		addComponent(myPanel1, score, 17,70,200,19); 
     		addComponent(myPanel1, playerHigh, 17,100,200,19);
     		addComponent(myPanel1, spin, 99,23,83,28); 
     		addComponent(myPanel1, gameHighest, 17,129,200,28); 
     		
    		if(playerNum >=1)
    			this.setLocation(((int)(Math.random()*600)),((int)(Math.random()*600)));
    		else
    			this.setLocation(312, 65); 
    		this.setSize(280, 250); 
    		this.setTitle("G"+ gameNum+",P" +playerNum ); 
    		this.addWindowListener(new WindowAdapter() 
    		{
    			public void windowClosing(WindowEvent e)
    			{
    				PlayerWindow.this.setVisible(false);
    			}
    		} ) ;
     	}
    	public void addComponent(Container container,Component c,int x,int y,int width,int height) 
     	{ 
     		c.setBounds(x,y,width,height); 
     		container.add(c); 
     	} 
    	
    }	
    [/spoil]
    yêu cầu của project này là tạo ra 1 game, có thể chơi nhiều người. Mỗi người bấm nút Spin để random 1 số, nếu số lớn hơn số hiện tại thì Game highest score sẽ hiện số đó lên.

    Mỗi lần bấm vô nút New Game, 1 cửa sổ giống vậy sẽ hiện ra, title sẽ thành Game x ( x là số thứ tự).
    Mỗi new game thì sẽ có Game Highest score khác nhau. chỉ có player tạo ra từ Game đó mới thấy Game highest score.
    Đang gặp rắc rối chỗ setTitle cho gameWindow và nơi chứa cái Game Highest score.
    bạn nào có thể vui lòng cho 1 giải pháp ko :-s
     
  2. VLCR

    VLCR Legend of Zelda Lão Làng GVN

    Tham gia ngày:
    15/2/10
    Bài viết:
    1,011
    Nơi ở:
    Chỗ ấy ấy
    hix ko ai giúp em à :(
     
  3. DieForSure

    DieForSure T.E.T.Я.I.S

    Tham gia ngày:
    8/7/08
    Bài viết:
    619
    Nơi ở:
    Zarnakand
    mình không rành java lắm, nhưng hình như để xài đc hàm setTitle bạn fải import java.lang.String
     
  4. Bối Bối

    Bối Bối C O N T R A

    Tham gia ngày:
    20/2/06
    Bài viết:
    1,859
    Nơi ở:
    Ha Noi, Vietnam,
    ^: chỉ bậy, cái String nó nằm trong gói java.lang rồi, mặc định ko phải import

    @VLCR: ko để ý thấy topic này
    Thêm vào dòng code in đậm :
    Mã:
    PlayerWindow(int playerNum, int gameNum)
    	{
    		this.playerNum = playerNum;
    		this.gameNum= gameNum;
    		createGame();
    		this.setVisible(true);
                    [B]setTitle("Game "+gameNum);[/B]
    		
    	}
    
     

Chia sẻ trang này