具体代码如下:
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()
}
}
java实现先输入10个int类型的参数,之后循环判断找到最大值和最小值,保存起来和其他的值进行比较。代码如下:public class Test{
public static void main(String args[]){
int i,min,max
int A[] = new int[10]
Scanner input=new Scanner(System.in)
for(int j=0,j<10j++){//输入10个数
System.out.println("请输入一个数字:")
int length=input.nextInt()//输入一个数字
A[i] =length
}
min=max=A[0]
System.out.print("数组A的元素包括:")
int j =0
int n =0
for(i=0i<A.lengthi++)
{
System.out.print(A[i]+" ")
if(A[i]>max) // 判断最大值
j =i
max=A[i]
if(A[i]<min) // 判断最小值
min=A[i]
n =i
}
System.out.println("\n数组的最大值是:"+max+".数组的位置是:"+(j+1))// 输出最大值和最大值的位置
System.out.println("数组的最小值是:"+min+".数组的位置是:"+(n+1))// 输出最小值
}
}
先从sun公司官方网站下载需要的程序工具;在计算机上配置java运行环境,和其他编程,如c,c++不同(只需安装一个软件,如vc6.0),其需要改变系统的相关设置,可以参考相关资料;
最后即可以编写程序代码(和其他语言一样),编译,运行。
注:在我空间博客有一些以前编写的小java程序,都是运行成功的,没事时,可以随便看看,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)