📄 luck52.java
字号:
import java.awt.BorderLayout;
//import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/*
* Created on 2004-11-27
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
/**
* @author louweiwei
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Luck52 {
public static void main(String[] args) {
Luck52Frame frame = new Luck52Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class Luck52Frame extends JFrame {
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;
private Luck52Panel panel;
public Luck52Frame()
{
setTitle("Pintu");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
Container contentPane = getContentPane();
panel = new Luck52Panel();
contentPane.add(panel);
}
}
class Luck52Panel extends JPanel {
public static JButton startButton,yesButton,canButton;
public static JFormattedTextField input;
public static JPanel startPanel,playPanel;
public static PicPanel picPanel;
private static int num;//要猜的数字
public static ResultDlg result;
public Luck52Panel(){
setLayout (new BorderLayout());
startButton= new JButton("开始游戏");
startButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e) {
num=(int)(Math.random()*1000);//数值范围为1000,自已设,随机的
input.setText("");
}
});
startPanel = new JPanel();
startPanel.add(startButton);
add(startPanel,BorderLayout.NORTH);
picPanel = new PicPanel();
picPanel.setSize(100,100);
add(picPanel,BorderLayout.CENTER);
playPanel = new JPanel();
input = new JFormattedTextField();
input.setValue(new Integer(num));
input.setText("请点开始游戏");
input.setColumns(8);
playPanel.add(input);
yesButton = new JButton("确定");
yesButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e) {
int a=((Number)input.getValue()).intValue();
result = new ResultDlg(a,num);
result.setTitle("提示");
result.setLocation(100,125);
result.show();
}
});
playPanel.add(yesButton);
canButton = new JButton("取消");
playPanel.add(canButton);
canButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e) {
input.setText("");
}
});
add(playPanel,BorderLayout.SOUTH);
}
}
class PicPanel extends JPanel
{
public PicPanel()
{
String filename = "63.jpg";
try
{
image =ImageIO.read(new File(filename));
}
catch(IOException exception)
{
exception.printStackTrace();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image,50,50,null);
}
private Image image;
}
class ResultDlg extends JDialog {
public ResultDlg(int a ,int num)
{
//super(owner,"About Dialog",true);
Container contentPane = getContentPane();
String slb= new String();
if(a==num)
{
slb="<html>猜对了!</html>";
}else if (a>num)
{
slb="<html>高了!</html>";
}else
{
slb="<html>低了!</html>";
}
contentPane.add(new JLabel(slb),BorderLayout.CENTER);
JPanel panel = new JPanel();
JButton ok = new JButton("ok");
ok.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
setVisible(false);
}
});
panel.add(ok);
contentPane.add(panel,BorderLayout.SOUTH);
setSize(200,150);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -