急~过两天就要交了;关于JAVA的问题,高手来解下围!帮下忙
这是老师布置的一个作业,界面如图!!谨记!!是用Swing组件做的!!!是用Swing组件做的!!!是用Swing组件做的!!!老师的意思是点击A生成一个随机数,点击大于...
这是老师布置的一个作业,界面如图!!谨记!!
是用Swing组件做的!!!
是用Swing组件做的!!!
是用Swing组件做的!!!
老师的意思是点击A生成一个随机数,点击大于或小于来猜B生成的一个随机数。如果大小关系猜对就弹出一个对话框显示“恭喜你答对了”。。。否则显示“答错了”。
我一直郁闷不知道怎么样才能做出来,即点A生成随机数后,点大于、小于,再点B后,让它自己判断两文本框内字数大小....还请高手指点下。
如果答案满意再给分 展开
是用Swing组件做的!!!
是用Swing组件做的!!!
是用Swing组件做的!!!
老师的意思是点击A生成一个随机数,点击大于或小于来猜B生成的一个随机数。如果大小关系猜对就弹出一个对话框显示“恭喜你答对了”。。。否则显示“答错了”。
我一直郁闷不知道怎么样才能做出来,即点A生成随机数后,点大于、小于,再点B后,让它自己判断两文本框内字数大小....还请高手指点下。
如果答案满意再给分 展开
展开全部
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class random extends JFrame
{
private JButton a,b;
private JRadioButton RadioButton1,RadioButton2;
private JTextField text1,text2;
private Random randomnumber=new Random();
int c=0;
int a1;
int a2;
public random(String s)
{
super(s);
setSize(300, 200);
setLocation(120, 120);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
}
public void init()
{
Container con = getContentPane();
con.setLayout(new GridLayout(3,3));
a=new JButton("随即自动生成A");
b=new JButton("随即自动生明帆差成B");
RadioButton1=new JRadioButton("大于");
RadioButton2=new JRadioButton("小于");
text1=new JTextField();
text2=new JTextField();
con.add(a);
con.add(text1);
con.add(RadioButton1);
con.add(RadioButton2);
con.add(b);
con.add(text2);
con.validate(); /激皮/取得容轿岩器上的所有组件
validate();
a.addActionListener(new ActionListener()
{
public void actionPerformed ( ActionEvent event )
{
aActionPerformed( event );
}
}
);
b.addActionListener(new ActionListener()
{
public void actionPerformed ( ActionEvent event )
{
bActionPerformed( event );
}
}
);
RadioButton1.addActionListener(new ActionListener()
{
public void actionPerformed ( ActionEvent event )
{
cActionPerformed( event );
}
}
);
RadioButton2.addActionListener(new ActionListener()
{
public void actionPerformed ( ActionEvent event )
{
dActionPerformed( event );
}
}
);
}
protected void dActionPerformed(ActionEvent event)
{
c=2;
}
protected void cActionPerformed(ActionEvent event)
{
c=1;
}
protected void aActionPerformed(ActionEvent event)
{
int a1=randomnumber.nextInt(100);
text1.setText(String.valueOf(a1));
}
protected void bActionPerformed(ActionEvent event)
{
int a2=randomnumber.nextInt(100);
text2.setText(String.valueOf(a2));
int a3=Integer.parseInt(text1.getText());
System.out.print(a3);
if(c==1)
{
if(a3>a2)
{
JOptionPane.showMessageDialog(this, "对不起你答错啦","", JOptionPane.PLAIN_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(this, "恭喜你答对啦","", JOptionPane.PLAIN_MESSAGE);
}
}
else if(c==2)
{
if(a3>a2)
{
JOptionPane.showMessageDialog(this, "对不起你答错啦","", JOptionPane.PLAIN_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(this, "恭喜你答对啦","", JOptionPane.PLAIN_MESSAGE);
}
}
}
public static void main(String args[])
{
random a=new random("java小游戏");
}
}
今天花费了我一个小时写的,不知道是不是你那个意思,里面有些错误,你自己改吧,不过基本的要求差不多实现啦
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class random extends JFrame
{
private JButton a,b;
private JRadioButton RadioButton1,RadioButton2;
private JTextField text1,text2;
private Random randomnumber=new Random();
int c=0;
int a1;
int a2;
public random(String s)
{
super(s);
setSize(300, 200);
setLocation(120, 120);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
}
public void init()
{
Container con = getContentPane();
con.setLayout(new GridLayout(3,3));
a=new JButton("随即自动生成A");
b=new JButton("随即自动生明帆差成B");
RadioButton1=new JRadioButton("大于");
RadioButton2=new JRadioButton("小于");
text1=new JTextField();
text2=new JTextField();
con.add(a);
con.add(text1);
con.add(RadioButton1);
con.add(RadioButton2);
con.add(b);
con.add(text2);
con.validate(); /激皮/取得容轿岩器上的所有组件
validate();
a.addActionListener(new ActionListener()
{
public void actionPerformed ( ActionEvent event )
{
aActionPerformed( event );
}
}
);
b.addActionListener(new ActionListener()
{
public void actionPerformed ( ActionEvent event )
{
bActionPerformed( event );
}
}
);
RadioButton1.addActionListener(new ActionListener()
{
public void actionPerformed ( ActionEvent event )
{
cActionPerformed( event );
}
}
);
RadioButton2.addActionListener(new ActionListener()
{
public void actionPerformed ( ActionEvent event )
{
dActionPerformed( event );
}
}
);
}
protected void dActionPerformed(ActionEvent event)
{
c=2;
}
protected void cActionPerformed(ActionEvent event)
{
c=1;
}
protected void aActionPerformed(ActionEvent event)
{
int a1=randomnumber.nextInt(100);
text1.setText(String.valueOf(a1));
}
protected void bActionPerformed(ActionEvent event)
{
int a2=randomnumber.nextInt(100);
text2.setText(String.valueOf(a2));
int a3=Integer.parseInt(text1.getText());
System.out.print(a3);
if(c==1)
{
if(a3>a2)
{
JOptionPane.showMessageDialog(this, "对不起你答错啦","", JOptionPane.PLAIN_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(this, "恭喜你答对啦","", JOptionPane.PLAIN_MESSAGE);
}
}
else if(c==2)
{
if(a3>a2)
{
JOptionPane.showMessageDialog(this, "对不起你答错啦","", JOptionPane.PLAIN_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(this, "恭喜你答对啦","", JOptionPane.PLAIN_MESSAGE);
}
}
}
public static void main(String args[])
{
random a=new random("java小游戏");
}
}
今天花费了我一个小时写的,不知道是不是你那个意思,里面有些错误,你自己改吧,不过基本的要求差不多实现啦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Test0602GuessNumber extends JFrame {
private Random random = new Random();
public Test0602GuessNumber() {
setTitle("Java小游戏"搭伍迹);
setLayout(new GridLayout(3, 2));
JButton generateA = new JButton("随机自动生成A");
final JTextField randomA = new JTextField(10);
randomA.setEditable(false);
JButton generateB = new JButton("随机自动生知并成B"橘陵);
final JTextField randomB = new JTextField(10);
randomB.setEditable(false);
final JRadioButton bigger = new JRadioButton("大于");
final JRadioButton smaller = new JRadioButton("小于");
ButtonGroup group = new ButtonGroup();
group.add(bigger);
group.add(smaller);
add(generateA);
add(randomA);
add(bigger);
add(smaller);
add(generateB);
add(randomB);
generateA.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
randomA.setText(String.valueOf(generate()));
}
});
generateB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int generatedB = generate();
randomB.setText(String.valueOf(generatedB));
if (!bigger.isSelected() && !smaller.isSelected()) {
return;
}
int generatedA = Integer.parseInt(randomA.getText());
boolean result = false;
if ((bigger.isSelected() && generatedA > generatedB)
|| (smaller.isSelected() && generatedA < generatedB)) {
result = true;
}
JOptionPane.showOptionDialog(
Test0602GuessNumber.this,
result ? "恭喜你答对了" : "很遗憾答错了",
"猜谜结果",
JOptionPane.DEFAULT_OPTION,
result ? JOptionPane.INFORMATION_MESSAGE : JOptionPane.ERROR_MESSAGE,
null,
new String[]{"OK" },
null);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
pack();
setVisible(true);
}
private int generate() {
return this.random.nextInt(100);
}
public static void main(String[] args) {
new Test0602GuessNumber();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Test0602GuessNumber extends JFrame {
private Random random = new Random();
public Test0602GuessNumber() {
setTitle("Java小游戏"搭伍迹);
setLayout(new GridLayout(3, 2));
JButton generateA = new JButton("随机自动生成A");
final JTextField randomA = new JTextField(10);
randomA.setEditable(false);
JButton generateB = new JButton("随机自动生知并成B"橘陵);
final JTextField randomB = new JTextField(10);
randomB.setEditable(false);
final JRadioButton bigger = new JRadioButton("大于");
final JRadioButton smaller = new JRadioButton("小于");
ButtonGroup group = new ButtonGroup();
group.add(bigger);
group.add(smaller);
add(generateA);
add(randomA);
add(bigger);
add(smaller);
add(generateB);
add(randomB);
generateA.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
randomA.setText(String.valueOf(generate()));
}
});
generateB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int generatedB = generate();
randomB.setText(String.valueOf(generatedB));
if (!bigger.isSelected() && !smaller.isSelected()) {
return;
}
int generatedA = Integer.parseInt(randomA.getText());
boolean result = false;
if ((bigger.isSelected() && generatedA > generatedB)
|| (smaller.isSelected() && generatedA < generatedB)) {
result = true;
}
JOptionPane.showOptionDialog(
Test0602GuessNumber.this,
result ? "恭喜你答对了" : "很遗憾答错了",
"猜谜结果",
JOptionPane.DEFAULT_OPTION,
result ? JOptionPane.INFORMATION_MESSAGE : JOptionPane.ERROR_MESSAGE,
null,
new String[]{"OK" },
null);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
pack();
setVisible(true);
}
private int generate() {
return this.random.nextInt(100);
}
public static void main(String[] args) {
new Test0602GuessNumber();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询