java简单小程序编写,就一点点
答案:3 悬赏:30 手机版
解决时间 2021-03-29 22:01
- 提问者网友:蓝琪梦莎
- 2021-03-29 11:46
java简单小程序编写,就一点点
最佳答案
- 五星知识达人网友:玩家
- 2021-03-29 13:07
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.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();
}
}
运行结果:
希望能帮助你哈
全部回答
- 1楼网友:底特律间谍
- 2021-03-29 14:58
一定要显示图片?
- 2楼网友:你可爱的野爹
- 2021-03-29 13:49
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
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 get();
}
private interface Judger{
boolean accept(R r,V v);
}
static private class JudgerFactory{
private JudgerFactory() {}
static Judger createJudger(){
return new Judger() {
public boolean accept(Integer integer, Integer[] integers) {
return integer==integers[0]+integers[1];
}
};
}
}
static private class NumberFactory{
private NumberFactory() {}
static private NumberCreator newCreator(){
return new NumberCreator() {
private Random random=new Random();
public Integer get() {
return random.nextInt(100);
}
};
}
}
private class ButtonAction extends AbstractAction{
private Judger 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 c= NumberFactory.newCreator();
subLabel.setText( (r[0]=c.get()) + " + " + (r[1]=c.get()) + " = " );
}
public void destroy() {
}
public void stop() {
}
public void start() {
}
}
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
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 get();
}
private interface Judger
boolean accept(R r,V v);
}
static private class JudgerFactory{
private JudgerFactory() {}
static Judger
return new Judger
public boolean accept(Integer integer, Integer[] integers) {
return integer==integers[0]+integers[1];
}
};
}
}
static private class NumberFactory{
private NumberFactory() {}
static private NumberCreator
return new NumberCreator
private Random random=new Random();
public Integer get() {
return random.nextInt(100);
}
};
}
}
private class ButtonAction extends AbstractAction{
private 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
subLabel.setText( (r[0]=c.get()) + " + " + (r[1]=c.get()) + " = " );
}
public void destroy() {
}
public void stop() {
}
public void start() {
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯