很冲动啊,第一次写出了一个有图形界面的,可是,才发觉。java用来写桌面程序,很鸡肋。。这不是他的强项啊。郁闷,有得必有失。
当然,不凡看到过大牛写过一个:
YOYOPlayer:Java 音乐播放器
2008-03-18 Toy Posted in Apps, Music PlayerRSSTrackback
YOYOPlayer 是一个使用 Java 语言写成的音乐播放器。该播放器目前包括自动保存当前配置,同步显示歌词,ID3v1、ID3v2、APEv2、Vorbis 标签编辑,歌曲进度显示,校正歌词进度,拖放支持,调音器,能够播放本地和网络歌曲,支持 snd、aifc、aif、wav、au、mp1、mp2、mp3、ogg、spx、flac、ape、mac 音频格式等功能。
牛吧。界面很漂亮。可是运行时候占CPU的资源还是太大了,毕竟还要考虑虚拟机嘛。
哈哈,不管怎么说,看看代码,还是看出了巨大的差距。我还菜得狠哦。下面奉上稚嫩的代码:
package first;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Calculator extends JFrame implements ActionListener
{
private JTextField cchundisplay;
//private JButton[] cchunbutton=new JButton[16];
private Container pane;
private GridBagConstraints constraints;
private GridBagLayout layout;
private String numStr1="";
private String numStr2="";
private char op;
private boolean firstInput=true;
public Calculator()
{
super("不靠谱计算器");
pane=getContentPane();
layout=new GridBagLayout();
pane.setLayout(layout);
constraints=new GridBagConstraints();
cchundisplay=new JTextField(20);//设置文本框
cchundisplay.setHorizontalAlignment(JTextField.RIGHT);
constraints.gridx=0;
constraints.gridy=0;
constraints.gridwidth=4;
constraints.gridheight=2;
constraints.fill=GridBagConstraints.BOTH;
constraints.weightx=1;//附权值,将空白处分配给网格单元
constraints.weighty=1;
layout.setConstraints(cchundisplay,constraints);//于布局管理器联系关系
pane.add(cchundisplay);
addButton("CE",0,2,1,1);
addButton("C",1,2,1,1);
addButton("delete",2,2,1,1);
addButton("(-/+)",3,2,1,1);
addButton("7",0,3,1,1);
addButton("8",1,3,1,1);
addButton("9",2,3,1,1);
addButton("/",3,3,1,1);
addButton("4",0,4,1,1);
addButton("5",1,4,1,1);
addButton("6",2,4,1,1);
addButton("*",3,4,1,1);
addButton("1",0,5,1,1);
addButton("2",1,5,1,1);
addButton("3",2,5,1,1);
addButton("--",3,5,1,1);
addButton("0",0,6,2,1);
addButton(".",2,6,1,1);
addButton("+",3,6,1,1);
addButton("sqrt",0,7,2,1);
addButton("==",2,7,2,1);
setSize(450,400);
setVisible(true);
}
public void addButton(String label,int gridx,int gridy,int gridwidth,int gridheight)
{
JButton button=new JButton(label);
constraints.gridx=gridx;
constraints.gridy=gridy;
constraints.gridwidth=gridwidth;
constraints.gridheight=gridheight;
constraints.fill=GridBagConstraints.BOTH;
button.addActionListener(this);
layout.setConstraints(button,constraints);
pane.add(button);
}
public void actionPerformed(ActionEvent e)
{
String resultStr;
String str2=(e.getActionCommand());
char ch=str2.charAt(0);
/* if (firstInput)
{
cchundisplay.setText("");
firstInput=false;
if(str2.equals("+/-"))
cchundisplay.setText(cchundisplay.getText()+"-");
}*/
switch(ch)
{
case '0':case '1':case '2':case '3':
case '4':case '5':case '6':case '7':
case '8':case '9':case '.':
if(firstInput)
{
numStr1+=ch;
cchundisplay.setText(numStr1);
}
else
{
numStr2+=ch;
cchundisplay.setText(numStr2);
}
break;
case '+':case '-':case '*':case '/':
op=ch;
firstInput=false;
break;
case '=':
resultStr=evaluate();
cchundisplay.setText(resultStr);
numStr1=resultStr;
numStr2="";
firstInput=false;
break;
case 'C':
if(!str2.equals('('))
{ cchundisplay.setText("0");
numStr1="";
numStr2="";
firstInput=true;}
break;
case 'd':
if(!str2.equals('('))
{
String str1=cchundisplay.getText();
if(str1.length()>0)
cchundisplay.setText(str1.substring(0,str1.length()-1));
}
break;
case '(':
cchundisplay.setText(cchundisplay.getText()+"-");
}
}
private String evaluate()
{
final char beep='\u0007';
try
{
double A=Double.parseDouble(numStr1);
double B=Double.parseDouble(numStr2);
double result=0;
switch(op)
{
case '+':result=A+B;
break;
case '-':result=A-B;
break;
case '*':result=A*B;
break;
case '/':result=A/B;
break;
}
return String.valueOf(result);
}
catch(ArithmeticException e)
{
System.out.print(beep);
return "ERROR"+e.getMessage();
}
catch(NumberFormatException e)
{
System.out.print(beep);
if(numStr1.equals(""))
return "ERROR";
else
return "ERROR";
}
catch (Exception e)
{
System.out.print(beep);
return "eFFOF";
}
}
public static void main(String[] args)
{
Calculator C=new Calculator();
}
}