求一个用JAVA写的简单的记事本源代码程序

求一个用JAVA写的简单的记事本源代码程序,第1张

import java.awt.*

import java.awt.event.*

import java.io.*

import java.awt.datatransfer.*

class MyMenuBar extends MenuBar{

public MyMenuBar(Frame parent){

parent.setMenuBar(this)

}

public void addMenus(String [] menus){

for(int i=0i<menus.lengthi++)

add(new Menu(menus[i]))

}

public void addMenuItems(int menuNumber,String[] items){

for(int i=0i<items.lengthi++){

if(items[i]!=null)

getMenu(menuNumber).add(new MenuItem(items[i]))

else getMenu(menuNumber).addSeparator()

}

}

public void addActionListener(ActionListener al){

for(int i=0i<getMenuCount()i++)

for(int j=0j<getMenu(i).getItemCount()j++)

getMenu(i).getItem(j).addActionListener(al)

}

}

class MyFile{

private FileDialog fDlg

public MyFile(Frame parent){

fDlg=new FileDialog(parent,"",FileDialog.LOAD)

}

private String getPath(){

return fDlg.getDirectory()+"\\"+fDlg.getFile()

}

public String getData() throws IOException{

fDlg.setTitle("打开")

fDlg.setMode(FileDialog.LOAD)

fDlg.setVisible(true)

BufferedReader br=new BufferedReader(new FileReader(getPath()))

StringBuffer sb=new StringBuffer()

String aline

while((aline=br.readLine())!=null)

sb.append(aline+'\n')

br.close()

return sb.toString()

}

public void setData(String data) throws IOException{

fDlg.setTitle("保存")

fDlg.setMode(FileDialog.SAVE)

fDlg.setVisible(true)

BufferedWriter bw=new BufferedWriter(new FileWriter(getPath()))

bw.write(data)

bw.close()

}

}

class MyClipboard{

private Clipboard cb

public MyClipboard(){

cb=Toolkit.getDefaultToolkit().getSystemClipboard()

}

public void setData(String data){

cb.setContents(new StringSelection(data),null)

}

public String getData(){

Transferable content=cb.getContents(null)

try{

return (String) content.getTransferData(DataFlavor.stringFlavor)

//DataFlavor.stringFlavor会将剪贴板中的字符串转换成Unicode码形式的String对象。

//DataFlavor类是与存储在剪贴板上的数据的形式有关的类。

}catch(Exception ue){}

return null

}

}

class MyFindDialog extends Dialog implements ActionListener{

private Label lFind=new Label("查找字符串")

private Label lReplace=new Label("替换字符串")

private TextField tFind=new TextField(10)

private TextField tReplace=new TextField(10)

private Button bFind=new Button("查找")

private Button bReplace=new Button("替换")

private TextArea ta

public MyFindDialog(Frame owner,TextArea ta){

super(owner,"查找",false)

this.ta=ta

setLayout(null)

lFind.setBounds(10,30,80,20)

lReplace.setBounds(10,70,80,20)

tFind.setBounds(90,30,90,20)

tReplace.setBounds(90,70,90,20)

bFind.setBounds(190,30,80,20)

bReplace.setBounds(190,70,80,20)

add(lFind)

add(tFind)

add(bFind)

add(lReplace)

add(tReplace)

add(bReplace)

setResizable(false)

bFind.addActionListener(this)

bReplace.addActionListener(this)

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

MyFindDialog.this.dispose()

}

})

}//构造函数结束

public void showFind(){

setTitle("查找")

setSize(280,60)

setVisible(true)

}

public void showReplace(){

setTitle("查找替换")

setSize(280,110)

setVisible(true)

}

private void find(){

String text=ta.getText()

String str=tFind.getText()

int end=text.length()

int len=str.length()

int start=ta.getSelectionEnd()

if(start==end) start=0

for(start<=end-lenstart++){

if(text.substring(start,start+len).equals(str)){

ta.setSelectionStart(start)

ta.setSelectionEnd(start+len)

return

}

}

//若找不到待查字符串,则将光标置于末尾

ta.setSelectionStart(end)

ta.setSelectionEnd(end)

}

public Button getBFind() {

return bFind

}

private void replace(){

String str=tReplace.getText()

if(ta.getSelectedText().equals(tFind.getText()))

ta.replaceRange(str,ta.getSelectionStart(),ta.getSelectionEnd())

else find()

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==bFind)

find()

else if(e.getSource()==bReplace)

replace()

}

}

public class MyMemo extends Frame implements ActionListener{

private TextArea editor=new TextArea()//可编辑的TextArea

private MyFile mf=new MyFile(this)//MyFile对象

private MyClipboard cb=new MyClipboard()

private MyFindDialog findDlg=new MyFindDialog(this,editor)

public MyMemo(String title){ //构造函数

super(title)

MyMenuBar mb=new MyMenuBar(this)

//添加需要的菜单及菜单项

mb.addMenus(new String[]{"文件","编辑","查找","帮助"})

mb.addMenuItems(0,new String[]{"新建","打开","保存",null,"全选"})

mb.addMenuItems(1,new String[]{"剪贴","复制","粘贴","清除",null,"全选"})

mb.addMenuItems(2,new String[]{"查找",null,"查找替换"})

mb.addMenuItems(3,new String[]{"我的记事本信息"})

add(editor) //为菜单项注册动作时间监听器

mb.addActionListener(this)

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

MyMemo.this.dispose()

}

}) //分号不能忘了

} //构造函数完

public void actionPerformed(ActionEvent e){

String selected=e.getActionCommand()//获取菜单项标题

if(selected.equals("新建"))

editor.setText("")

else if(selected.equals("打开")){

try{

editor.setText(mf.getData())

}catch(IOException ie){}

}

else if(selected.equals("保存")){

try{

mf.setData(editor.getText())

}catch(IOException ie){}

}

else if(selected.equals("退出")){

dispose()

}

else if(selected.equals("剪贴")){

//将选中的字符串复制到剪贴板中并清除字符串

cb.setData(editor.getSelectedText())

editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd())

}

else if(selected.equals("复制")){

cb.setData(editor.getSelectedText())

}

else if(selected.equals("粘贴")){

String str=cb.getData()

editor.replaceRange(str,editor.getSelectionStart(),editor.getSelectionEnd())

//粘贴在光标位置

}

else if(selected.equals("清除")){

editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd())

}

else if(selected.equals("全选")){

editor.setSelectionStart(0)

editor.setSelectionEnd(editor.getText().length())

}

else if(selected.equals("查找")){

findDlg.showFind()

}

else if(selected.equals("查找替换")){

findDlg.showReplace()

}

}

public static void main(String[] args){

MyMemo memo=new MyMemo("记事本")

memo.setSize(650,450)

memo.setVisible(true)

}

}

您好,写了一个程序,求素数,并将所有素数存到ArrayList sushu中:

import java.util.ArrayList

import java.util.zip.Inflater

public class sushu {

public static void main(String[] args) {

int n=50,b=0

float a=0,c=0

ArrayList sushu=new ArrayList()

for(int i=3i<=ni++){

int state=0

for(int j=2j<(i/2+1)j++){

a=(float)i/(float)j

//System.out.println(a)

b=(int)a

//System.out.println(a-b)

c=a-b

//System.out.println(c)

if(c==0){state=1break}

}

if(state==0)sushu.add(i)

}

System.out.println(sushu)

}

}

输出结果为:[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

第一题

import java.util.Random

import java.util.Scanner

public class Guess{

public static void main(String[] args) {

int rightNum = new Random().nextInt(100) + 1

Scanner scanner = new Scanner(System.in)

int input = 0

do{

System.out.print("清猜数字(1-100)!")

input = scanner.nextInt()

if(input >rightNum){

System.out.println("猜大了!")

}

else if(input <rightNum){

System.out.println("猜小了!")

}

}while(input != rightNum)

System.out.println("猜对了" + rightNum)

}

}

第二题

import java.util.*

public class A{

public static void main(String args[]){

int i,j,k,temp

int a[][]=new int[2][3]

a[0][0]=(int)(100*Math.random())

a[0][1]=(int)(100*Math.random())

a[0][2]=(int)(100*Math.random())

a[1][0]=(int)(100*Math.random())

a[1][1]=(int)(100*Math.random())

a[1][2]=(int)(100*Math.random())

for(j=0j<3j++)

System.out.println("a[0]["+j+"]="+a[0][j])

System.out.println(" ")

for(j=0j<3j++)

System.out.println("a[1]["+j+"]="+a[1][j])

System.out.println(" ")

for(i=0i<2i++){

for(j=0j<2j++){

for(k=jk<2k++){

if(a[i][j]>a[i][k+1]){

temp=a[i][j]

a[i][j]=a[i][k+1]

a[i][k+1]=temp

}

}

}

}

System.out.println("第一行按从小到大排列:")

for(j=0j<3j++){

System.out.println("a[0]["+j+"]="+a[0][j])

}

System.out.println("第二行按从小到大排列:")

for(j=0j<3j++)

System.out.println("a[1]["+j+"]=" +a[1][j])

}

}

春春??还不快采纳嘛


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12196825.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存