如题,不用太漂亮的,只要功能全就行。谢谢啦(^_^)
解决方案 »
- 构造器是不是static的
- 如何用将HTML中<BODY></body>之间的值取出来?
- A fatal error has been detected by the Java Runtime Environment
- 关于java参数的问题
- 靠死,JAVA居然不支持创建泛型数组!!!
- Eclipse问题
- 小弟想把大程序转换为applet请大侠帮个忙!
- 新手求救,如何实现网页上显示在线用户和网页统计点击数
- 在JSP中怎么调用OCX控件的方法,解决后马上给分
- 我想在JInternalFrame中放入JDialog,可是每次最小化后,再最大化JDialog就跑到底面处了,出不来,不知各位有没有好的解决方按?
- 一个JAVA问题
- 关于java日期的小结
give me your email
我要份,
随便写的好多了,从中可以学到设计模式,虽然开始不见得知道什么设计模式,但是
这种代码结构/类之间的关系定义等等,积累多了非常有好处。
C:\Program Files\Java\jdk1.5.0\demo\jfc\Notepad
give me your email看到的话给我一份 谢谢[email protected]
import java.awt.event.*;
import javax.swing.*;
import java.io.*;class TextEditorFrame extends JFrame
{ File file = null;
Color color = Color.black;
TextEditorFrame(){
initTextPane();
initMenu();
initAboutDialog();
initToolBar();
} void initTextPane(){
getContentPane().add( new JScrollPane(text) );
} JTextPane text = new JTextPane();
JFileChooser filechooser = new JFileChooser();
JColorChooser colorchooser = new JColorChooser();
JDialog about = new JDialog(this);
JMenuBar menubar = new JMenuBar(); JMenu [] menus = new JMenu[] {
new JMenu("File"),
new JMenu("Edit"),
new JMenu("Help")
};
JMenuItem menuitems [][] = new JMenuItem[][]{{
new JMenuItem("New"),
new JMenuItem("Open..."),
new JMenuItem("Save..."),
new JMenuItem("Exit")},{
new JMenuItem("Copy"),
new JMenuItem("Cut"),
new JMenuItem("Paste"),
new JMenuItem("Color...")},{
new JMenuItem("About")}
}; void initMenu(){
for( int i=0; i<menus.length; i++ ){
menubar.add( menus[i] );
for( int j=0; j<menuitems[i].length; j++ ){
menus[i].add( menuitems[i][j] );
menuitems[i][j].addActionListener( action );
}
}
this.setJMenuBar( menubar );
} ActionListener action = new ActionListener(){
public void actionPerformed( ActionEvent e ){
JMenuItem mi = (JMenuItem)e.getSource();
String id = mi.getText();
if( id.equals("New" )){
text.setText("");
file = null;
}else if( id.equals("Open...")){
if( file != null ) filechooser.setSelectedFile( file );
int returnVal = filechooser.showOpenDialog(
TextEditorFrame.this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
file = filechooser.getSelectedFile();
openFile();
}
}else if( id.equals("Save...")){
if( file != null ) filechooser.setSelectedFile( file );
int returnVal = filechooser.showSaveDialog(
TextEditorFrame.this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
file = filechooser.getSelectedFile();
saveFile();
}
}else if( id.equals("Exit")){
System.exit(0);
}else if( id.equals("Cut")){
text.cut();
}else if( id.equals("Copy")){
text.copy();
}else if( id.equals("Paste")){
text.paste();
}else if( id.equals("Color...")){
color = JColorChooser.showDialog(
TextEditorFrame.this, "", color );
text.setForeground(color);
}else if( id.equals("About")){
about.setSize(100,50);
about.show();
}
}
}; void saveFile(){
try{
FileWriter fw = new FileWriter( file );
fw.write( text.getText() );
fw.close();
}catch(Exception e ){ e.printStackTrace(); }
}
void openFile(){
try{
FileReader fr = new FileReader( file );
int len = (int) file.length();
char [] buffer = new char[len];
fr.read( buffer, 0, len );
fr.close();
text.setText( new String( buffer ) );
}catch(Exception e ){ e.printStackTrace(); }
} void initAboutDialog(){
about.getContentPane().add( new JLabel("简单编辑器 V1.0") );
about.setModal( true );
about.setSize(100,50 );
}
JToolBar toolbar = new JToolBar();
JButton [] buttons = new JButton[] {
new JButton( "", new ImageIcon("POINTCURSOR.gif") ),
new JButton( "", new ImageIcon("pointer.gif") ),
new JButton( "", new ImageIcon("PolygonLineTrackerBean.gif") )
};
void initToolBar(){
for( int i=0; i<buttons.length; i++)
toolbar.add( buttons[i] );
buttons[0].setToolTipText( "copy" );
buttons[0].addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
text.copy();
}
});
buttons[1].setToolTipText( "cut" );
buttons[1].addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
text.cut();
}
});
buttons[2].setToolTipText( "paste" );
buttons[2].addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
text.paste();
}
});
this.getContentPane().add( toolbar, BorderLayout.NORTH );
toolbar.setRollover(true);
}
}public class ex_10_3_5
{
public static void main( String [] args){
TextEditorFrame f = new TextEditorFrame();
f.setTitle( "TongShu");
f.setSize( 400, 300 );
f.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){ System.exit(0);}
});
f.show();
}
}
import java.awt.event.*;
import javax.swing.*;
import java.io.*; class TextEditorFrame extends JFrame
{ File file = null;
Color color = Color.black; TextEditorFrame(){
initTextPane();
initMenu();
initAboutDialog();
initToolBar();
} void initTextPane(){
getContentPane().add( new JScrollPane(text) );
} JTextPane text = new JTextPane();
JFileChooser filechooser = new JFileChooser();
JColorChooser colorchooser = new JColorChooser();
JDialog about = new JDialog(this);
JMenuBar menubar = new JMenuBar(); JMenu [] menus = new JMenu[] {
new JMenu("File"),
new JMenu("Edit"),
new JMenu("Help")
};
JMenuItem menuitems [][] = new JMenuItem[][]{{
new JMenuItem("New"),
new JMenuItem("Open..."),
new JMenuItem("Save..."),
new JMenuItem("Exit")},{
new JMenuItem("Copy"),
new JMenuItem("Cut"),
new JMenuItem("Paste"),
new JMenuItem("Color...")},{
new JMenuItem("About")}
}; void initMenu(){
for( int i=0; i <menus.length; i++ ){
menubar.add( menus[i] );
for( int j=0; j <menuitems[i].length; j++ ){
menus[i].add( menuitems[i][j] );
menuitems[i][j].addActionListener( action );
}
}
this.setJMenuBar( menubar );
} ActionListener action = new ActionListener(){
public void actionPerformed( ActionEvent e ){
JMenuItem mi = (JMenuItem)e.getSource();
String id = mi.getText();
if( id.equals("New" )){
text.setText("");
file = null;
}else if( id.equals("Open...")){
if( file != null ) filechooser.setSelectedFile( file );
int returnVal = filechooser.showOpenDialog(
TextEditorFrame.this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
file = filechooser.getSelectedFile();
openFile();
}
}else if( id.equals("Save...")){
if( file != null ) filechooser.setSelectedFile( file );
int returnVal = filechooser.showSaveDialog(
TextEditorFrame.this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
file = filechooser.getSelectedFile();
saveFile();
}
}else if( id.equals("Exit")){
System.exit(0);
}else if( id.equals("Cut")){
text.cut();
}else if( id.equals("Copy")){
text.copy();
}else if( id.equals("Paste")){
text.paste();
}else if( id.equals("Color...")){
color = JColorChooser.showDialog(
TextEditorFrame.this, "", color );
text.setForeground(color);
}else if( id.equals("About")){
about.setSize(100,50);
about.show();
}
}
}; void saveFile(){
try{
FileWriter fw = new FileWriter( file );
fw.write( text.getText() );
fw.close();
}catch(Exception e ){ e.printStackTrace(); }
}
void openFile(){
try{
FileReader fr = new FileReader( file );
int len = (int) file.length();
char [] buffer = new char[len];
fr.read( buffer, 0, len );
fr.close();
text.setText( new String( buffer ) );
}catch(Exception e ){ e.printStackTrace(); }
} void initAboutDialog(){
about.getContentPane().add( new JLabel("简单编辑器 V1.0") );
about.setModal( true );
about.setSize(100,50 );
} JToolBar toolbar = new JToolBar();
JButton [] buttons = new JButton[] {
new JButton( "", new ImageIcon("POINTCURSOR.gif") ),
new JButton( "", new ImageIcon("pointer.gif") ),
new JButton( "", new ImageIcon("PolygonLineTrackerBean.gif") )
}; void initToolBar(){
for( int i=0; i <buttons.length; i++)
toolbar.add( buttons[i] );
buttons[0].setToolTipText( "copy" );
buttons[0].addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
text.copy();
}
});
buttons[1].setToolTipText( "cut" );
buttons[1].addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
text.cut();
}
});
buttons[2].setToolTipText( "paste" );
buttons[2].addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
text.paste();
}
});
this.getContentPane().add( toolbar, BorderLayout.NORTH );
toolbar.setRollover(true);
}
} public class ex_10_3_5
{
public static void main( String [] args){
TextEditorFrame f = new TextEditorFrame();
f.setTitle( "TongShu");
f.setSize( 400, 300 );
f.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){ System.exit(0);}
});
f.show();
}
}
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
class EditBook
{
public static void main(String[] args)
{
Edit e=new Edit();
e.run();
}
}
class Edit extends JFrame
{
JMenuBar jmb;
JMenu file;
JMenu compile;
JMenu tool;
JMenu help;
JMenuItem newF;
JMenuItem open;
JMenuItem save;
JMenuItem exit;
JMenuItem copy;
JMenuItem cut;
JMenuItem paste;
JMenuItem s_all;
JMenuItem color;
JMenuItem toolb;
JMenuItem toolc;
JMenuItem h;
JTextArea jta;
JDialog jd;
JPanel jp;
JToolBar jtb;
String message;
JFileChooser jfc;
Color co;
File path;
String messageP;
Boolean flag;
public void run(){
flag=true; jmb=new JMenuBar();
jp=new JPanel();
file=new JMenu("文件");
compile=new JMenu("编辑");
tool=new JMenu("工具");
help=new JMenu("帮助");
newF=new JMenuItem("新建");
open=new JMenuItem("打开");
save=new JMenuItem("保存");
exit=new JMenuItem("退出"); copy=new JMenuItem("Copy");
cut=new JMenuItem("Cut");
paste=new JMenuItem("Paste");
s_all=new JMenuItem("保存全部");
color=new JMenuItem("Color"); toolb=new JMenuItem("MS记事本");
toolc=new JMenuItem("MS计算器");
h=new JMenuItem("关于"); jtb=new JToolBar("MyJToolBar");
jta=new JTextArea(20,30); //文件
file.add(newF);
file.add(open); //////////如何显示文件内容FileReader 打开中文的
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jfc=new JFileChooser();
jfc.showOpenDialog(Edit.this);
FileReader is=null;
try
{
jta.setText(" ");
is=new FileReader(jfc.getSelectedFile());
int i=0;
int returnVal=0;
if(returnVal==JFileChooser.APPROVE_OPTION){
while((i=is.read())!=-1){
char[] a=new char[1];
a[0]=(char)i;
jta.append(new String(a));
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally{
try
{
if(null!=is){
is.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
});
file.add(save);
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FileOutputStream fos=null;
int returnVal=0;
try
{
if(flag){
jfc=new JFileChooser();
jfc.showSaveDialog(Edit.this);
path=jfc.getSelectedFile();
if(returnVal==JFileChooser.APPROVE_OPTION){
String message=jta.getText();
fos=new FileOutputStream(messageP);
fos.write(message.getBytes());
messageP=path.getAbsolutePath();
System.out.println(messageP);
}
}
else{
jfc=new JFileChooser(messageP);
jfc.showSaveDialog(Edit.this);
fos=new FileOutputStream(messageP);
fos.write(message.getBytes());
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally{
try
{
if(null!=fos){
fos.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
});
file.add(exit);
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
//编辑
compile.add(copy);
copy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jta.copy();
}
});
compile.add(cut);
cut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jta.cut();
}
});
compile.add(paste);
paste.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jta.paste();
}
});
compile.add(s_all);
s_all.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jta.selectAll();
}
});
compile.add(color);
color.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JColorChooser c=new JColorChooser();
co=c.showDialog(Edit.this,"颜色设置",Color.red);
int returnVal=0;
jta.setForeground(co);
}
});
//工具
tool.add(toolb);
tool.add(toolc);
toolc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Calculate cc=new Calculate();
cc.display();
}
});
//帮助
help.add(h); jmb.add(file);
jmb.add(compile);
jmb.add(tool);
jmb.add(help); jtb.add(new JButton(new ImageIcon("copy.jpg")));
jtb.add(new JButton(new ImageIcon("copy.jpg")));
jtb.add(new JButton(new ImageIcon("copy.jpg"))); jp.setLayout(new BorderLayout());
jp.add("North",jtb);
jp.add("Center",jta);
this.setJMenuBar(jmb);
this.add(jp);
this.pack();
this.setTitle("简单文本编辑器");
this.setLocation(100,150);
this.setVisible(true);
}
}
/*
简单计算器的功能的实现
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public class Calculate implements ActionListener {
Frame f1;
Panel p1=new Panel();
Panel p2=new Panel();
double x,y=0;
int f=0;
String s="";
TextField tf1,tf2;
Button b1,b2,b3,b4,b5,b6;
Button b[]=new Button[11]; public static void main(String args[]) {
(new Calculate()).display();
} public void display(){
f1=new Frame("窗口界面计算器");
f1.setSize(260,150);
f1.setLocation(320,240);
f1.setBackground(Color.orange);
f1.setLayout(new FlowLayout(FlowLayout.LEFT));
f1.add(p1);
f1.add(p2);
p1.setLayout(new GridLayout(2,1));
p2.setLayout(new GridLayout(2,9));
tf1=new TextField(30);
tf2=new TextField(30);
tf1.setEditable(false);
p1.add(tf1);
tf2.setEditable(false);
p1.add(tf2);
for(int i=0;i<10;i++){
String s1=""+i;
b[i]=new Button(s1);
p2.add(b[i]);
b[i].addActionListener(this);
}
b[10]=new Button(".");
p2.add(b[10]);
b[10].addActionListener(this);
b1=new Button("+");
b2=new Button("-");
b3=new Button("*");
b4=new Button("/");
b5=new Button("=");
b6=new Button("C");
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
f1.addWindowListener(new WinClose());
f1.setVisible(true);
}
public void actionPerformed(ActionEvent e){
s=s+e.getActionCommand();
for(int i=0;i<11;i++){
if(e.getSource()==b[i])
tf1.setText(tf1.getText()+e.getActionCommand());
}
if(e.getSource()==b6){
tf1.setText("");
tf2.setText("");
x=0;
y=0;
f=0;
s="";
}
if(e.getSource()==b1){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
tf2.setText(""+(y+x));
y=y+x;
f=1;
}
if(e.getSource()==b2){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){
tf2.setText(""+x);
y=x;
}
else{
tf2.setText(""+(y-x));
y=y-x;
}
f=2;
}
if(e.getSource()==b3){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){
tf2.setText(""+x);
y=x;
}
else{
tf2.setText(""+(y*x));
y=y*x;
}
f=3;
}
if(e.getSource()==b4){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){
tf2.setText(""+x);y=x;
}
else{
tf2.setText(""+(y/x));
y=y/x;
}
f=4;
}
if(e.getSource()==b5){
x=Double.parseDouble(tf1.getText());
if(f==1) tf1.setText(""+(y+x));
if(f==2) tf1.setText(""+(y-x));
if(f==3) tf1.setText(""+(y*x));
if(f==4) tf1.setText(""+(y/x));
y=0;
tf2.setText(s+tf1.getText());
}
}
class WinClose extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}