该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
哈哈
哥们刚好编了个
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class guessNumber extends JFrame{
private Container container;
private JTextArea textArea;
private JLabel showArea;
private JButton show;
private JTextField cinGuess,outResult;
private GridLayout gridLayout;
private JPanel panel;
public guessNumber(){
super("猜数程序");
container=getContentPane();
gridLayout=new GridLayout(4,1);
container.setLayout(gridLayout);
textArea=new JTextArea("我有一个在0-1000范围内的数字,\n你能猜出来吗?请输你猜的数字");
textArea.setEditable(false);
container.add(textArea);
//设置panel
panel=new JPanel();
panel.setBackground(Color.yellow);
panel.setLayout(new FlowLayout());
showArea=new JLabel("请你输入你猜的数字:");
panel.add(showArea);
//加输入区域
cinGuess=new JTextField(10);
panel.add(cinGuess);
container.add(panel);
//container.add(cinGuess);
outResult=new JTextField();
outResult.setEditable(false);
container.add(outResult);
show=new JButton("重新开始");
container.add(show);
//添加监听器
show.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
int dataRandom=new Random().nextInt(1001);
String outString=new String();
outString="这个数字是"+String.valueOf(dataRandom);
if(cinGuess.getText()==outString)
{
outString+=" 恭喜你,你猜对了";
outResult.setBackground(Color.blue);
}
else
{
outString+=" 对不起,你猜错了";
outResult.setBackground(Color.red);
}
outResult.setText(outString);
outResult.setText(outString);
}
}
);
setSize(300,400);
setVisible(true);
}//end structor
public static void main(String args[]){
guessNumber application=new guessNumber();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}