具体代码如下:
import javax.swing.*
import java.awt.event.*
import java.awt.*
public class Calculator extends JFrame implements ActionListener {
private JFrame jf
private JButton[] allButtons
private JButton clearButton
private JTextField jtf
public Calculator() {
//对图形组件实例化
jf=new JFrame("任静的计算器1.0:JAVA版")
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0)
}
})
allButtons=new JButton[16]
clearButton=new JButton("清除")
jtf=new JTextField(25)
jtf.setEditable(false)
String str="123+456-789*0.=/"
for(int i=0i<allButtons.lengthi++){
allButtons[i]=new JButton(str.substring(i,i+1))
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout())
JPanel northPanel=new JPanel()
JPanel centerPanel=new JPanel()
JPanel southPanel=new JPanel()
northPanel.setLayout(new FlowLayout())
centerPanel.setLayout(new GridLayout(4,4))
southPanel.setLayout(new FlowLayout())
northPanel.add(jtf)
for(int i=0i<16i++){
centerPanel.add(allButtons[i])
}
southPanel.add(clearButton)
jf.add(northPanel,BorderLayout.NORTH)
jf.add(centerPanel,BorderLayout.CENTER)
jf.add(southPanel,BorderLayout.SOUTH)
addEventHandler()
}
//添加事件监听
public void addEventHandler(){
jtf.addActionListener(this)
for(int i=0i<allButtons.lengthi++){
allButtons[i].addActionListener(this)
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("")
}
})
}
//事件处理
public void actionPerformed(ActionEvent e) {
//在这里完成事件处理 使计算器可以运行
String action=e.getActionCommand()
if(action=="+"||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋体",Font.BOLD,24)
jtf.setFont(f)
jtf.setBackground(new Color(0x8f,0xa0,0xfb))
for(int i=0i<16i++){
allButtons[i].setFont(f)
allButtons[i].setForeground(Color.RED)
}
}
public void showMe(){
init()
setFontAndColor()
jf.pack()
jf.setVisible(true)
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
public static void main(String[] args){
new Calculator().showMe()
}
}
1加到50求和的Java代码如下:
public int intSum(){int total = 0
for(int i = 1i<51i ++){
total += i
}
System.out.println("1加到50结果为:" + total)
return total
}
结果是:1275
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 [1] 。
Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点 [2] 。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等
package calcimport java.awt.Container
import java.awt.FlowLayout
import java.awt.GridLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.FocusEvent
import java.awt.event.FocusListener
import javax.swing.ButtonGroup
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JOptionPane
import javax.swing.JPanel
import javax.swing.JRadioButton
import javax.swing.JTextField
public class Frame extends JFrame{
private JLabel label1 = new JLabel("请输入第一个整数")
private JTextField textField1 = new JTextField("", 10)
private JLabel label2 = new JLabel("请输入第二个整数")
private JTextField textField2 = new JTextField("", 10)
private JRadioButton radioButton1 = new JRadioButton("加法")
private JRadioButton radioButton2 = new JRadioButton("减法")
private JRadioButton radioButton3 = new JRadioButton("乘法")
private JRadioButton radioButton4 = new JRadioButton("除法")
ButtonGroup group = new ButtonGroup()
private JLabel label3= new JLabel("此结果为")
private JTextField textField3 = new JTextField("",10)
private JButton button = new JButton("计算")
public Frame() {
this.setTitle("简单计算器")
this.setBounds(100, 100, 440, 300)
this.setResizable(false)
Container c = this.getContentPane()
//网格布局(5行1列)
JPanel p0 = new JPanel(new GridLayout(5, 1))
//每行中使用流式布局
JPanel p1 = new JPanel(new FlowLayout())
JPanel p2 = new JPanel(new FlowLayout())
JPanel p3 = new JPanel(new FlowLayout())
JPanel p4 = new JPanel(new FlowLayout())
JPanel p5 = new JPanel(new FlowLayout())
//将每行的布局面板放置在网格布局的面板p0上
p0.add(p1)p0.add(p2)p0.add(p3)p0.add(p4)p0.add(p5)
//将单选按钮组件编组
group.add(radioButton1)group.add(radioButton2)group.add(radioButton3)group.add(radioButton4)
p1.add(label1)
p1.add(textField1)
p2.add(label2)
p2.add(textField2)
p3.add(radioButton1)
//设置默认选中
radioButton1.setSelected(true)
p3.add(radioButton2)
p3.add(radioButton3)
p3.add(radioButton4)
p4.add(label3)
p4.add(textField3)
p5.add(button)
c.add(p0)
//文本框动作监听事件//设置只能输入数字
textField1.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
String str1 = textField1.getText().trim()
if (!str1.matches("[1-9][0-9]*")) {
JOptionPane.showMessageDialog(null, "请输入一个整数!")
textField1.setText("")
textField1.requestFocus()
}
}
@Override
public void focusGained(FocusEvent e) { }
})
textField2.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
String str2 = textField2.getText().trim()
if (!str2.matches("[1-9][0-9]*")) {
JOptionPane.showMessageDialog(null, "请输入一个整数!")
textField2.setText("")
textField2.requestFocus()
}
}
@Override
public void focusGained(FocusEvent e) { }
})
//按钮动作监听事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (radioButton1.isSelected()) {//选择加法
textField3.setText(Integer.toString(Integer.parseInt(textField1.getText()) + Integer.parseInt(textField2.getText())))
}else if (radioButton2.isSelected()) {//选择减法
textField3.setText(Integer.toString(Integer.parseInt(textField1.getText()) - Integer.parseInt(textField2.getText())))
}else if (radioButton3.isSelected()) {//选择乘法
textField3.setText(Integer.toString(Integer.parseInt(textField1.getText()) * Integer.parseInt(textField2.getText())))
}else {//选择除法
textField3.setText(Integer.toString(Integer.parseInt(textField1.getText()) / Integer.parseInt(textField2.getText())))
}
}
})
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
this.setVisible(true)
}
public static void main(String[] args) {
new Frame()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)