有图形界面的猜数字java代码

 我来答
百度网友12ee0fd
2012-11-04 · TA获得超过4.7万个赞
知道大有可为答主
回答量:1.1万
采纳率:66%
帮助的人:1973万
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

public class Guessing {

public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
} catch (final UnsupportedLookAndFeelException xe) {
assert false : "It should not reach here";
}
final JFrame frame = new JFrame("我猜我猜我猜猜猜");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GuessingPanel());
frame.pack();
frame.setVisible(true);
}
});
}

static class GuessingPanel extends JPanel {
public GuessingPanel() {
final Border border = userInput.getBorder();
userInput.setBorder(BorderFactory.createTitledBorder(border, "输入你的猜测"));
userInput.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(final DocumentEvent e) {
changeButtonState();
}

@Override
public void insertUpdate(final DocumentEvent e) {
changeButtonState();
}

@Override
public void changedUpdate(final DocumentEvent e) {
changeButtonState();
}

private void changeButtonState() {
guessButton.setEnabled(userInput.getText().isEmpty());
}
});
startButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(final ActionEvent e) {
generate();
}
});
guessButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(final ActionEvent e) {
doGuessing();
}
});
userInput.setEnabled(false);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(info);
add(startButton);
add(userInput);
add(guessButton);
add(message);
}

private void generate() {
final StringBuilder builder = new StringBuilder(50);
builder.append("谜底是一个");
do {
mystery = generator.nextInt(1000);
if (mystery > 0) {
builder.append("正");
break;
} else if (mystery < 0) {
builder.append("负");
break;
}
} while (mystery != 0);
builder.append(Integer.toString(Math.abs(mystery)).length());
builder.append("位整数");
info.setText(builder.toString());
userInput.setEnabled(true);
userInput.setText("");
}

private void doGuessing() {
try {
final int user_input = Integer.parseInt(userInput.getText());
if (user_input > mystery) {
message.setText("你输入的数大了!");
return;
}
if (user_input < mystery) {
message.setText("你输入的数小了!");
return;
}
message.setText("恭喜你猜中谜底!!");
// userInput.setText("");
userInput.setEnabled(false);
guessButton.setEnabled(false);
} catch (final NumberFormatException e) {
message.setText("你确信你输入的是一个整数!!!");
userInput.setText("");
userInput.requestFocus();
}
}

private int mystery;
private JLabel info = new JLabel("开始猜数游戏");
private JTextField userInput = new JTextField(10);
private JLabel message = new JLabel(new Date().toString());
private Random generator = new Random();
private JButton startButton = new JButton("开始");
private JButton guessButton = new JButton("我猜");
private static final long serialVersionUID = -1111111111111111L;
}
}
追问
快了,和我自己做的界面差不多,就是当输入数值大了让文本框显示red,输入数值小了显示green这个让我很头疼啊。不老兄作的也不错了。解决了这问题给分了
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式