简介可用的java源代码小程序我才写的一个乎兄简易计算器代码,能运行。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class CalculatorTest { public static void main(String[] args) { CalculatorFrame frame = new CalculatorFrame(); frame.setDefaultCloseOperation(JFrame.
可用的java源代码小程序
我才写的一个乎兄简易计算器代码,能运行。
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class CalculatorTest { public static void main(String[] args) { Calculator = new Calculator (); .setDefaultCloseOperation(J .EXIT_ON_CLOSE); .setVisible(true); // .dispose(); }}class Calculator extends J { public Calculator (){ //获取屏幕尺寸 Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int screenWidth = screenSize.width; int screenheight = screenSize.height; //使框架定位于屏幕正中 setBounds(screenWidth/4,screenheight/4,screenWidth /2,screenheight/2 ); set ("kangta's Calculator"); CalculatorPanel panel = new CalculatorPanel(); add(panel); pack(); /*colorPanel colorPanel = new colorPanel(); panel.setBackground(Color.green); add(colorPanel); */ }}/*class colorPanel extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); }}*///生成一个计算器并显示结果的面板class CalculatorPanel extends JPanel{ public CalculatorPanel(){ setLayout(new BorderLayout()); result = 0; lastCommand = "="; start = true; //添加显示按钮 display = new JButton("0"); display.setEnabled(false); add(display,BorderLayout.NORTH); ActionListener insert = new InsertAction(); ActionListener command = new CommandAction(); //添加4x4按钮 panel1 = new JPanel(); panel1.setLayout(new GridLayout(4,4)); addButton("7",insert); addButton("8",insert); addButton("9",insert); addButton("/"岁链袭,command); addButton("4",insert); addButton("5",insert); addButton("6",insert); addButton("*",command); addButton("1",insert); addButton("2",insert); addButton("3"唤侍,insert); addButton("-",command); addButton("0",insert); addButton(".",insert); addButton("=",command); addButton("+",command); add(panel1,BorderLayout.CENTER); } //将按钮添加到面板中央 private void addButton(String label,ActionListener listener){ JButton button = new JButton(label); button.addActionListener(listener); panel1.add(button); } private class InsertAction implements ActionListener{ public void actionPerformed(ActionEvent event){ String input = event.getActionCommand(); if(start){ display.setText(""); start = false; } display.setText(display.getText() + input); } } private class CommandAction implements ActionListener{ public void actionPerformed(ActionEvent event){ String command = event.getActionCommand(); if(start){ if(command.equals("-")){ display.setText(command); start = false; } else lastCommand = command; } else{ calculate(Double.parseDouble(display.getText())); lastCommand = command; start = true; } } } public void calculate(double x){ if(lastCommand.equals("+")) result += x; else if(lastCommand.equals("-")) result -=x; else if(lastCommand.equals("*")) result *=x; else if(lastCommand.equals("/")) result /=x; else if(lastCommand.equals("=")) result =x; display.setText(""+result); } private JButton display; private JPanel panel1; private double result; private String lastCommand; private boolean start;}
java 小程序源代码
求素数,比较经明核渣典的,下面是代码及注释================================================public class Sushu { /** * 判断一个数是不是素数 * @param a 被判断的数 * @return 是素数返回真氏洞 */ public boolean isSuhu(int a){ boolean isSushu=true; //根据素数的性质判断一个数是否为素数 for(int i=2;i
求java小程序源代码,图形界面程序
一个Button或MenuItem组件,用户点击后弹出对话神禅框,显示软件版游野本信息,作者学号与游磨尘姓名,并提供说明文档