目录
- 飞机大战源码及分析
- 坦克大战源码及分析
- 简单仿记事本源码及分析
- 错题本源码及分析
**引言:**代码有一段历史了,有些格式或方法可能用的不好,但是没关系,我们学习的是代码风格和编程思想
1.飞机大战源码及分析
返回目录 有空再更新
2.坦克大战源码及分析
有空再更新
返回目录
3.简单仿记事本源码及分析
返回目录 先给源码,有空再分析
package notepad;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.undo.UndoManager;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
public class Test {
public static void main(String[] args) {
new Set(595,824,0);
}
}
class homePage extends JFrame {
int location_x;
int location_y;
int width ;
int height ;
JMenuItem jm04;
int i;
public homePage(int width, int height) {
this.width = width;
this.height = height;
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
location_x = (int) (Math.random() * (scr.getWidth() - width - 50));
location_y = (int) (Math.random() * (scr.getHeight() - height - 50));
try {
URL u = new URL("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=133867479,2938309641&fm=26&gp=0.jpg");
Image image= ImageIO.read(u);
this.setIconImage(image);
}catch (Exception e){e.printStackTrace();
}
this.setTitle("鑫式--记事本");
this.setBounds(location_x, location_y, width, height);
//
final JTextArea jTextArea=new JTextArea();
jTextArea.setLineWrap(true);
jTextArea.setFont(new Font("宋体",Font.BOLD,40+Set.getSize()));
//
JMenu j01 = new JMenu("文件");
JMenuBar menuBar = new JMenuBar();
JMenuItem jm01 = new JMenuItem("新建");
jm01.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK));
jm01.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Esc();
new Set(Set.width,Set.height,0);
}
});
JMenuItem jm02 = new JMenuItem("新窗口");
jm02.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK + Event.SHIFT_MASK));
jm02.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Set(Set.width,Set.height,0);
}
});
JMenuItem jm03 = new JMenuItem("打开");
jm03.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
//
final JFileChooser fileChooser=new JFileChooser();
fileChooser.setCurrentDirectory(new File("C:\\"));
fileChooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory()||f.getName().toLowerCase().endsWith(".txt");
}
@Override
public String getDescription() {
return "*.txt";
}
});
//
jm03.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
int result=fileChooser.showOpenDialog(new JFrame());
if(result==JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try{
InputStream in=new FileInputStream(file);
StringBuffer sb=new StringBuffer();
byte[] b=new byte[1024];
int len=0;
for(;((len=in.read(b))!=-1);){
sb.append(new String(b,0,len));
jTextArea.setText(sb.toString());
}
}
catch (Exception ex){ex.printStackTrace();}
}
}
});
jm04 = new JMenuItem("保存");
jm04.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
jm04.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File file=new File("D:\\ldx");
int y=(int)(Math.random()*100);
if(new File("D:\\ldx\\a"+y+".txt").exists())
y=(int)(Math.random()*100);
File file1=new File("D:\\ldx\\a"+y+".txt");
i=y;
try{
if(!file.exists())
file.mkdir();
else if(!file1.exists())
file1.createNewFile();
FileOutputStream f=new FileOutputStream(file1);
f.write(jTextArea.getText().getBytes());f.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
});
JMenuItem jm05 = new JMenuItem("另存为");
jm05.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK + Event.SHIFT_MASK));
jm05.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{new readfile(jTextArea.getText());}catch (Exception ex){ex.printStackTrace();}
}
});
JMenuItem jm06 = new JMenuItem("页面设置");
jm06.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] obj = {"A4", "600x400", "1500x1000"};
String s = (String) JOptionPane.showInputDialog(null,
"请选择页面大小:\n", "页面设置", JOptionPane.PLAIN_MESSAGE,
new ImageIcon(""), obj, "A4");
if (s.equals("600x400")) {
new Set(600, 400,1);
} else if (s.equals("1500x1000")) {
new Set(1500, 1000,1);
} else {
new Set(595, 824,0);
}
}
});
JMenuItem jm07 = new JMenuItem("退出");
jm07.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int n=JOptionPane.showConfirmDialog(null,"是否将更改保存到D:\\ldx文件夹下随机生成.txt文件?","退出",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==0){
jm04.doClick();
}else{
System.exit(0);
}
}
});
j01.add(jm01);
j01.add(jm02);
j01.add(jm03);
j01.add(jm04);
j01.add(jm05);
j01.add(jm06);
j01.add(jm07);
//
JMenu j02 = new JMenu("编辑");
JMenuItem jm08 = new JMenuItem("粘贴");
jm08.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK ));
JMenuItem jm09 = new JMenuItem("复制");
jm09.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK ));
//
jTextArea.getDocument().addUndoableEditListener(new UndoableEditListener() {
@Override
public void undoableEditHappened(UndoableEditEvent e) {
UndoManager u=new UndoManager();
u.addEdit(e.getEdit());
}
});
//
JMenuItem jm10 = new JMenuItem("剪切");
jm10.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK ));
j02.add(jm08);
j02.add(jm09);
j02.add(jm10);
//
JMenu j03 = new JMenu("格式");
JMenuItem jm11 = new JMenuItem("字体");
jm11.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object[] obj = {"5","10", "20", "50","80","160"};
String s = (String) JOptionPane.showInputDialog(null,
"请选择字体大小:\n", "字体设置", JOptionPane.PLAIN_MESSAGE,
new ImageIcon(""), obj, "A4");
if (s.equals("5")) {
Set.setSize(-35);
} else if (s.equals("10")) {
Set.setSize(-30);
} else if (s.equals("20")) {
Set.setSize(-20);
}else if (s.equals("50")) {
Set.setSize(10);
}else if (s.equals("80")) {
Set.setSize(40);
}else if (s.equals("160")) {
Set.setSize(120);
}else{
Set.setSize(0);
}
jTextArea.setFont(new Font("宋体",Font.BOLD,40+Set.getSize()));
}
});
j03.add(jm11);
//
JMenu j04 = new JMenu("查看");
final JMenuItem jm12 = new JMenuItem("放大");
jm12.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, Event.CTRL_MASK ));
jm12.addActionListener(new ActionListener() {
int n=0;
@Override
public void actionPerformed(ActionEvent e) {
if(jm12.equals(e.getSource())&&n<12){
n++;
}
jTextArea.setFont(new Font("宋体",Font.BOLD,40+10*n));
}
});
final JMenuItem jm13 = new JMenuItem("缩小");
jm13.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, Event.CTRL_MASK ));
jm13.addActionListener(new ActionListener() {
int n=0;
@Override
public void actionPerformed(ActionEvent e) {
if(jm13.equals(e.getSource())&&n>-3){
n--;
}
jTextArea.setFont(new Font("宋体",Font.BOLD,40+10*n));
}
});
j04.add(jm12);
j04.add(jm13);
//
JMenu j05 = new JMenu("帮助");
JMenuItem jm14 = new JMenuItem("关于记事本");
jm14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"这个字体我改不了,尝试了很多方法,将就看吧\n说明1.作者较懒,只用一个父窗口,子窗口关闭既关闭所有窗口\n说明2.窗口位置随机,别问作者,问了也不知道\n说明3.页面设置只在新窗口有效\n" +
"说明4.文件默认保存位置在D盘ldx文件夹里,命名随机,如果没有找到文件可能是你没有D盘或者我代码问题,C盘我权限不够\n" +
"说明5.文件保存名未选择就随机,还有即使是空文件,或者打开过文件,退出时都会提醒是否保存,还是随机生成文件名,这个bug我懒得改,只写了主要的一些功能,因为时间不够,\n" +
"说明6.Ctrl加+号如果失效了,可能与你热键冲突了,这个我解决不了,也懒得解决,如果本产品有其他bug有空了我再更新\n注意:该记事本只用于实验,禁止发布,否则与作者无关\n作者:刘*鑫",
"说明书与声明",JOptionPane.WARNING_MESSAGE);
}
});
j05.add(jm14);
//
menuBar.add(j01);
menuBar.add(j02);
menuBar.add(j03);
menuBar.add(j04);
menuBar.add(j05);
this.add(jTextArea);
this.setJMenuBar(menuBar);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int n=JOptionPane.showConfirmDialog(null,"是否将更改保存到D:\\ldx文件夹下随机生成.txt文件?","退出",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==0){
jm04.doClick();
}else{
System.exit(0);
}
}
});
JScrollPane scrollPane=new JScrollPane(jTextArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
this.setContentPane(scrollPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void Esc() {
this.dispose();
}
}
class readfile {
public readfile(String s) {
FileNameExtensionFilter filter=new FileNameExtensionFilter("*.txt","txt");
JFileChooser fc=new JFileChooser();
fc.setCurrentDirectory(new File("C:\\"));
fc.setFileFilter(filter);
fc.setMultiSelectionEnabled(false);
int result=fc.showSaveDialog(null);
if (result==JFileChooser.APPROVE_OPTION) {
File file=fc.getSelectedFile();
if (!file.getPath().endsWith(".txt")) {
file=new File(file.getPath()+".txt");
}
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos=new FileOutputStream(file);
fos.write(s.getBytes());
fos.close();
}catch (Exception e){
System.err.println("文件创建失败:");
e.printStackTrace();
}
}
}
}
package notepad;
public class Set {
static int width;
static int height;
int i;
private static int Size;
public Set(int width,int height,int i){
this.i=i;
if(this.i==0){
this.width= 595;
this.height=824;
}else{
this.width=width;
this.height=height;}
new homePage(this.width,this.height);
}
public static int getSize() {
return Size;
}
public static void setSize(int size) {
Size = size;
}
}
4.错题本源码及分析
返回目录 先给源码,有空再分析
目录结构:
package notebook;
import javax.swing.*;
import java.math.BigDecimal;
@SuppressWarnings("all")
public class Count {
static BigDecimal i,j;
public Count(double i,double j) {
this.i=new BigDecimal(String.valueOf(i));
this.j=new BigDecimal(String.valueOf(j));
}
public static BigDecimal adds(){
return i.add(j);
}
public static BigDecimal sub(){
return i.subtract(j);
}
public static BigDecimal mul(){
return i.multiply(j);
}
public static BigDecimal div(){
try {
return i.divide(j, 1, BigDecimal.ROUND_DOWN);
}catch (Exception e){
JOptionPane.showMessageDialog(null, "被除数不能为零", "错误退出", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
return null;
}
public static int left(){
try{
int a=i.intValue();
int b=j.intValue();
return a%b;
}catch (Exception e){
JOptionPane.showMessageDialog(null, "被除数不能为零", "错误退出", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
return 0;
}
}
package notebook;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
@SuppressWarnings("all")
public class Frame extends JFrame {
jpanel jpanel;
JTextField j01;
JTextField j02;
JTextField j03;
static String sBox;
public Frame(){
this.setSize(600,400);
this.setLocationRelativeTo(null);
this.setTitle("错题本");
//
jpanel=new jpanel();
jpanel.setLayout(null);
j01=new JTextField();
j01.setBounds(40,80,100,30);
j01.setFont(new Font("宋体",Font.BOLD,20));
j01.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
String s = j01.getText();
if (s.length() >= 3) {
e.consume();
}
char c = e.getKeyChar();
if (!(c >= '0' && c <= '9')&&!(c=='.')) {
e.consume();
}
}
});
j01.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
j02.requestFocus();
}
});
//
j02=new JTextField();
j02.setBounds(220,80,100,30);
j02.setFont(new Font("宋体",Font.BOLD,20));
j02.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
String s = j02.getText();
if (s.length() >= 3) {
e.consume();
}
char c = e.getKeyChar();
if (!(c >= '0' && c <= '9')&&!(c=='.')) {
e.consume();
}
}
});
j02.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
j03.requestFocus();
}
});
//
j03=new JTextField();
j03.setBounds(380,80,100,30);
j03.setFont(new Font("宋体",Font.BOLD,20));
j03.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
String s = j03.getText();
if (s.length() >= 4) {
e.consume();
}
char c = e.getKeyChar();
if (!(c >= '0' && c <= '9')&&!(c=='.')&&!(c=='-')) {
e.consume();
}
}
});
j03.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Esc();
try{
new Self(j01.getText(), j02.getText(), j03.getText(), sBox);
new Frame();
}catch (Exception ex) {
JOptionPane.showMessageDialog(null, "输入不能为空", "错误退出", JOptionPane.ERROR_MESSAGE);
}
}
});
JLabel jl01=new JLabel("=");
jl01.setFont(new Font("宋体",Font.BOLD,60));
jl01.setBounds(330,62,60,60);
//
String[] list= {"+", "-","×","÷","%"};
final JComboBox<String> comboBox=new JComboBox<String>(list);
comboBox.setBounds(155,80,50,30);
comboBox.setFont(new Font("宋体",Font.BOLD,20));
comboBox.setSelectedIndex(0);
sBox=(String)comboBox.getSelectedItem();
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==ItemEvent.SELECTED) {
sBox =(String) comboBox.getSelectedItem();
}
}
});
//
JLabel jLabel02=new JLabel("显示详情");
jLabel02.setBounds(500,70,60,50);
jLabel02.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
new JSc();
}
});
//
this.add(j01);
this.add(j02);
this.add(j03);
this.add(jl01);
this.add(comboBox);
this.add(jLabel02);
//
this.add(jpanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void Esc(){
this.dispose();
}
}
class jpanel extends JPanel{
ImageIcon imageIcon;
Image image;
Self self;
static int x,y;
double z;
public jpanel(){
try{
URL u=new URL("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1615396319,1786475166&fm=26&gp=0.jpg");
image= ImageIO.read(u);
}catch (Exception e){
e.printStackTrace();
}
//辅助一下
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("x="+e.getX()+"y="+e.getY());
}
});
}
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, 600, 400, this);
g.drawLine(0, 70, 600, 70);
g.drawLine(0, 120, 600, 120);
g.setFont(new Font("宋体", Font.BOLD, 18));
g.drawString("自命题数目:"+self.list.size(), 10, 60);
g.drawString("做对题数目:"+x, 160, 60);
g.drawString("做错题数目:"+y, 300, 60);
Color c01 = Color.RED;
g.setColor(c01);
try{
z=(x*1.0)/(x+y)*100;
z=(double)Math.round(z*100)/100;
}catch (Exception e){
z=100;
}
g.drawString("正确率:"+z+"%", 450, 60);
//*************
g.setFont(new Font("宋体",Font.BOLD,25));
g.setColor(Color.black);
for(int i=0;i<Self.list.size();i++){
g.drawString(Self.list.get(i),50,150+25*i);
g.drawString(Self.list02.get(i),320,150+25*i);
}
}
}
class JSc extends JFrame{
public JSc() {
this.setTitle("详情");
this.setBounds(1400, 400, 500,300);
JTextArea textArea=new JTextArea();
textArea.setEditable(false);
textArea.setFont(new Font("宋体",Font.BOLD,25));
textArea.setWrapStyleWord(true);
for(int i=0;i<Self.list.size();i++) {
textArea.append("题号("+i+"):"+Self.list.get(i)+"→"+Self.list02.get(i)+"\n");
}
JScrollPane scrollPane=new JScrollPane(textArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
this.setContentPane(scrollPane);
this.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
Sce();
}
});
this.setVisible(true);
}
public void Sce(){
this.dispose();
}
}
package notebook;
import javax.swing.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("all")
class Self extends JPanel {
static List<String> list=new ArrayList<String>();
static List<String> list02=new ArrayList<String>();
boolean flag;
String i,j,k,l;
public Self(String i,String j,String k,String l){
this.i=i;
this.j=j;
this.k=k;
this.l=l;
list.add(i+l+j+"="+k);
//
new Count(Double.valueOf(i),Double.valueOf(j));
double k01=Double.valueOf(k);
String k02=String.valueOf(k01);
//
if(l.equals("+")&&(Count.adds().setScale(1,BigDecimal.ROUND_DOWN).toString().equals(k)||Count.adds().setScale(1,BigDecimal.ROUND_DOWN).toString().equals(k02))){
flag=true;
// System.out.println("回答正确");
}else if(l.equals("-")&&(Count.sub().setScale(1,BigDecimal.ROUND_DOWN).toString().equals(k)||Count.sub().setScale(1,BigDecimal.ROUND_DOWN).toString().equals(k02))){
flag=true;
// System.out.println("回答正确");
}else if(l.equals("×")&&((Count.mul().setScale(1,BigDecimal.ROUND_DOWN).toString().equals(k)||Count.mul().setScale(1,BigDecimal.ROUND_DOWN).toString().equals(k02)))){
flag=true;
// System.out.println("回答正确");
}else if(l.equals("÷")&&(Count.div().toString().equals(k)||Count.div().toString().equals(k02))){
flag=true;
// System.out.println("回答正确");
}else if(l.equals("%")&&String.valueOf(Count.left()).equals(k)){
if(k.equals("0")){
JOptionPane.showMessageDialog(null, "被除数不能为零", "错误退出", JOptionPane.ERROR_MESSAGE);
}else {
flag = true;
}
}
else {
flag=false;
// System.out.println(Count.sub());
// System.out.println("回答错误");
// System.out.println(k);
// System.out.println(k01);
// System.out.println(k02);
}
//
if(l.equals("+")){
if(flag){
list02.add("正确");
jpanel.x+=1;
jpanelSet.x+=1;
}else{
list02.add("错误,"+i+"+"+j+"="+Count.adds().setScale(1,BigDecimal.ROUND_DOWN).toString());
jpanel.y+=1;
jpanelSet.y+=1;
}
}else if(l.equals("-")){
if(flag){
list02.add("正确");
jpanel.x+=1;
jpanelSet.x+=1;
}else {
list02.add("错误,"+i + "-" + j + "=" + Count.sub().setScale(1, BigDecimal.ROUND_DOWN).toString());
jpanel.y+=1;
jpanelSet.y+=1;
}
}else if(l.equals("×")){
if(flag){
list02.add("正确");
jpanel.x+=1;
jpanelSet.x+=1;
}else {
list02.add("错误,"+i + "×" + j + "=" + Count.mul().setScale(1, BigDecimal.ROUND_DOWN).toString());
jpanel.y+=1;
jpanelSet.y+=1;
}
}else if(l.equals("÷")){
if(flag){
list02.add("正确");
jpanel.x+=1;
jpanelSet.x+=1;
}else {
list02.add("错误,"+i + "÷" + j + "=" + Count.div().setScale(1, BigDecimal.ROUND_DOWN).toString());
jpanel.y+=1;
jpanelSet.y+=1;
}
}else if(l.equals("%")){
if(flag){
list02.add("正确");
jpanel.x+=1;
jpanelSet.x+=1;
}else {
list02.add("错误,"+i + "%" + j + "="+Count.left());
jpanel.y+=1;
jpanelSet.y+=1;
}
}
//
}
}
package notebook;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import java.net.URL;
@SuppressWarnings("all")
public class Seting extends JFrame{
jpanelSet jpanelSet;
JTextField j01;
JTextField j02;
JTextField j03;
sets se;
static String sBox;
public Seting(){
this.setSize(610,400);
this.setLocationRelativeTo(null);
this.setTitle("错题本");
//
jpanelSet=new jpanelSet();
jpanelSet.setLayout(null);
//
se=new sets();
//
j01=new JTextField();
j01.setBounds(40,80,100,30);
j01.setFont(new Font("宋体",Font.BOLD,20));
j01.setFocusable(false);
j01.setText(se.bigDecimals[0].toString());
j01.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
e.consume();
}
});
j02=new JTextField();
j02.setBounds(220,80,100,30);
j02.setFont(new Font("宋体",Font.BOLD,20));
j02.setFocusable(false);
j02.setText(se.bigDecimals[1].toString());
j02.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
e.consume();
}
});
j03=new JTextField();
j03.setBounds(380,80,100,30);
j03.requestFocus();
j03.setFont(new Font("宋体",Font.BOLD,20));
j03.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
String s = j03.getText();
if (s.length() >= 4) {
e.consume();
}
char c = e.getKeyChar();
if (!(c >= '0' && c <= '9')&&!(c=='.')&&!(c=='-')) {
e.consume();
}
}
});
j03.addActionListener(new ActionListener() {
int count;
public void actionPerformed(ActionEvent e) {
Esc();
new Self(j01.getText(), j02.getText(), j03.getText(), sBox);
j01.setText(se.bigDecimals[count].toString());
j02.setText(se.bigDecimals[count+1].toString());
j03.setText("");
new Seting();
}
});
JLabel jl01=new JLabel("=");
jl01.setFont(new Font("宋体",Font.BOLD,60));
jl01.setBounds(330,62,60,60);
//
String[] list= {"+", "-","×","÷","%"};
final JComboBox<String> comboBox=new JComboBox<String>(list);
comboBox.setBounds(155,80,50,30);
comboBox.setFont(new Font("宋体",Font.BOLD,20));
comboBox.setSelectedIndex(0);
sBox=(String)comboBox.getSelectedItem();
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==ItemEvent.SELECTED){
sBox =(String) comboBox.getSelectedItem();
}
}
});
//
JLabel jLabel02=new JLabel("显示详情");
jLabel02.setBounds(500,70,60,50);
jLabel02.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
new JSc02();
}
});
//
this.add(j01);
this.add(j02);
this.add(j03);
this.add(jl01);
this.add(comboBox);
this.add(jLabel02);
//
this.add(jpanelSet);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void Esc(){
this.dispose();
}
}
class jpanelSet extends JScrollPane {
// ImageIcon imageIcon02;
Image image02;
Self self;
static int x,y;
double z;
public jpanelSet(){
try{
URL u=new URL("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1759664200,3094997462&fm=26&gp=0.jpg");
image02= ImageIO.read(u);
}catch (Exception e){
e.printStackTrace();
}
}
public void paintComponent(Graphics g){
g.drawImage(image02,0,0,610,400,this);
g.drawLine(0,70,600,70);
g.drawLine(0,120,600,120);
g.setFont(new Font("宋体",Font.BOLD,18));
g.drawString("系统命题数目:"+(x+y),10,60);
g.drawString("做对题数目:"+x,160,60);
g.drawString("做错题数目:"+y,300,60);
Color c02=Color.RED;
g.setColor(c02);
try{
z=(x*1.0)/(x+y)*100;
z=(double)Math.round(z*100)/100;
}catch (Exception e){
z=100;
}
g.drawString("正确率:"+z+"%",460,60);
//*************
g.setFont(new Font("宋体",Font.BOLD,25));
g.setColor(Color.black);
for(int i=0;i<Self.list.size();i++){
g.drawString(Self.list.get(i),50,150+25*i);
g.drawString(Self.list02.get(i),260,150+25*i);
}
}
}
class sets{
BigDecimal[] bigDecimals=new BigDecimal[2];
public sets(){
for(int i=0;i<2;i++){
double d=(int)(Math.random()*99+1);
bigDecimals[i]=new BigDecimal(""+d);
}
}
}
class JSc02 extends JFrame{
public JSc02() {
this.setTitle("详情");
this.setBounds(500, 10, 900,300);
JTextArea textArea=new JTextArea();
textArea.setEditable(false);
textArea.setFont(new Font("宋体",Font.BOLD,25));
textArea.setWrapStyleWord(true);
for(int i=0;i<Self.list.size();i++) {
textArea.append("题号("+i+"):"+Self.list.get(i)+"→"+Self.list02.get(i)+"\n");
}
JScrollPane scrollPane=new JScrollPane(textArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
this.setContentPane(scrollPane);
this.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
Sce();
}
});
this.setVisible(true);
}
public void Sce(){
this.dispose();
}
}
package notebook;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
Object[] obj={"自命题","系统命题","说明书"};
try{
String s= (String) JOptionPane.showInputDialog(null,
"请选择类型:\n", "类型选择", JOptionPane.PLAIN_MESSAGE,
new ImageIcon(""), obj, "自命题");
if(s.equals("自命题")){
new Frame();
}else if(s.equals("系统命题")){
new Seting();
}else if(s.equals("说明书")){
JOptionPane.showMessageDialog(null,"这个字体我改不了,尝试了很多方法,将就看吧\n" +
"说明1.由于我个人计算能力有限,我禁止了你们输入三个数以上\n"+"说明2.计算结果四舍五入保留一位小数\n"+
"说明3.系统命题随机,但是计算方式可以自己选"+
"注意:该错题本只用于实验,禁止发布,否则与作者无关\n作者:刘*鑫",
"说明书与声明",JOptionPane.WARNING_MESSAGE);
}
}catch (Exception e){
System.exit(0);
}
}
}
返回目录