找传奇、传世资源到传世资源站!

java 计算器示例源码(入门级)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboardimport java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import javax.swing.*;import java.awt.*;import java.io.IOException;public class CalWin extends JFrame implements ActionListener,ItemListener{final int WIN_W=225,WIN_H=320;JMenuBar menubar;JMenu menuOperate,menuHelp;JMenuItem itemHelp,itemAbout,itemBug;JTextField text1,text2,text3;JPanel panelCommon,panelSuper;JRadioButton buttonCommon,buttonSuper;BugWin bugwin;Cal cal;ButtonGroup bg;public CalWin(){bugwin=new BugWin();setTitle("计算器 2.0 beta2");menubar=new JMenuBar();menuOperate=new JMenu("操作");menuHelp=new JMenu("帮助");buttonCommon=new JRadioButton("普通版本");buttonSuper=new JRadioButton("超级版本");itemHelp=new JMenuItem("获取帮助");itemAbout=new JMenuItem("关于");itemBug=new JMenuItem("报告Bug");bg=new ButtonGroup();bg.add(buttonCommon);bg.add(buttonSuper);buttonSuper.setSelected(true);menuOperate.add(buttonCommon);menuOperate.add(buttonSuper);menubar.add(menuOperate);menubar.add(menuHelp);menuHelp.add(itemHelp);menuHelp.add(itemAbout);menuHelp.add(itemBug);this.setJMenuBar(menubar);cSuper();cCommon();cal=new Cal(this);text2.addActionListener(this);itemHelp.addActionListener(this);itemAbout.addActionListener(this);buttonSuper.addItemListener(this);buttonCommon.addItemListener(this);itemBug.addActionListener(this);if(buttonSuper.isSelected()){this.add(panelSuper);}else{this.add(panelCommon);}double width = Toolkit.getDefaultToolkit().getScreenSize().width;double height =Toolkit.getDefaultToolkit().getScreenSize().height;setBounds((int)(width-WIN_W)/2,(int)(height-WIN_H)/2,WIN_W,WIN_H);validate();setResizable(false);setVisible(true);setDefaultCloseOperation(DISPOSE_ON_CLOSE);}public void actionPerformed(ActionEvent e) {if(e.getSource()==text2){cal.getResult();}else if(e.getSource()==itemHelp){JOptionPane.showMessageDialog(this, "既然点了,那就进来留个言吧");Runtime rt = Runtime.getRuntime(); try {rt.exec("rundll32 url.dll,FileProtocolHandler " "http://blog.csdn.net/leasystu");} catch (IOException ee) {}}else if(e.getSource()==itemAbout){JOptionPane.showMessageDialog(this," Java计算器 作者:_EaSy。 http://blog.csdn.net/leasystu","关于计算器",JOptionPane.INFORMATION_MESSAGE);}else if(e.getSource()==itemBug){bugwin.setVisible(true);}}public void itemStateChanged(ItemEvent ee){if(buttonSuper==ee.getSource()){this.remove(panelCommon);this.add(panelSuper,BorderLayout.CENTER);panelSuper.repaint();}else if(buttonCommon==ee.getItemSelectable()){this.remove(panelSuper);this.add(panelCommon,BorderLayout.CENTER);panelCommon.repaint();}validate();}public void cSuper(){panelSuper=new JPanel();panelSuper.setLayout(null);text2=new JTextField(20);text2.setBounds(5, 10,200,20);text2.setHorizontalAlignment(JTextField.RIGHT);text3=new JTextField(10);text3.setEditable(false);text3.setBackground(new Color(255, 255, 255));text3.setHorizontalAlignment(JTextField.RIGHT);JLabel label=new JLabel("=");label.setBounds(5,40,20,20);text3.setBounds(15, 40,180,20);JLabel label2=new JLabel("输入式子如:-12 (1 2)*2^2-5/2");label2.setBounds(4,70,200,20);panelSuper.add(text2);panelSuper.add(label);panelSuper.add(text3);panelSuper.add(label2);}public void cCommon(){panelCommon=new panelCommon(this);}public static void main(String[] args) {new CalWin();}public void f(){this.text3.setText("输入有误");/*this.panelSuper.validate();this.text3.repaint();this.text3.validate();this.panelSuper.repaint();this.panelSuper.validate();this.repaint();this.validate();*/System.out.println("执行啦");}}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复