
给Java代码的的每一句做一个注释,很简单的
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassTestextendsJFr...
import java.awt.*;
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(300, 300);
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 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(300, 300);
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();
}
} 展开
2个回答
展开全部
import java.awt.*;
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; 定义text框
private JButton check; 定义按钮
private int first, second; 定义int变量
public Test(){
super("小程序查看器"); 初始化 jframe的名称
one = new JLabel("小程序");初始化 标签的名称
one.setFont(new Font("宋体", Font.PLAIN, 15)); 设置字体
makeRandom(); 调用方法makeRandom
equ = new JLabel(first + "+" + second + "="); 设置equ标签的内容,内容为2个随机数相加的等式
input = new JTextField(10); 初始化输入框。
check = new JButton("检查"); 初始化按钮
check.addActionListener(new ActionListener(){ 给按钮添加点击事件,如果input中的值为first和second的和,result中显示you are right!,否则显示you are wrong!
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);
//设置jframe的大小及常规属性
setSize(300, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(3); 用于关闭
setVisible(true); 设置可见
}
给first和second初始化的函数
public void makeRandom(){
first = (int)(Math.random() * 100 + 1);
second = (int)(Math.random() * 100 + 1);
}
调用Test
public static void main (String[] args) {
new Test();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询