资源简介

JAVA WindowBuilder 制作的简易粗暴的计算器,适合初学者。 package 计算器; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.LayoutStyle.ComponentPlacement; public class 计算器5 extends JFrame { private JPanel contentPane; private JTextField textField; private double v1, v2, value; private char operator; private boolean reop = false; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { 计算器5 frame = new 计算器5(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }

资源截图

代码片段和文件信息

package 计算器;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.Jframe;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.Layoutstyle.ComponentPlacement;

public class 计算器5 extends Jframe {

private JPanel contentPane;
private JTextField textField;
    private double v1 v2 value;
    private char operator;
    private boolean reop = false;
    

/**
 * Launch the application.
 */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
计算器5 frame = new 计算器5();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static int getSubCount_2(String str String key) {
        int count = 0;
        int index = 0;
        while ((index = str.indexOf(key index)) != -1) {
            index = index + key.length();

            count++;
        }
        return count;
    }
/**
 * Create the frame.
 */
public 计算器5() {
setResizable(false);


JButton button_4 = new JButton(“0“);
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

if(reop ==true&&oo==1) textField.setText(““);
if((textField.getText().indexOf(‘0‘)==-1||textField.getText().charAt(0)!=‘0‘||textField.getText().indexOf(‘0‘) String text = textField.getText() +‘0‘;
                textField.setText(text);oo=0;
                }

}
});

JButton btnNewButton = new JButton(“7“);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(reop ==true&&oo==1)textField.setText(““);
String text = textField.getText() +‘7‘;
                textField.setText(text);oo=0;
}
});

JButton btnNewButton_1 = new JButton(“4“);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(reop ==true&&oo==1)textField.setText(““);
String text = textField.getText() +‘4‘;
                textField.setText(text);oo=0;
}
});

JButton btnNewButton_2 = new JButton(“1“);
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(reop ==true&&oo==1)textField.setText(““);
String text = textField.getText() +‘1‘;
                textField.setText(text);oo=0;
}
});

JButton btnNewButton_3 = new JButton(“8“);
btnNewButton_3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
if(reop ==true&&oo==1)textField.setText(““);
String text = textField.getText

评论

共有 条评论