求一个简单的java程序

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
mengvlog 阅读 5 次 更新于 2025-07-19 08:28:11 我来答关注问题0
  •  深空见闻 急求一道简单的Java程序题求1到n的自然数之和,从命令行输入n

    该程序首先创建了一个名为Sum的公共类,并在其中定义了一个main方法,这是程序的入口点。main方法通过BufferedReader对象读取用户输入的一个自然数,并使用sum方法计算该自然数及其所有小于它的整数的和。sum方法是一个递归函数,它通过不断调用自身来计算和,直到输入的整数为1时,递归终止,并返回1作为...

  • 编写Java程序的第一步通常是创建一个类,然后在类中定义一个main方法。在这个main方法中,你可以执行各种操作,比如输出文本、读取输入、调用其他方法等。在这个简单的例子中,我们选择了输出"HelloWorld"作为展示程序运行的基本任务。除了输出文本,Java还提供了丰富的库和框架,用于处理各种复杂的数据结构和...

  •  仰晴虹0Ci 求两道简单的java代码:1. 编写一个程序,要求在运行时,输入一个数字n,程序运行后,直接输出n!的结果

    import java.util.Scanner;public class First { public static void main(String[] args){ Scanner in=new Scanner(System.in);System.out.println("请输入一个数字");int n=in.nextInt();System.out.println(n+"的阶乘是:"+factorial(n));} public static int factorial(int m){ if(m==...

  • import java.io.FileReader;import java.io.IOException;public class FileReaderDemo { public static void main(String[] args) throws IOException { // 创建对象 FileReader fr = new FileReader("a.txt");// 读取文件 int num = 0;while((num=fr.read())!=-1){ System.out.print((char)...

  • jumpkang 求一非常简单的java 程序?

    第一题:import java.util.Scanner;public class CountDate { public static void main(String[] args) { Scanner s = new Scanner(System.in);System.out.println("解放军战士连续作战的小时数:");int hours = s.nextInt();System.out.print("解放军战士连续作战");if(hours

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部