java简单小程序编写,就一点点
展开全部
lz 你好
具体代码如下:
import java.awt.*;
import java.math.*;
import javax.swing.*;
import java.awt.event.*;
public class Test extends JFrame{
private JPanel panel;
private JLabel one,equ,result,img,end;
private JTextField input;
private JButton check;
private int first, second;
public Test(){
super("小程序查看器");
one = new JLabel("小程序");
one.setFont(new Font("宋体", Font.PLAIN, 15));
makeRandom();
equ = new JLabel(first + "+" + second + "=");
input = new JTextField(10);
check = new JButton("检查");
check.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int r = Integer.parseInt(input.getText());
if(first+second == r){
result.setText("you are right!");
}
else{
result.setText("you are wrong!");
}
}
});
result = new JLabel();
img = new JLabel(new ImageIcon("1.jpg"));
panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
panel.add(equ);
panel.add(input);
panel.add(check);
panel.add(result);
panel.add(img);
end = new JLabel("小程序已启动。");
end.setFont(new Font("宋体", Font.PLAIN, 15));
end.setOpaque(true);
end.setBackground(Color.WHITE);
getContentPane().add(one, BorderLayout.NORTH);
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().add(end, BorderLayout.SOUTH);
setSize(230, 230);
setLocationRelativeTo(null);
setDefaultCloseOperation(3);
setVisible(true);
}
public void makeRandom(){
first = (int)(Math.random() * 100 + 1);
second = (int)(Math.random() * 100 + 1);
}
public static void main (String[] args) {
new Test();
}
}
运行结果:
希望能帮助你哈
展开全部
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
/**
* @author Hardneedl
*/
public class CheckDemoApplet extends JApplet {
private final static Dimension SIZE = new Dimension(400,300);
public Dimension getPreferredSize() {return SIZE;}
public Dimension getMinimumSize() {return SIZE;}
public Dimension getMaximumSize() {return SIZE;}
public Dimension getSize() {return SIZE;}
private Integer[]r=new Integer[2];
/*数值生成器*/
private interface NumberCreator<T>{
T get();
}
/**
* 判定者
* @param <R> 答题者给出的结论
* @param <V> 可变参,题面
*/
private interface Judger<R,V>{
boolean accept(R r,V v);
}
static private class JudgerFactory{
private JudgerFactory() {}
static Judger<Integer,Integer[]> createJudger(){
return new Judger<Integer, Integer[]>() {
public boolean accept(Integer integer, Integer[] integers) {
return integer==integers[0]+integers[1];
}
};
}
}
/*数值生产工厂*/
static private class NumberFactory{
private NumberFactory() {}
static private NumberCreator<Integer> newCreator(){
return new NumberCreator<Integer>() {
private Random random=new Random();
public Integer get() {
return random.nextInt(100);
}
};
}
}
private class ButtonAction extends AbstractAction{
private Judger<Integer,Integer[]> judger;
private ButtonAction() {
super("check");
judger=JudgerFactory.createJudger();
}
public void actionPerformed(ActionEvent e) {
try{
Integer i = Integer.parseInt(textField.getText());
final boolean b=judger.accept(i,r);
resultLabel.setText(b?"You are right":"You are wrong");
}catch (NumberFormatException ee){
resultLabel.setText("You are wrong");
}
}
}
private JTextField textField=new JTextField(5);
private JLabel subLabel = new JLabel(),resultLabel = new JLabel();
public void init() {
setLayout(new GridLayout(0,1));
JPanel p = new JPanel();
p.add(subLabel);
p.add(textField);
JPanel p2 = new JPanel();
p2.add(new JButton(new ButtonAction()));
p2.add(resultLabel);
final Container container=getContentPane();
container.add(p);
container.add(p2);
NumberCreator<Integer> c= NumberFactory.newCreator();
subLabel.setText( (r[0]=c.get()) + " + " + (r[1]=c.get()) + " = " );
}
public void destroy() {
}
public void stop() {
}
public void start() {
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-01
展开全部
一定要显示图片?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询