import java.awt.event.MouseListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JTextArea;imp...
求一个简单的java程序
package test;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
@SuppressWarnings("serial")
public class MyEditor extends JFrame implements ActionListener,MouseListener {
private Container contentPane;
private JMenuBar menuBar=new JMenuBar();
private JMenu fileMenu=new JMenu("File");
private JMenu editMenu=new JMenu("Edit");
private JMenu helpMenu=new JMenu("Help");
private JMenuItem newMI=new JMenuItem("New");
private JMenuItem openMI=new JMenuItem("Open");
private JMenuItem saveMI=new JMenuItem("Save");
private JMenuItem exitMI=new JMenuItem("Exit");
private JMenuItem copyMI=new JMenuItem("Copy");
private JMenuItem cutMI=new JMenuItem("Cut");
private JMenuItem pasteMI=new JMenuItem("Paste");
private JMenuItem aboutMI=new JMenuItem("About");
private JToolBar toolBar=new JToolBar();
private JButton copyBtn=new JButton("Copy");
private JButton cutBtn=new JButton("Cut");
private JButton pasteBtn=new JButton("Paste");
private JScrollPane scorllPane=new JScrollPane();
private JTextArea editArea=new JTextArea();
private JLabel statusBar=new JLabel("status:");
private JPopupMenu popupMenu=new JPopupMenu();
private JMenuItem copyPMI=new JMenuItem("Copy");
private JMenuItem cutPMI=new JMenuItem("Cut");
private JMenuItem pastePMI=new JMenuItem("Paste");
public MyEditor(String title){
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane=getContentPane();
setSize(800,600);
initGUI();
}
private void initGUI(){
fileMenu.add(newMI);
fileMenu.add(openMI);
fileMenu.add(saveMI);
fileMenu.addSeparator(); //分割符
fileMenu.add(exitMI);
editMenu.add(copyMI);
editMenu.add(cutMI);
editMenu.add(pasteMI);
helpMenu.add(aboutMI);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
popupMenu.add(copyPMI);
popupMenu.add(cutPMI);
popupMenu.add(pastePMI);
setJMenuBar(menuBar);
contentPane.setLayout(new BorderLayout());
toolBar.add(copyBtn);
toolBar.add(cutBtn);
toolBar.add(pasteBtn);
contentPane.add(toolBar,BorderLayout.NORTH);
this.scorllPane.getViewport().add(this.editArea);
contentPane.add(scorllPane,BorderLayout.CENTER);
contentPane.add(this.statusBar,BorderLayout.SOUTH);
exitMI.addActionListener(this);
copyMI.addActionListener(this);
cutMI.addActionListener(this);
pasteMI.addActionListener(this);
saveMI.addActionListener(this);
copyBtn.addActionListener(this);
cutBtn.addActionListener(this);
pasteBtn.addActionListener(this);
editArea.addMouseListener(this);
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
if(source==exitMI){
System.exit(0);
}
if(source==copyMI || source==copyBtn){
editArea.copy();
}
if(source == cutMI || source == cutBtn){
editArea.cut();
}
if(source==pasteMI || source==pasteBtn){
editArea.paste();
}
}
public void go(){
setVisible(true);
}
public static void main(String[] args) {
(new MyEditor("my editor")).go();
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
int x=e.getX();int y=e.getY();
if(popupMenu.isPopupTrigger(e)){
popupMenu.show(editArea, x, y);
}
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
mousePressed(e);
}
}
这可是我的原创啊,你要是能在网上搜到,我就佩服你!
另外,你是学java的吗?你要是会java就不可能运行不了的!!!2008-12-25
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.datatransfer.*;
public class yangweitao extends Frame implements ActionListener,MouseListener{
FileDialog fileDlg;
String str,fileName;
byte byteBuf[]=new byte[10000];
Toolkit toolKit=Toolkit.getDefaultToolkit();
Clipboard clipBoard=toolKit.getSystemClipboard();
TextArea ta=new TextArea();
PopupMenu pm=new PopupMenu();
MenuBar mb=new MenuBar();
Menu m1=new Menu("文件");
Menu m2=new Menu("编辑");
Menu m3=new Menu("工具");
MenuItem cut2=new MenuItem("剪切");
MenuItem copy2=new MenuItem("复制");
MenuItem paste2=new MenuItem("粘贴");
MenuItem cut=new MenuItem("剪切");
MenuItem copy=new MenuItem("复制");
MenuItem paste=new MenuItem("粘贴");
MenuItem computer=new MenuItem("计算器");
MenuItem open=new MenuItem("打开");
MenuItem close=new MenuItem("关闭");
MenuItem save=new MenuItem("保存");
MenuItem exit=new MenuItem("推出");
yangweitao(){
setTitle("简易文件编辑器");
setSize(400,280);
add("Center",ta);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
m1.add(open);
m1.add(close);
m1.add(save);
m1.addSeparator();
m1.add(exit);
m2.add(cut);
m2.add(paste);
m2.add(copy);
m3.add(computer);
open.addActionListener(this);
close.addActionListener(this);
save.addActionListener(this);
exit.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
cut2.addActionListener(this);
copy2.addActionListener(this);
paste2.addActionListener(this);
computer.addActionListener(this);
mb.add(m1);
mb.add(m2);
mb.add(m3);
pm.add(cut2);
pm.add(paste2);
pm.add(copy2);
ta.add(pm);
this.setMenuBar(mb);
ta.addMouseListener(this);
show();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==exit)
System.exit(0);
else if(e.getSource()==close)
ta.setText(null);
else if(e.getSource()==open){
fileDlg=new FileDialog(this,"打开文件");
fileDlg.show();
fileName=fileDlg.getFile();
try{
FileInputStream in=new FileInputStream(fileName);
in.read(byteBuf);
in.close();
str=new String(byteBuf);
ta.setText(str);
setTitle("adsad"+fileName);
}catch(IOException ioe){}
}
else if(e.getSource()==save){
fileDlg=new FileDialog(this,"保存文件",FileDialog.SAVE);
fileDlg.show();
fileName=fileDlg.getFile();
str=ta.getText();
byteBuf=str.getBytes();
try{
FileOutputStream out=new FileOutputStream(fileName);
out.write(byteBuf);
out.close();
}catch(IOException ioe){}
}
else if(e.getActionCommand()=="剪切"){
String text=ta.getSelectedText();
StringSelection selection=new StringSelection(text);
clipBoard.setContents(selection,null);
ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
}
else if(e.getActionCommand()=="复制")
{String text=ta.getSelectedText();
StringSelection selection=new StringSelection(text);
clipBoard.setContents(selection,null);
}
else if(e.getActionCommand()=="粘贴")
{Transferable contents=clipBoard.getContents(this);
if(contents==null) return;
String text;
text="";
try{
text=(String)contents.getTransferData(DataFlavor.stringFlavor);
}catch(Exception exception){
}
ta.replaceRange(text,ta.getSelectionStart(),ta.getSelectionEnd());
}
else if(e.getActionCommand()=="计算器") {
Runtime r= Runtime.getRuntime();
try {
r.exec("calc");
} catch (IOException e1) {}
}
}
public void mouseReleased(MouseEvent e){
if(e.isPopupTrigger())
pm.show(this,e.getX(),e.getX());
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public static void main(String args[]){
new yangweitao();
}
}
不会吧 我的能运行呢 我只有一个能达到你的要求 就是代码有蛮长的 发不了 所以我发了这个比较短的2008-12-22
我的记事本功能比你要求的多。不过太大了,不方便帖上来。
而且不只80分。2008-12-22
太繁了,激活两个字不知道怎么理解啊~全部System.out.println("激活了");行不行啊?2008-12-22
太繁琐,并且网上有现成的东西,自己找一找吧,有时候去网上找资源是一件很快的的事情。2008-12-22