求助 需要一个简单java计算器源代码运行出来截图啊

自学java这么久,终于编写出了自己的第一个计算器。含代码_java吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:636,051贴子:
自学java这么久,终于编写出了自己的第一个计算器。含代码收藏
用java做的计算器,可以求1到某个数之间所有数得和
比如求1到100所有数的和是50501到1000所有数得和为50050.。。。。。。下面是效果图。
2017java学习来上市公司博为峰学java,入学即签就业协议,不就业不收费,查看java课程!java好学吗?java课程来博为峰学,java工程师就业年薪十几万!
下面是代码package 小程序;import java.awt.*;import java.awt.event.*;import java.io.Fimport javax.swing.*;public class 求某个数以内所有整数的和 {private Frame f=private TextField tf1=private TextField tf2=private Button but1=private Button but2=private Dialog d=private Label lab=private Button okBut=求某个数以内所有整数的和(){init();}public void init(){f=new Frame(&求和计算器&);f.setBackground(null);f.setBounds(200, 300, 400, 200);//f.setLayout(new BoxLayout(f,2));f.setLayout(new
GridLayout(2,2));but1=new Button(&请输入你要求和的数&);tf1=new TextField(15);tf1.setFont(new Font(&宋体&,Font.BOLD,72));//三个参数
第一个 可以到word里面查
第二个就是样式
比如粗体 斜体 什么的 这个API里面有 //第三个字体大小but2=new Button(&求这个数以内所有整数的和&);tf2=new TextField(15);tf2.setFont(new Font(&宋体&,Font.BOLD,72));tf2.setBackground(Color.YELLOW);d=new Dialog(f,&错误提示&,true);d.setBounds(400,300,300,200);d.setLayout(new FlowLayout());lab=new Label();okBut=new Button(&确定&);d.add(lab);d.add(okBut);f.add(but1);f.add(tf1);f.add(but2);f.add(tf2);f.setVisible(true);myEvent();}private void myEvent(){f.addWindowListener((new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}}));okBut.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){d.setVisible(false);}});okBut.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){if(e.getKeyCode()==KeyEvent.VK_ENTER)d.setVisible(false);}});d.addWindowListener((new WindowAdapter(){public void windowClosing(WindowEvent e){d.setVisible(false);}}));tf1.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){int code=e.getKeyCode();if(code&=e.VK_0&&code&=e.VK_9||code==e.VK_BACK_SPACE){//System.out.println(e.getKeyText(e.getKeyCode())+&是非法的&);}else if(code==e.VK_ENTER){String c1=tf1.getText();int n1=Integer.parseInt(c1);int count=0;for(int i=n1;i&=0;i--){count+=i;}String c=Integer.toString(count);tf2.setText(c);}else{e.consume();String info=&您输入的&+e.getKeyText(code)+&不是一个整数,请重新输入&;lab.setText(info);d.setVisible(true);};}});but2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String c1=tf1.getText();int n1=Integer.parseInt(c1);int count=0;for(int i=n1;i&=0;i--){count+=i;}String c=Integer.toString(count);tf2.setText(c);}});}public static void main(String[]args){new 求某个数以内所有整数的和();}}
学java就好比做一个计算器,当你能用java语言做出一个科学计算器时,你的java算法应该是说的过去了,所以,我们需要以个人自主的做一个java的科学计算器为目标。
1+222222能计算得出么?
这是啥计算器啊?吓我一跳
你这个不就是带着 ui 的求和程序吗
小伙子我看你骨骼惊异,脸带傻气。我这边有本易经经10块钱卖你。
楼主给你个福利
/file/奖励你的解压密码shengzhong
看着类名我就不看啦
类名吊炸天
北大青鸟java开发入门课程,针对0基础的学员,帮助他们轻松掌握java的要领.120天极速java入门教学,您系好安全带了吗?现在报名可免费试听课程,众多特色班级供您选择!
类名可以用中文的吗?见识到了,我还真没见过,原来这也可以!!!开眼了
类名……就算了'一层层的没格式看瞎了(≥▽≤)'
不要用基本数据类型 用Bigdecimal
看到类名就吓尿了
类名,。。
掉渣天的计算器啊,从未见过这样功能的计算器,厉害啊
类名项目名全中文屌炸天!!!
呃。。。。。。如果不用gui的话二十行代码不到绝对搞定
方法名更是吊炸天
看了楼主代码长见识了
这样求和效率是有多慢
我表示,不知道该怎么说!
给你建议,初学也不要用中文的包名和类名,这是大忌!不会英文一定要逼着自己去学英文!养成良好的习惯很重要!
public class 求某个数以内所有整数的和   --那个...你能借我5毛钱吗?我真的很想吃小布丁(≥v≤)
变量、类名,都要见名知义,英文格式,一个程序员最基本的要求、
去学下高精度吧 -----------------------------------------------------------------------一个程序猿的成长博客,里面有好东西哦
判断是否是正整数有一个简洁代码,在map的源码里面有,可以去查一下
高斯的故事,楼主忘记了吗
登录百度帐号推荐应用查看: 892|回复: 3
用Java做一个简单的计算器
该用户从未签到
package gyb.import java.awt.BorderLimport java.awt.Cimport java.awt.Dimport java.awt.FlowLimport java.awt.Fimport java.awt.GridLimport java.awt.event.ActionEimport java.awt.event.ActionLimport java.util.Simport javax.swing.Iimport javax.swing.ImageIimport javax.swing.JBimport javax.swing.JFimport javax.swing.JLimport javax.swing.JMimport javax.swing.JMenuBimport javax.swing.JMenuIimport javax.swing.JPimport javax.swing.JTextFimport javax.swing.border.LineBimport javax.swing.border.TitledBpublic class JSQFrame extends JFrame implements ActionListener {public JSQFrame() {init();}private JButton[] button = new JButton[20];JTextFprivate void init() {JMenuBar bar = new JMenuBar();setJMenuBar(bar);setSize(200, 250);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setResizable(false);JMenu menu = new JMenu(&计算器&);bar.add(menu);JMenuItem exit = new JMenuItem(&退出&);field = new JTextField(18);field.setText(&0&);field.setBorder(new LineBorder(Color.LIGHT_GRAY));field.setForeground(Color.BLUE);field.setFont(new Font(&console&, Font.BOLD, 16));field.setBackground(Color.ORANGE);field.setHorizontalAlignment(JTextField.RIGHT);field.setEditable(false);menu.add(exit);setLayout(new BorderLayout());add(BorderLayout.NORTH, field);add(BorderLayout.CENTER, createCenter());add(BorderLayout.EAST, createEast());for (JButton b : button) {b.setBorder(new LineBorder(new Color(200, 200, 200), 1, false));b.addActionListener(this);}exit.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {System.exit(0);}});}private JPanel createEast() {JPanel pane = new JPanel();pane.setLayout(new GridLayout(5, 2, 5, 5));pane.setBorder(new TitledBorder(&&));button[10] = new JButton(&+&);pane.add(button[10]);button[11] = new JButton(&-&);pane.add(button[11]);button[12] = new JButton(&*&);pane.add(button[12]);button[13] = new JButton(&/&);pane.add(button[13]);button[16] = new JButton(&=&);pane.add(button[16]);button[17] = new JButton(&PI&);pane.add(button[17]);button[18] = new JButton(&x2&);pane.add(button[18]);button[19] = new JButton(&E&);pane.add(button[19]);}private JPanel createCenter() {JPanel pane = new JPanel();pane.setLayout(new GridLayout(4, 3, 10, 5));pane.setBorder(new TitledBorder(&&));button[14] = new JButton(&C&);pane.add(button[14]);button[14].setForeground(Color.RED);button[15] = new JButton(&.&);pane.add(button[15]);for (int i = 0; i & 10; i++) {button = new JButton(&& + i);pane.add(button);}}public static void main(String[] args) {JSQFrame frame = new JSQFrame();frame.setVisible(true);}public void check(){String s=&[0][0-9]&;String s2=&[0-9]+[.][0-9]*[.]&;String str=field.getText();if(str.startsWith(&0&)&&str.matches(s)){str=str.substring(1);}if(str.endsWith(&.&)&&str.matches(s2)){str=str.substring(0, str.length()-1);}field.setText(str);System.out.println(str);}public void actionPerformed(ActionEvent e) {String s = e.getActionCommand();if (&1&.equals(s)) {field.setText(field.getText() + &1&);}else if (&2&.equals(s)) {field.setText(field.getText() + &2&);}else if (&3&.equals(s)) {field.setText(field.getText() + &3&);}else if (&4&.equals(s)) {field.setText(field.getText() + &4&);}else if (&5&.equals(s)) {field.setText(field.getText() + &5&);}else if (&6&.equals(s)) {field.setText(field.getText() + &6&);}else if (&7&.equals(s)) {field.setText(field.getText() + &7&);}else if (&8&.equals(s)) {field.setText(field.getText() + &8&);}else if (&9&.equals(s)) {field.setText(field.getText() + &9&);}else if (&0&.equals(s)) {field.setText(field.getText() + &0&);}else if (&.&.equals(s)) {field.setText(field.getText() + &.&);}else if (&C&.equals(s)) {field.setText(&0&);}else if (&=&.equals(s)) {String str = field.getText();last = Double.parseDouble(str);double number = 0;switch (fh) {case 0:number = first +case 1:number = first -case 2:number = first *case 3:number = first /}field.setText(&& + number);}else if (&+&.equals(s)) {String str = field.getText();first = Double.parseDouble(str);field.setText(&&);fh = 0;}else if (&-&.equals(s)) {String str = field.getText();first = Double.parseDouble(str);field.setText(&&);fh = 1;}else if (&*&.equals(s)) {String str = field.getText();first = Double.parseDouble(str);field.setText(&&);fh = 2;}else if (&/&.equals(s)) {String str = field.getText();first = Double.parseDouble(str);field.setText(&&);fh = 3;}else if (&PI&.equals(s)) {field.setText(&& + Math.PI);}else if (&x2&.equals(s)) {String str = field.getText();first = Double.parseDouble(str);field.setText(&& + Math.pow(first, 2));}else if (&E&.equals(s)) {field.setText(&& + Math.E);}check();}}
该用户从未签到
不错不错,
TA的每日心情无聊 14:14签到天数: 1 天[LV.1]初来乍到
TA的每日心情无聊 14:14签到天数: 1 天[LV.1]初来乍到
Beijing Aptech Beida Jade Bird Information Technology Co.,Ltd
北大青鸟IT教育 北京阿博泰克北大青鸟信息技术有限公司 版权所有Java开发的一个简单截屏工具 - 邵海雄 - 博客园
随笔 - 38, 文章 - 0, 评论 - 5, 引用 - 0
import java.awt.*;import java.awt.datatransfer.DataFimport java.awt.datatransfer.Timport java.awt.datatransfer.UnsupportedFlavorEimport java.awt.event.*;import javax.swing.*;import java.io.*;import javax.imageio.*;import java.awt.image.*;/** * 截屏工具 * @className
CaptureScreen * @author
邵海雄 * @date
& 下午07:28:37 */public class CaptureScreen extends JFrame implements ActionListener {
private JButton start,
private JP
private BufferedI
private JTabbedP//一个放置很多份图片 //一个一直会递增的索引,用于标认图片
private JRadioButton java,//JAVA界面,系统界面
/** Creates a new instance of CaptureScreen */
public CaptureScreen() {
super("屏幕截取
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception exe){
exe.printStackTrace();
initWindow();
initOther();
private void initOther(){
jtp=new JTabbedPane(JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT);
private void initWindow(){
start=new JButton("开始截取");
cancel=new JButton("退出");
start.addActionListener(this);
cancel.addActionListener(this);
JPanel buttonJP=new JPanel();
c=new JPanel(new BorderLayout());
JLabel jl=new JLabel("屏幕截取", JLabel.CENTER);
JLabel jl1=new JLabel("作者: 邵海雄",JLabel.CENTER);
jl.setFont(new Font("黑体",Font.BOLD,40));
jl1.setFont(new Font("宋体",Font.BOLD,20));
jl.setForeground(Color.RED);
jl1.setForeground(Color.BLUE);
c.add(jl,BorderLayout.CENTER);
c.add(jl1,BorderLayout.SOUTH);
buttonJP.add(start);
buttonJP.add(cancel);
buttonJP.setBorder(BorderFactory.createTitledBorder("公共操作区"));
JPanel jp=new JPanel();//放置两个单选按钮的面板
jp.add(java=new JRadioButton("java界面"));
jp.add(system=new JRadioButton("系统界面",true));
java.addActionListener(this);
system.addActionListener(this);
jp.setBorder(BorderFactory.createTitledBorder("界面风格"));
ButtonGroup bg=new ButtonGroup();
bg.add(java);
bg.add(system);
JPanel all=new JPanel();
all.add(jp);
all.add(buttonJP);
this.getContentPane().add(c,BorderLayout.CENTER);
this.getContentPane().add(all,BorderLayout.SOUTH);
this.setSize(500,400);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
private void updates(){
this.setVisible(true);
if(get!=null){
//如果索引是0,则表示一张图片都没有被加入过,
//则要清除当前的东西,重新把tabpane放进来
if(index==0){
c.removeAll();
c.add(jtp,BorderLayout.CENTER);
}else{//否则的话,直接对tabpane添加面板就可以了
//就什么都不用做了
PicPanel pic=new PicPanel(get);
jtp.addTab("图片"+(++index),pic);
jtp.setSelectedComponent(pic);
SwingUtilities.updateComponentTreeUI(c); // 调整LookAndFeel(javax.swing)
private void doStart(){
this.setVisible(false);
Thread.sleep(500);//睡500毫秒是为了让主窗完全不见
Robot ro=new Robot(); // (通过本地操作)控制鼠标、键盘等实际输入源(java.awt)
Toolkit tk=Toolkit.getDefaultToolkit(); // AWT组件的抽象父类(java.awt)
Dimension di=tk.getScreenSize();
Rectangle rec=new Rectangle(0,0,di.width,di.height);
BufferedImage bi=ro.createScreenCapture(rec);
JFrame jf=new JFrame();
Temp temp=new Temp(jf,bi,di.width,di.height); // 自定义的Temp类的对象
jf.getContentPane().add(temp,BorderLayout.CENTER);
jf.setUndecorated(true);
jf.setSize(di);
jf.setVisible(true);
jf.setAlwaysOnTop(true);
} catch(Exception exe){
exe.printStackTrace();
*公用的处理保存图片的方法
void doSave(BufferedImage get){
if(get==null){
JOptionPane.showMessageDialog(this
, "图片不能为空!!", "错误", JOptionPane.ERROR_MESSAGE);
JFileChooser jfc=new JFileChooser(".");
jfc.addChoosableFileFilter(new GIFfilter());
jfc.addChoosableFileFilter(new BMPfilter());
jfc.addChoosableFileFilter(new JPGfilter());
jfc.addChoosableFileFilter(new PNGfilter());
int i=jfc.showSaveDialog(this);
if(i==JFileChooser.APPROVE_OPTION){
File file=jfc.getSelectedFile();
String about="PNG";
String ext=file.toString().toLowerCase();
javax.swing.filechooser.FileFilter ff=jfc.getFileFilter();
if(ff instanceof JPGfilter){
if(!ext.endsWith(".jpg")){
String ns=ext+".jpg";
file=new File(ns);
about="JPG";
} else if(ff instanceof PNGfilter){
if(!ext.endsWith(".png")){
String ns=ext+".png";
file=new File(ns);
about="PNG";
}else if(ff instanceof BMPfilter){
if(!ext.endsWith(".bmp")){
String ns=ext+".bmp";
file=new File(ns);
about="BMP";
}else if(ff instanceof GIFfilter){
if(!ext.endsWith(".gif")){
String ns=ext+".gif";
file=new File(ns);
about="GIF";
if(ImageIO.write(get,about,file)){
JOptionPane.showMessageDialog(this,"保存成功!");
JOptionPane.showMessageDialog(this,"保存失败!");
} catch(Exception exe){
exe.printStackTrace();
*公共的处理把当前的图片加入剪帖板的方法
public void doCopy(final BufferedImage image){
if(get==null){
JOptionPane.showMessageDialog(this
,"图片不能为空!!","错误",JOptionPane.ERROR_MESSAGE);
} // java.awt.datatransfer(接口)
Transferable trans = new Transferable(){ // 内部类
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.imageFlavor };
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.imageFlavor.equals(flavor);
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if(isDataFlavorSupported(flavor))
throw new UnsupportedFlavorException(flavor);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);
JOptionPane.showMessageDialog(this,"已复制到系统粘帖板!!");
}catch(Exception exe){
exe.printStackTrace();
JOptionPane.showMessageDialog(this
,"复制到系统粘帖板出错!!","错误",JOptionPane.ERROR_MESSAGE);
//处理关闭事件
private void doClose(Component c){
jtp.remove(c);
System.gc();
public void actionPerformed(ActionEvent ae){
Object source=ae.getSource();
if(source==start){
doStart();
} else if(source==cancel){
System.exit(0);
}else if(source==java){ // 金属外观
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception exe){
exe.printStackTrace();
}else if(source==system){ // 本地外观
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception exe){
exe.printStackTrace();
//一个内部类,它表示一个面板,一个可以被放进tabpane的面板
//也有自己的一套处理保存和复制的方法
private class PicPanel extends JPanel implements ActionListener{
JButton save,copy,//表示保存,复制,关闭的按钮
BufferedI//得到的图片
public PicPanel(BufferedImage get){
super(new BorderLayout());
initPanel();
private void initPanel(){
save=new JButton("保存(S)");
copy=new JButton("复制到剪帖板(C)");
close=new JButton("关闭(X)");
save.setMnemonic('S');
copy.setMnemonic('C');
close.setMnemonic('X');
JPanel buttonPanel=new JPanel();
buttonPanel.add(copy);
buttonPanel.add(save);
buttonPanel.add(close);
JLabel icon=new JLabel(new ImageIcon(get));
this.add(new JScrollPane(icon),BorderLayout.CENTER);
this.add(buttonPanel,BorderLayout.SOUTH);
save.addActionListener(this);
copy.addActionListener(this);
close.addActionListener(this);
public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==save){
doSave(get);
}else if(source==copy){
doCopy(get);
}else if(source==close){
doClose(this);
//保存BMP格式的过滤器
private class BMPfilter extends javax.swing.filechooser.FileFilter{
public BMPfilter(){
public boolean accept(File file){
if(file.toString().toLowerCase().endsWith(".bmp")||
file.isDirectory()){
public String getDescription(){
return "*.BMP(BMP图像)";
//保存JPG格式的过滤器
private class JPGfilter extends javax.swing.filechooser.FileFilter{
public JPGfilter(){
public boolean accept(File file){
if(file.toString().toLowerCase().endsWith(".jpg")||
file.isDirectory()){
public String getDescription(){
return "*.JPG(JPG图像)";
//保存GIF格式的过滤器
private class GIFfilter extends javax.swing.filechooser.FileFilter{
public GIFfilter(){
public boolean accept(File file){
if(file.toString().toLowerCase().endsWith(".gif")||
file.isDirectory()){
public String getDescription(){
return "*.GIF(GIF图像)";
//保存PNG格式的过滤器
private class PNGfilter extends javax.swing.filechooser.FileFilter{
public boolean accept(File file){
if(file.toString().toLowerCase().endsWith(".png")||
file.isDirectory()){
public String getDescription(){
return "*.PNG(PNG图像)";
//一个临时类,用于显示当前的屏幕图像
private class Temp extends JPanel implements MouseListener,MouseMotionListener{
private BufferedI
private int width,
private int startX,startY,endX,endY,tempX,tempY;
private JF
private Rectangle select=new Rectangle(0,0,0,0);//表示选中的区域
private Cursor cs=new Cursor(Cursor.CROSSHAIR_CURSOR);//表示一般情况下的鼠标状态(十字线)
private States current=States.DEFAULT;// 表示当前的编辑状态
private Rectangle[]//表示八个编辑点的区域
//下面四个常量,分别表示谁是被选中的那条线上的端点
public static final int START_X=1;
public static final int START_Y=2;
public static final int END_X=3;
public static final int END_Y=4;
private int currentX,currentY;//当前被选中的X和Y,只有这两个需要改变
private Point p=new Point();//当前鼠标移的地点
private boolean showTip=//是否显示提示.如果鼠标左键一按,则提示就不再显示了
public Temp(JFrame jf,BufferedImage bi,int width,int height){
this.width=
this.height=
this.addMouseListener(this);
this.addMouseMotionListener(this);
initRecs();
private void initRecs(){
rec=new Rectangle[8];
for(int i=0;i&rec.i++){
rec[i]=new Rectangle();
public void paintComponent(Graphics g){
g.drawImage(bi,0,0,width,height,this);
g.setColor(Color.RED);
g.drawLine(startX,startY,endX,startY);
g.drawLine(startX,endY,endX,endY);
g.drawLine(startX,startY,startX,endY);
g.drawLine(endX,startY,endX,endY);
int x=startX&endX?startX:endX;
int y=startY&endY?startY:endY;
select=new Rectangle(x,y,Math.abs(endX-startX),Math.abs(endY-startY));
int x1=(startX+endX)/2;
int y1=(startY+endY)/2;
g.fillRect(x1-2,startY-2,5,5);
g.fillRect(x1-2,endY-2,5,5);
g.fillRect(startX-2,y1-2,5,5);
g.fillRect(endX-2,y1-2,5,5);
g.fillRect(startX-2,startY-2,5,5);
g.fillRect(startX-2,endY-2,5,5);
g.fillRect(endX-2,startY-2,5,5);
g.fillRect(endX-2,endY-2,5,5);
rec[0]=new Rectangle(x-5,y-5,10,10);
rec[1]=new Rectangle(x1-5,y-5,10,10);
rec[2]=new Rectangle((startX&endX?startX:endX)-5,y-5,10,10);
rec[3]=new Rectangle((startX&endX?startX:endX)-5,y1-5,10,10);
rec[4]=new Rectangle((startX&endX?startX:endX)-5,(startY&endY?startY:endY)-5,10,10);
rec[5]=new Rectangle(x1-5,(startY&endY?startY:endY)-5,10,10);
rec[6]=new Rectangle(x-5,(startY&endY?startY:endY)-5,10,10);
rec[7]=new Rectangle(x-5,y1-5,10,10);
if(showTip){
g.setColor(Color.CYAN);
g.fillRect(p.x,p.y,170,20);
g.setColor(Color.RED);
g.drawRect(p.x,p.y,170,20);
g.setColor(Color.BLACK);
g.drawString("请按住鼠标左键不放选择截图区",p.x,p.y+15);
//根据东南西北等八个方向决定选中的要修改的X和Y的座标
private void initSelect(States state){
switch(state){
case DEFAULT:
currentX=0;
currentY=0;
case EAST:
currentX=(endX&startX?END_X:START_X);
currentY=0;
case WEST:
currentX=(endX&startX?START_X:END_X);
currentY=0;
case NORTH:
currentX=0;
currentY=(startY&endY?END_Y:START_Y);
case SOUTH:
currentX=0;
currentY=(startY&endY?START_Y:END_Y);
case NORTH_EAST:
currentY=(startY&endY?END_Y:START_Y);
currentX=(endX&startX?END_X:START_X);
case NORTH_WEST:
currentY=(startY&endY?END_Y:START_Y);
currentX=(endX&startX?START_X:END_X);
case SOUTH_EAST:
currentY=(startY&endY?START_Y:END_Y);
currentX=(endX&startX?END_X:START_X);
case SOUTH_WEST:
currentY=(startY&endY?START_Y:END_Y);
currentX=(endX&startX?START_X:END_X);
currentX=0;
currentY=0;
public void mouseMoved(MouseEvent me){
doMouseMoved(me);
initSelect(current); // current:当前状态(state)
if(showTip){
p=me.getPoint();
repaint();
//特意定义一个方法处理鼠标移动,是为了每次都能初始化一下所要选择的区域
private void doMouseMoved(MouseEvent me){
if(select.contains(me.getPoint())){
this.setCursor(new Cursor(Cursor.MOVE_CURSOR));
current=States.MOVE;
States[] st=States.values();
for(int i=0;i&rec.i++){
if(rec[i].contains(me.getPoint())){
current=st[i];
this.setCursor(st[i].getCursor());
this.setCursor(cs);
current=States.DEFAULT;
public void mouseExited(MouseEvent me){
public void mouseEntered(MouseEvent me){
public void mouseDragged(MouseEvent me){
int x=me.getX();
int y=me.getY();
// 分别处理一系列的(光标)状态(枚举值)
if(current==States.MOVE){
startX+=(x-tempX);
startY+=(y-tempY);
endX+=(x-tempX);
endY+=(y-tempY);
}else if(current==States.EAST||current==States.WEST){
if(currentX==START_X){
startX+=(x-tempX);
endX+=(x-tempX);
}else if(current==States.NORTH||current==States.SOUTH){
if(currentY==START_Y){
startY+=(y-tempY);
endY+=(y-tempY);
}else if(current==States.NORTH_EAST||current==States.NORTH_EAST||
current==States.SOUTH_EAST||current==States.SOUTH_WEST){
if(currentY==START_Y){
startY+=(y-tempY);
endY+=(y-tempY);
if(currentX==START_X){
startX+=(x-tempX);
endX+=(x-tempX);
startX=tempX;
startY=tempY;
endX=me.getX();
endY=me.getY();
this.repaint();
public void mousePressed(MouseEvent me){
tempX=me.getX();
tempY=me.getY();
public void mouseReleased(MouseEvent me){
if(me.isPopupTrigger()){ // 右键
if(current==States.MOVE){
p=me.getPoint();
repaint();
} else{ // 普通情况
jf.dispose();
updates();
public void mouseClicked(MouseEvent me){
if(me.getClickCount()==2){
//Rectangle rec=new Rectangle(startX,startY,Math.abs(endX-startX),Math.abs(endY-startY));
Point p=me.getPoint();
if(select.contains(p)){
if(select.x+select.width&this.getWidth()&&select.y+select.height&this.getHeight()){
get=bi.getSubimage(select.x,select.y,select.width,select.height);
jf.dispose();
updates();
int wid=select.width,het=select.
if(select.x+select.width&=this.getWidth()){
wid=this.getWidth()-select.x;
if(select.y+select.height&=this.getHeight()){
het=this.getHeight()-select.y;
get=bi.getSubimage(select.x,select.y,wid,het);
jf.dispose();
updates();
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new CaptureScreen();
//一些表示状态的枚举enum States{
NORTH_WEST(new Cursor(Cursor.NW_RESIZE_CURSOR)),//表示西北角
NORTH(new Cursor(Cursor.N_RESIZE_CURSOR)),
NORTH_EAST(new Cursor(Cursor.NE_RESIZE_CURSOR)),
EAST(new Cursor(Cursor.E_RESIZE_CURSOR)),
SOUTH_EAST(new Cursor(Cursor.SE_RESIZE_CURSOR)),
SOUTH(new Cursor(Cursor.S_RESIZE_CURSOR)),
SOUTH_WEST(new Cursor(Cursor.SW_RESIZE_CURSOR)),
WEST(new Cursor(Cursor.W_RESIZE_CURSOR)),
MOVE(new Cursor(Cursor.MOVE_CURSOR)),
DEFAULT(new Cursor(Cursor.DEFAULT_CURSOR));
States(Cursor cs){
public Cursor getCursor(){}

我要回帖

更多关于 java编写计算器 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信