查看: 833|回复: 1
|
java问题。。。。Jpanel + JFrame
[复制链接]
|
|
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.Container;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Crops extends JFrame {
final int WON=0 , LOST=1, CONTINUE= 2;
boolean firstRoll = true;
int SumofDice=0;
int mypoint = 0;
int gameStatus = CONTINUE;
JLabel dice1, dice2,lbsum, lbwin;
JTextField JTdice1, JTdice2, JTSUM, JTwin;
JButton JBdice1;
JFrame JfDice;
JPanel p1;
public Crops(){
super ("try");
dice1 = new JLabel("Number 1");
dice2 = new JLabel("Number 2");
lbsum = new JLabel("Sum Total");
lbwin = new JLabel("Point");
JBdice1= new JButton ("Dice");
Container content = getContentPane();
p1 = new JPanel();
setVisible(true);
setLocation(300,350);
setSize(360,550);
content.add(p1);
// p1.setLayout(new GridLayout(4,2));
p1.add(dice1);
p1.add(dice2);
//p1.show();
p1.setVisible(true);
p1.setSize(360,550);
p1.setBackground(Color.cyan);
// getContentPane().add(p1);
// setContentPane(p1);
JBdice1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
SumofDice =rollDice();
if(firstRoll) {
switch (SumofDice){
case 7:
case 11:
gameStatus = WON;
JTwin.setText("");
break;
case 2:
case 3:
case 12:
gameStatus =LOST;
JTwin.setText("");
break;
default:
gameStatus =CONTINUE;
mypoint = SumofDice;
JTwin.setText(Integer.toString(mypoint));
firstRoll = false;
break;
}
}
else{
if(SumofDice ==mypoint)
gameStatus = WON;
else
if(SumofDice==7)
gameStatus =LOST;
}
displayMessage();
}
});
}
public int rollDice(){
int die1 = 1+ (int) (Math.random()*6);
int die2 = 1+ (int) (Math.random()*6);
int sum = die1+die2;
JTdice1.setText(Integer.toString(die1));
JTdice2.setText(Integer.toString(die2));
JTSUM.setText(Integer.toString(sum));
return sum;
}
public void displayMessage()
{
if(gameStatus==CONTINUE)
// showStatus("Roll Again");
// JTwin.setText("Roll Again");
// else{
if(gameStatus==WON)
// JTwin.setText("play win");
// showStatus("play win");
// else
// JTwin.setText("player lost");
// showStatus("player lost");
firstRoll =true;
}
}
/*public static void main(String arg[])
{
Crops 121=Crops();
}
*/
为什么我set 在Jpanel 的
东西不display 出来?
你们知道什么问题吗?
谢谢 |
|
|
|
|
|
|
|
发表于 28-3-2007 06:15 AM
|
显示全部楼层
|
|
|
|
|
|
| |
本周最热论坛帖子
|