社区
Java SE
帖子详情
请问TextArea的这个方法setCaretposition(int)的功能。谢谢了~
Fxlsl51
2004-05-08 03:53:38
请问TextArea的这个方法setCaretposition(int)的功能。
随便请教输入输出流的SercerSocket/Socket的具体用途,,越详细越好。谢了~
...全文
193
1
打赏
收藏
请问TextArea的这个方法setCaretposition(int)的功能。谢谢了~
请问TextArea的这个方法setCaretposition(int)的功能。 随便请教输入输出流的SercerSocket/Socket的具体用途,,越详细越好。谢了~
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
1 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
rainboy20024
2004-05-08
打赏
举报
回复
Set the position of the text insertion caret for the TextComponent
java 小游戏(5子棋的实现)
import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; class clientThread extends Thread { chessClient chessclient; clientThread(chessClient chessclient) { this.chessclient=chessclient; } public void acceptMessage(String recMessage) { if(recMessage.startsWith("/userlist ")) { StringTokenizer userToken=new StringTokenizer(recMessage," ");
int
userNumber=0; chessclient.userpad.userList.removeAll(); chessclient.inputpad.userChoice.removeAll(); chessclient.inputpad.userChoice.addItem("所有人"); while(userToken.hasMoreTokens()) { String user=(String)userToken.nextToken(" "); if(userNumber>0 && !user.startsWith("[inchess]")) { chessclient.userpad.userList.add(user); chessclient.inputpad.userChoice.addItem(user); } userNumber++; } chessclient.inputpad.userChoice.select("所有人"); } else if(recMessage.startsWith("/yourname ")) { chessclient.chessClientName=recMessage.substring(10); chessclient.
set
Title("Java五子棋客户端 "+"用户名:"+chessclient.chessClientName); } else if(recMessage.equals("/reject")) { try { chessclient.chesspad.statusText.
set
Text("不能加入游戏"); chessclient.controlpad.cancelGameButton.
set
Enabled(false); chessclient.controlpad.joinGameButton.
set
Enabled(true); chessclient.controlpad.creatGameButton.
set
Enabled(true); } catch(Exception ef) { chessclient.chatpad.chatLineArea.
set
Text("chessclient.chesspad.chessSocket.close无法关闭"); } chessclient.controlpad.joinGameButton.
set
Enabled(true); } else if(recMessage.startsWith("/peer ")) { chessclient.chesspad.chessPeerName=recMessage.substring(6); if(chessclient.isServer) { chessclient.chesspad.chessColor=1; chessclient.chesspad.isMouseEnabled=true; chessclient.chesspad.statusText.
set
Text("请黑棋下子"); } else if(chessclient.isClient) { chessclient.chesspad.chessColor=-1; chessclient.chesspad.statusText.
set
Text("已加入游戏,等待对方下子..."); } } else if(recMessage.equals("/youwin")) { chessclient.isOnChess=false; chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor); chessclient.chesspad.statusText.
set
Text("对方退出,请点放弃游戏退出连接"); chessclient.chesspad.isMouseEnabled=false; } else if(recMessage.equals("/OK")) { chessclient.chesspad.statusText.
set
Text("创建游戏成功,等待别人加入..."); } else if(recMessage.equals("/error")) { chessclient.chatpad.chatLineArea.append("传输错误:请退出程序,重新加入 \n"); } else { chessclient.chatpad.chatLineArea.append(recMessage+"\n"); chessclient.chatpad.chatLineArea.
set
Caret
Position
( chessclient.chatpad.chatLineArea.getText().length()); } } public void run() { String message=""; try { while(true) { message=chessclient.in.readUTF(); acceptMessage(message); } } catch(IOException es) { } } } public class chessClient extends Frame implements ActionListener,KeyListener { userPad userpad=new userPad(); chatPad chatpad=new chatPad(); controlPad controlpad=new controlPad(); chessPad chesspad=new chessPad(); inputPad inputpad=new inputPad(); Socket chatSocket; DataInputStream in; DataOutputStream out; String chessClientName=null; String host=null;
int
port=4331; boolean isOnChat=false; //在聊天? boolean isOnChess=false; //在下棋? boolean isGameConnected=false; //下棋的客户端连接? boolean isServer=false; //如果是下棋的主机 boolean isClient=false; //如果是下棋的客户端 Panel southPanel=new Panel(); Panel northPanel=new Panel(); Panel centerPanel=new Panel(); Panel westPanel=new Panel(); Panel eastPanel=new Panel(); chessClient() { super("Java五子棋客户端");
set
Layout(new BorderLayout()); host=controlpad.inputIP.getText(); westPanel.
set
Layout(new BorderLayout()); westPanel.add(userpad,BorderLayout.NORTH); westPanel.add(chatpad,BorderLayout.CENTER); westPanel.
set
Background(Color.pink); inputpad.inputWords.addKeyListener(this); chesspad.host=controlpad.inputIP.getText(); centerPanel.add(chesspad,BorderLayout.CENTER); centerPanel.add(inputpad,BorderLayout.SOUTH); centerPanel.
set
Background(Color.pink); controlpad.connectButton.addActionListener(this); controlpad.creatGameButton.addActionListener(this); controlpad.joinGameButton.addActionListener(this); controlpad.cancelGameButton.addActionListener(this); controlpad.exitGameButton.addActionListener(this); controlpad.creatGameButton.
set
Enabled(false); controlpad.joinGameButton.
set
Enabled(false); controlpad.cancelGameButton.
set
Enabled(false); southPanel.add(controlpad,BorderLayout.CENTER); southPanel.
set
Background(Color.pink); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if(isOnChat) { try { chatSocket.close(); } catch(Exception ed) { } } if(isOnChess || isGameConnected) { try { chesspad.chessSocket.close(); } catch(Exception ee) { } } System.exit(0); } public void windowActivated(WindowEvent ea) { } }); add(westPanel,BorderLayout.WEST); add(centerPanel,BorderLayout.CENTER); add(southPanel,BorderLayout.SOUTH); pack();
set
Size(670,548);
set
Visible(true);
set
Resizable(false); validate(); } public boolean connectServer(String serverIP,
int
serverPort) throws Exception { try { chatSocket=new Socket(serverIP,serverPort); in=new DataInputStream(chatSocket.getInputStream()); out=new DataOutputStream(chatSocket.getOutputStream()); clientThread clientthread=new clientThread(this); clientthread.start(); isOnChat=true; return true; } catch(IOException ex) { chatpad.chatLineArea.
set
Text("chessClient:connectServer:无法连接,建议重新启动程序 \n"); } return false; } public void actionPerformed(ActionEvent e) { if(e.getSource()==controlpad.connectButton) { host=chesspad.host=controlpad.inputIP.getText(); try { if(connectServer(host,port)) { chatpad.chatLineArea.
set
Text(""); controlpad.connectButton.
set
Enabled(false); controlpad.creatGameButton.
set
Enabled(true); controlpad.joinGameButton.
set
Enabled(true); chesspad.statusText.
set
Text("连接成功,请创建游戏或加入游戏"); } } catch(Exception ei) { chatpad.chatLineArea.
set
Text("controlpad.connectButton:无法连接,建议重新启动程序 \n"); } } if(e.getSource()==controlpad.exitGameButton) { if(isOnChat) { try { chatSocket.close(); } catch(Exception ed) { } } if(isOnChess || isGameConnected) { try { chesspad.chessSocket.close(); } catch(Exception ee) { } } System.exit(0); } if(e.getSource()==controlpad.joinGameButton) { String selectedUser=userpad.userList.getSelectedItem(); if(selectedUser==null || selectedUser.startsWith("[inchess]") || selectedUser.equals(chessClientName)) { chesspad.statusText.
set
Text("必须先选定一个有效用户"); } else { try { if(!isGameConnected) { if(chesspad.connectServer(chesspad.host,chesspad.port)) { isGameConnected=true; isOnChess=true; isClient=true; controlpad.creatGameButton.
set
Enabled(false); controlpad.joinGameButton.
set
Enabled(false); controlpad.cancelGameButton.
set
Enabled(true); chesspad.chessthread.sendMessage("/joingame "+userpad.userList.getSelectedItem()+" "+chessClientName); } } else { isOnChess=true; isClient=true; controlpad.creatGameButton.
set
Enabled(false); controlpad.joinGameButton.
set
Enabled(false); controlpad.cancelGameButton.
set
Enabled(true); chesspad.chessthread.sendMessage("/joingame "+userpad.userList.getSelectedItem()+" "+chessClientName); } } catch(Exception ee) { isGameConnected=false; isOnChess=false; isClient=false; controlpad.creatGameButton.
set
Enabled(true); controlpad.joinGameButton.
set
Enabled(true); controlpad.cancelGameButton.
set
Enabled(false); chatpad.chatLineArea.
set
Text("chesspad.connectServer无法连接 \n"+ee); } } } if(e.getSource()==controlpad.creatGameButton) { try { if(!isGameConnected) { if(chesspad.connectServer(chesspad.host,chesspad.port)) { isGameConnected=true; isOnChess=true; isServer=true; controlpad.creatGameButton.
set
Enabled(false); controlpad.joinGameButton.
set
Enabled(false); controlpad.cancelGameButton.
set
Enabled(true); chesspad.chessthread.sendMessage("/creatgame "+"[inchess]"+chessClientName); } } else { isOnChess=true; isServer=true; controlpad.creatGameButton.
set
Enabled(false); controlpad.joinGameButton.
set
Enabled(false); controlpad.cancelGameButton.
set
Enabled(true); chesspad.chessthread.sendMessage("/creatgame "+"[inchess]"+chessClientName); } } catch(Exception ec) { isGameConnected=false; isOnChess=false; isServer=false; controlpad.creatGameButton.
set
Enabled(true); controlpad.joinGameButton.
set
Enabled(true); controlpad.cancelGameButton.
set
Enabled(false); ec.pr
int
StackTrace(); chatpad.chatLineArea.
set
Text("chesspad.connectServer无法连接 \n"+ec); } } if(e.getSource()==controlpad.cancelGameButton) { if(isOnChess) { chesspad.chessthread.sendMessage("/giveup "+chessClientName); chesspad.chessVictory(-1*chesspad.chessColor); controlpad.creatGameButton.
set
Enabled(true); controlpad.joinGameButton.
set
Enabled(true); controlpad.cancelGameButton.
set
Enabled(false); chesspad.statusText.
set
Text("请建立游戏或者加入游戏"); } if(!isOnChess) { controlpad.creatGameButton.
set
Enabled(true); controlpad.joinGameButton.
set
Enabled(true); controlpad.cancelGameButton.
set
Enabled(false); chesspad.statusText.
set
Text("请建立游戏或者加入游戏"); } isClient=isServer=false; } } public void keyPressed(KeyEvent e) { TextField inputWords=(TextField)e.getSource(); if(e.getKeyCode()==KeyEvent.VK_ENTER) { if(inputpad.userChoice.getSelectedItem().equals("所有人")) { try { out.writeUTF(inputWords.getText()); inputWords.
set
Text(""); } catch(Exception ea) { chatpad.chatLineArea.
set
Text("chessClient:KeyPressed无法连接,建议重新连接 \n"); userpad.userList.removeAll(); inputpad.userChoice.removeAll(); inputWords.
set
Text(""); controlpad.connectButton.
set
Enabled(true); } } else { try { out.writeUTF("/"+inputpad.userChoice.getSelectedItem()+" "+inputWords.getText()); inputWords.
set
Text(""); } catch(Exception ea) { chatpad.chatLineArea.
set
Text("chessClient:KeyPressed无法连接,建议重新连接 \n"); userpad.userList.removeAll(); inputpad.userChoice.removeAll(); inputWords.
set
Text(""); controlpad.connectButton.
set
Enabled(true); } } } } public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public static void main(String args[]) { chessClient chessClient=new chessClient(); } } /****************************************************************************************** 下面是:chess
Int
eface.java ******************************************************************************************/ import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; class userPad extends Panel { List userList=new List(10); userPad() {
set
Layout(new BorderLayout()); for(
int
i=0;i<50;i++) { userList.add(i+"."+"没有用户"); } add(userList,BorderLayout.CENTER); } } class chatPad extends Panel {
TextArea
chatLineArea=new
TextArea
("",18,30,
TextArea
.SCROLLBARS_VERTICAL_ONLY); chatPad() {
set
Layout(new BorderLayout()); add(chatLineArea,BorderLayout.CENTER); } } class controlPad extends Panel { Label IPlabel=new Label("IP",Label.LEFT); TextField inputIP=new TextField("localhost",10); Button connectButton=new Button("连接主机"); Button creatGameButton=new Button("建立游戏"); Button joinGameButton=new Button("加入游戏"); Button cancelGameButton=new Button("放弃游戏"); Button exitGameButton=new Button("关闭程序"); controlPad() {
set
Layout(new FlowLayout(FlowLayout.LEFT));
set
Background(Color.pink); add(IPlabel); add(inputIP); add(connectButton); add(creatGameButton); add(joinGameButton); add(cancelGameButton); add(exitGameButton); } } class inputPad extends Panel { TextField inputWords=new TextField("",40); Choice userChoice=new Choice(); inputPad() {
set
Layout(new FlowLayout(FlowLayout.LEFT)); for(
int
i=0;i<50;i++) { userChoice.addItem(i+"."+"没有用户"); } userChoice.
set
Size(60,24); add(userChoice); add(inputWords); } } /********************************************************************************************** 下面是:chessPad.java **********************************************************************************************/ import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; class chessThread extends Thread { chessPad chesspad; chessThread(chessPad chesspad) { this.chesspad=chesspad; } public void sendMessage(String sndMessage) { try { chesspad.outData.writeUTF(sndMessage); } catch(Exception ea) { System.out.pr
int
ln("chessThread.sendMessage:"+ea); } } public void acceptMessage(String recMessage) { if(recMessage.startsWith("/chess ")) { StringTokenizer userToken=new StringTokenizer(recMessage," "); String chessToken; String[] chessOpt={"-1","-1","0"};
int
chessOptNum=0; while(userToken.hasMoreTokens()) { chessToken=(String)userToken.nextToken(" "); if(chessOptNum>=1 && chessOptNum<=3) { chessOpt[chessOptNum-1]=chessToken; } chessOptNum++; } chesspad.netChessPa
int
(
Int
eger.parse
Int
(chessOpt[0]),
Int
eger.parse
Int
(chessOpt[1]),
Int
eger.parse
Int
(chessOpt[2])); } else if(recMessage.startsWith("/yourname ")) { chesspad.chessSelfName=recMessage.substring(10); } else if(recMessage.equals("/error")) { chesspad.statusText.
set
Text("错误:没有这个用户,请退出程序,重新加入"); } else { //System.out.pr
int
ln(recMessage); } } public void run() { String message=""; try { while(true) { message=chesspad.inData.readUTF(); acceptMessage(message); } } catch(IOException es) { } } } class chessPad extends Panel implements MouseListener,ActionListener {
int
chessPo
int
_x=-1,chessPo
int
_y=-1,chessColor=1;
int
chessBlack_x[]=new
int
[200];
int
chessBlack_y[]=new
int
[200];
int
chessWhite_x[]=new
int
[200];
int
chessWhite_y[]=new
int
[200];
int
chessBlackCount=0,chessWhiteCount=0;
int
chessBlackWin=0,chessWhiteWin=0; boolean isMouseEnabled=false,isWin=false,isInGame=false; TextField statusText=new TextField("请先连接服务器"); Socket chessSocket; DataInputStream inData; DataOutputStream outData; String chessSelfName=null; String chessPeerName=null; String host=null;
int
port=4331; chessThread chessthread=new chessThread(this); chessPad() {
set
Size(440,440);
set
Layout(null);
set
Background(Color.pink); addMouseListener(this); add(statusText); statusText.
set
Bounds(40,5,360,24); statusText.
set
Editable(false); } public boolean connectServer(String ServerIP,
int
ServerPort) throws Exception { try { chessSocket=new Socket(ServerIP,ServerPort); inData=new DataInputStream(chessSocket.getInputStream()); outData=new DataOutputStream(chessSocket.getOutputStream()); chessthread.start(); return true; } catch(IOException ex) { statusText.
set
Text("chessPad:connectServer:无法连接 \n"); } return false; } public void chessVictory(
int
chessColorWin) { this.removeAll(); for(
int
i=0;i<=chessBlackCount;i++) { chessBlack_x[i]=0; chessBlack_y[i]=0; } for(
int
i=0;i<=chessWhiteCount;i++) { chessWhite_x[i]=0; chessWhite_y[i]=0; } chessBlackCount=0; chessWhiteCount=0; add(statusText); statusText.
set
Bounds(40,5,360,24); if(chessColorWin==1) { chessBlackWin++; statusText.
set
Text("黑棋胜,黑:白为"+chessBlackWin+":"+chessWhiteWin+",重新开局,等待白棋下子..."); } else if(chessColorWin==-1) { chessWhiteWin++; statusText.
set
Text("白棋胜,黑:白为"+chessBlackWin+":"+chessWhiteWin+",重新开局,等待黑棋下子..."); } } public void getLocation(
int
a,
int
b,
int
color) { if(color==1) { chessBlack_x[chessBlackCount]=a*20; chessBlack_y[chessBlackCount]=b*20; chessBlackCount++; } else if(color==-1) { chessWhite_x[chessWhiteCount]=a*20; chessWhite_y[chessWhiteCount]=b*20; chessWhiteCount++; } } public boolean checkWin(
int
a,
int
b,
int
checkColor) {
int
step=1,chessLink=1,chessLinkTest=1,chessCompare=0; if(checkColor==1) { chessLink=1; for(step=1;step<=4;step++) { for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++) { if(((a+step)*20==chessBlack_x[chessCompare]) && ((b*20)==chessBlack_y[chessCompare])) { chessLink=chessLink+1; if(chessLink==5) { return(true); } } } if(chessLink==(chessLinkTest+1)) chessLinkTest++; else break; } for(step=1;step<=4;step++) { for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++) { if(((a-step)*20==chessBlack_x[chessCompare]) && (b*20==chessBlack_y[chessCompare])) { chessLink++; if(chessLink==5) { return(true); } } } if(chessLink==(chessLinkTest+1)) chessLinkTest++; else break; } chessLink=1; chessLinkTest=1; for(step=1;step<=4;step++) { for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++) { if((a*20==chessBlack_x[chessCompare]) && ((b+step)*20==chessBlack_y[chessCompare])) { chessLink++; if(chessLink==5) { return(true); } } } if(chessLink==(chessLinkTest+1)) chessLinkTest++; else break;
小型简单的记事本程序
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; public class NotePad { public static void main(String[] args){ myfr fr=new myfr("猪猪java记事本"); fr.
set
Location(100,100); fr.
set
Size(750,650); } } ///////////////////////////myfr主窗体类////////////////////////////////////// class myfr extends JFrame implements ActionListener{ String str,strnext,path,fname; //部分变量的声明 JPanel mainpane; JFileChooser choose = new JFileChooser(); //文件对话框 Dialog find,replace; //find为查找对话框,replace对话框 JTextField findtxt,repltxt; //find为查找对话框的输入文本区,replace对话框的输入文本区 Font newfont; JButton findenter,replb; //find为查找对话框,replace对话框的确定按钮 JLabel state=new JLabel(" 猪猪java记事本------"); //状态栏 J
TextArea
txt1; //主输入文本区 File newfiles; JPopupMenu popm; //弹出菜单声明 JMenu m1,m2,m3,m4,m5,m6; //各菜单项 JMenuItem m61,m62,m26,m271,m34,m51,m52,m53,m54,p_copy,p_cut,p_paste,p_del;
int
startp,endp,nexttemp,newstartp,newendp; //查找替换时所用的临时变量 JToolBar toolbar = new JToolBar(); //工具条 JButton newf=new JButton(new ImageIcon("9.jpg")); //图标在PIC下 JButton open=new JButton(new ImageIcon("2.jpg")); JButton save=new JButton(new ImageIcon("3.jpg")); JButton copy=new JButton(new ImageIcon("6.jpg")); JButton cut=new JButton(new ImageIcon("7.jpg")); JButton pp=new JButton(new ImageIcon("8.jpg")); JButton del=new JButton(new ImageIcon("pic/del.gif")); JButton findc=new JButton(new ImageIcon("9.jpg")); JButton color=new JButton(new ImageIcon("10.jpg")); JButton help=new JButton(new ImageIcon("pic/help.gif")); JButton exit=new JButton(new ImageIcon("pic/exit.gif")); myfr(String sss){ /////构造函数开始 super(sss); JMenuBar mb=new JMenuBar(); fname=null; //初始文件名为空 findenter=new JButton("确定"); findenter.addActionListener(this); //声明对话框中上确定按钮,并注册事件 replb=new JButton("确定"); replb.addActionListener(this); mainpane=(JPanel)this.getContentPane(); mainpane.
set
Layout(new BorderLayout()); txt1=new J
TextArea
("",13,61); txt1.addMouseListener(new handlemouse(this));//注册鼠标右击事件 txt1.
set
Font(new Font("宋体",Font.PLAIN,18)); mainpane.add(txt1, BorderLayout.CENTER); mainpane.add("North",toolbar); mainpane.add("South",state); JScrollPane sll = new JScrollPane(); //创建滚动条 mainpane.add("Center", sll); ; sll.getViewport().add(txt1); //将滚动条装入文本区 //文本栏中的浮动条 popm=new JPopupMenu(); ////POPMeun 开始 p_copy=new JMenuItem("复制 "); p_copy.addActionListener(this); KeyStroke keycopyp=KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK); p_copy.
set
Accelerator(keycopyp); p_cut=new JMenuItem("剪切 "); p_cut.addActionListener(this); KeyStroke keycutp=KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK); p_cut.
set
Accelerator(keycutp); p_paste=new JMenuItem("粘贴 "); p_paste.addActionListener(this); KeyStroke keypp=KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK); p_paste.
set
Accelerator(keypp); p_del=new JMenuItem("删除 "); p_del.addActionListener(this); KeyStroke keydelp=KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0); p_del.
set
Accelerator(keydelp); popm.add(p_copy); popm.add(p_cut); popm.add(p_paste); popm.add(p_del); txt1.add(popm); ////POPMenu 结束 m1=new JMenu("文件(F)"); m1.
set
Mnemonic('F'); //定义快捷方式 JMenuItem m10=new JMenuItem("新建 "); m10.addActionListener(this); //注册事件监听器 KeyStroke keynew=KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK); //定义快捷键 m10.
set
Accelerator(keynew); JMenuItem m11=new JMenuItem("打开 "); m11.addActionListener(this); KeyStroke keyopen=KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK); m11.
set
Accelerator(keyopen); JMenuItem m12=new JMenuItem("保存 "); m12.addActionListener(this); KeyStroke keysave=KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK); m12.
set
Accelerator(keysave); JMenuItem m13=new JMenuItem("另保存为 "); m13.addActionListener(this); JMenuItem m14=new JMenuItem("退出 "); m14.addActionListener(this); KeyStroke keyexit=KeyStroke.getKeyStroke(KeyEvent.VK_F4,Event.ALT_MASK); m14.
set
Accelerator(keyexit); //////////////////////////////////////////// m2=new JMenu("编辑(E)"); m2.
set
Mnemonic('E'); JMenuItem m21=new JMenuItem("复制 "); m21.addActionListener(this); KeyStroke keycopy=KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK); m21.
set
Accelerator(keycopy); JMenuItem m22=new JMenuItem("剪切 "); m22.addActionListener(this); KeyStroke keycut=KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK); m22.
set
Accelerator(keycut); JMenuItem m23=new JMenuItem("粘贴 "); m23.addActionListener(this); KeyStroke keyp=KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK); m23.
set
Accelerator(keyp); JMenuItem m24=new JMenuItem("删除 "); m24.addActionListener(this); KeyStroke keydel=KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0); m24.
set
Accelerator(keydel); JMenuItem m25=new JMenuItem("查找 "); m25.addActionListener(this); KeyStroke keyfind=KeyStroke.getKeyStroke(KeyEvent.VK_F,Event.CTRL_MASK); m25.
set
Accelerator(keyfind); m26=new JMenuItem("查找下一个 "); m26.addActionListener(this); KeyStroke keyfn=KeyStroke.getKeyStroke(KeyEvent.VK_F3,0); m26.
set
Accelerator(keyfn); m26.
set
Enabled(false); JMenuItem m27=new JMenuItem("替换 "); m27.addActionListener(this); KeyStroke keyrepl=KeyStroke.getKeyStroke(KeyEvent.VK_H,Event.CTRL_MASK); m27.
set
Accelerator(keyrepl); m271=new JMenuItem("替换下一个"); m271.
set
Enabled(false); m271.addActionListener(this); KeyStroke keyrepn=KeyStroke.getKeyStroke(KeyEvent.VK_F6,0); m271.
set
Accelerator(keyrepn); JMenuItem m28=new JMenuItem("全选 "); m28.addActionListener(this); KeyStroke keyall=KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.CTRL_MASK); m28.
set
Accelerator(keyall); JMenuItem m29=new JMenuItem("日期/时间 "); m29.addActionListener(this); m3=new JMenu("格式(O)"); m3.
set
Mnemonic('O'); JMenu m31=new JMenu("字体样式 "); JMenuItem m311=new JMenuItem("正常 "); m311.addActionListener(this); JMenuItem m312=new JMenuItem("粗体 "); m312.addActionListener(this); JMenuItem m313=new JMenuItem("斜体 "); m313.addActionListener(this); JMenu m32=new JMenu("字体大小 "); JMenuItem m321=new JMenuItem("最大 "); m321.addActionListener(this); JMenuItem m322=new JMenuItem("较大 "); m322.addActionListener(this); JMenuItem m323=new JMenuItem("适中 "); m323.addActionListener(this); JMenuItem m324=new JMenuItem("较小 "); m324.addActionListener(this); JMenuItem m325=new JMenuItem("最小 "); m325.addActionListener(this); JMenuItem m33=new JMenuItem("字体颜色 "); m33.addActionListener(this); m34=new JMenuItem("自动换行 "); m34.addActionListener(this); m5=new JMenu("视图风格(V)"); m5.
set
Mnemonic('V'); //m51=new JMenuItem("系统风格 "); //m51.addActionListener(this); m52=new JMenuItem("MOTIF风格 "); m52.addActionListener(this); m53=new JMenuItem("默认风格 "); m53.addActionListener(this); m54=new JMenuItem("状态栏 "); m54.addActionListener(this); m6=new JMenu("帮助(H)"); m6.
set
Mnemonic('H'); m61=new JMenuItem("帮助主题 "); m61.addActionListener(this); m62=new JMenuItem("关于 "); m62.addActionListener(this); //添加各项 m1.add(m10); m1.add(m11); m1.add(m12); m1.add(m13); m1.addSeparator(); m1.add(m14); m2.add(m21); m2.add(m22); m2.add(m23); m2.add(m24); m2.addSeparator(); m2.add(m25); m2.add(m26); m2.add(m27); m2.add(m271); m2.addSeparator(); m2.add(m28); m2.add(m29); m3.add(m31); m31.add(m311); m31.add(m312); m31.add(m313); m3.add(m32); m32.add(m321); m32.add(m322); m32.add(m323); m32.add(m324); m32.add(m325); m3.add(m33); m3.addSeparator(); m3.add(m34); //m5.add(m51); m5.add(m52); m5.add(m53); m5.addSeparator(); m5.add(m54); m6.add(m61); m6.addSeparator(); m6.add(m62); mb.add(m1); mb.add(m2); mb.add(m3); mb.add(m5); mb.add(m6); this.
set
JMenuBar(mb); //设置菜单栏 toolbar.add(newf); //工具栏各按钮 newf.
set
ToolTipText("新建"); newf.addActionListener(this); //toolbar.add(open); //open.
set
ToolTipText("打开"); //open.addActionListener(this); //toolbar.add(save); //save.
set
ToolTipText("保存"); //save.addActionListener(this); //toolbar.addSeparator(); toolbar.add(copy); copy.
set
ToolTipText("复制"); copy.addActionListener(this); toolbar.add(cut); cut.
set
ToolTipText("剪切"); cut.addActionListener(this); toolbar.add(pp); pp.
set
ToolTipText("粘贴"); pp.addActionListener(this); //toolbar.add(del); //del.
set
ToolTipText("删除"); //del.addActionListener(this); toolbar.addSeparator(); //toolbar.add(findc); //findc.
set
ToolTipText("查找"); //findc.addActionListener(this); toolbar.add(color); color.
set
ToolTipText("字体颜色"); color.addActionListener(this); toolbar.addSeparator(); //toolbar.add(help); //help.
set
ToolTipText("帮助主题"); //help.addActionListener(this); //toolbar.add(exit); //exit.
set
ToolTipText("退出"); //exit.addActionListener(this);
set
Visible(true); pack(); show(); this.addWindowListener(new xxx(this)); }///构造函数结束/// ////////////////事件处理///////////////////////////////////// public void actionPerformed(ActionEvent p){ if(p.getActionCommand()=="新建 "||p.getSource()==newf){ //响应菜单及工具栏事件 fname=null; //置文件名为空,便于判断文件是否保存过 txt1.
set
Text(""); state.
set
Text(" 猪猪java记事本------"); } if(p.getActionCommand()=="打开 "||p.getSource()==open){ try { if(this.choose.APPROVE_OPTION==this.choose.showOpenDialog(this)){ path=this.choose.getSelectedFile().getPath(); fname=this.choose.getSelectedFile().getName(); File file=new File(path);
int
flength=(
int
)file.length(); FileReader fReader=new FileReader(file); char[] data=new char[flength]; fReader.read(data,0,flength); txt1.
set
Text(new String(data)); state.
set
Text(" 猪猪java记事本------"+path+" 共"+flength+"字节");//状态栏统计文件字节数 txt1.
set
Caret
Position
(0); } }catch(IOException e){} } if(p.getActionCommand()=="保存 "||p.getSource()==save){ if(fname==null){ othersave(); } //如果文件名为空,说明文件未被创建,弹出另存为对话框 try { File savefile=new File(path); savefile.createNewFile(); FileWriter fw=new FileWriter(savefile); fw.write(txt1.getText()); fw.close(); }catch(IOException e){} } if(p.getActionCommand()=="另保存为 "){ othersave(); } if(p.getActionCommand()=="退出 "||p.getSource()==exit){ exit(); } //////////////////编辑//////////////////// if(p.getActionCommand()=="复制 "||p.getSource()==copy) { txt1.copy(); } if(p.getActionCommand()=="剪切 "||p.getSource()==cut) { txt1.cut(); } if(p.getActionCommand()=="粘贴 "||p.getSource()==pp) { txt1.paste(); } if(p.getActionCommand()=="删除 "||p.getSource()==del){ txt1.replaceSelection(""); } /////////////////////////////////////////////////////// if(p.getActionCommand()=="全选 "){ txt1.selectAll(); } if(p.getActionCommand()=="日期/时间 "){ //用DATE类插入当前日期/时间,编译时会出现说明
int
inpo
int
=txt1.get
Caret
Position
(); Date dt=new Date(); String strdate=dt.toLocaleString(); txt1.insert(strdate,inpo
int
);} //格式// //////////字体样式//////////////// if(p.getActionCommand()=="正常 "){ newfont=txt1.getFont(); //得到当前应用字体,获得字体大小,创建相应样式
int
size=newfont.getSize(); txt1.
set
Font(new Font("宋体",Font.PLAIN,size)); } if(p.getActionCommand()=="粗体 "){ newfont=txt1.getFont();
int
size=newfont.getSize(); txt1.
set
Font(new Font("宋体",Font.BOLD,size)); } if(p.getActionCommand()=="斜体 "){ newfont=txt1.getFont();
int
size=newfont.getSize(); txt1.
set
Font(new Font("宋体",Font.ITALIC,size)); } ////////////字体大小///////////////// if(p.getActionCommand()=="最大 "){ newfont=txt1.getFont(); ////得到当前应用字体,获得字体样式,创建相应字体的大小
int
sty=newfont.getStyle() ; txt1.
set
Font(new Font("宋体",sty,35)); } if(p.getActionCommand()=="较大 "){ newfont=txt1.getFont();
int
sty=newfont.getStyle() ; txt1.
set
Font(new Font("宋体",sty,30)); } if(p.getActionCommand()=="适中 "){ newfont=txt1.getFont();
int
sty=newfont.getStyle() ; txt1.
set
Font(new Font("宋体",sty,18)); } if(p.getActionCommand()=="较小 "){ newfont=txt1.getFont();
int
sty=newfont.getStyle() ; txt1.
set
Font(new Font("宋体",sty,16)); } if(p.getActionCommand()=="最小 "){ newfont=txt1.getFont();
int
sty=newfont.getStyle() ; txt1.
set
Font(new Font("宋体",sty,14)); } if(p.getActionCommand()=="字体颜色 "||p.getSource()==color){ //字体颜色 JColorChooser jColor=new JColorChooser(); //调用颜色面板,设置前景就可更改字体颜色 Color fcolor=txt1.getForeground(); txt1.
set
Foreground( jColor.showDialog(txt1,"选择字体颜色",fcolor)); } if(p.getActionCommand()=="自动换行 "){ txt1.
set
LineWrap(true); m34.
set
Enabled(false);} //if(p.getActionCommand()=="系统风格 ") //不稳定,在不同编译平台下表现不一致 // { } if(p.getActionCommand()=="MOTIF风格 "){ this.dispose(); myfr ww=new myfr("我的javaX记事本"); try { //MOTIF界面 UIManager.
set
LookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); SwingUtilities.updateComponentTreeUI(ww); }catch ( Exception e ) {} } if(p.getActionCommand()=="默认风格 "){ this.dispose(); myfr ww=new myfr("猪猪java记事本"); try { UIManager.
set
LookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(ww); }catch ( Exception e ) {} } if(p.getActionCommand()=="状态栏 ") //状态栏的隐藏和显视 {state.
set
Visible( !(state.isVisible()) );} /////////////////////帮助/////////////////////////////////////////// if(p.getActionCommand()=="帮助主题 "||p.getSource()==help){ myhelp help=new myhelp(); help.
set
Size(200,240);} if(p.getActionCommand()=="关于 "){ JLabel prompt=new JLabel("欢迎使用我的JAVAX记事本",JLabel.CENTER); JOptionPane.showMessageDialog(null,prompt,"关于我的JAVAX记事本",JOptionPane.INFORMATION_MESSAGE); } } public void othersave() {//另存为
方法
if(choose.APPROVE_OPTION==choose.showSaveDialog(this)){ path=choose.getSelectedFile().getPath(); newfiles=new File(path); state.
set
Text(" 猪猪java记事本------"+path); fname=choose.getSelectedFile().getName();; try { newfiles=new File(path); newfiles.createNewFile(); FileWriter fw=new FileWriter(newfiles); fw.write(txt1.getText()); fw.close(); }catch(IOException e){} } } public void notfindmethod(){ //提示查找不到的
方法
notfindmethod if(!txt1.getSelectedText().equals(findtxt.getText())) { txt1.
set
Caret
Position
(0); //光标返回文件头部 JOptionPane.showMessageDialog(null,"查找不到对应的字符!","查找错误",JOptionPane.ERROR_MESSAGE); m26.
set
Enabled(false); } } public void exit(){
int
value; String[] qq={"返回", "退出"}; value=JOptionPane.showOptionDialog(null, "你确定退出吗?请注意保存文件!", "退出程序?", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,null, qq, qq[0]); if(value==JOptionPane.YES_OPTION) return; else if(value==JOptionPane.NO_OPTION) { System.exit(0); } } }//主窗口类结束// class xxx extends WindowAdapter{ //关闭窗口XXX类//////////// myfr fx; xxx(myfr fxx) {fx=fxx;} public void windowClosing(WindowEvent e){ JOptionPane.showMessageDialog(null,"欢迎使用本软件!","退出",JOptionPane.INFORMATION_MESSAGE); fx.dispose(); System.exit(0); } } class xxxhelp extends WindowAdapter{ //关闭帮助主题窗口类XXXhelp myhelp fx; xxxhelp(myhelp fxx){ fx=fxx;} public void windowClosing(WindowEvent e){ fx.dispose(); } } class myhelp extends JFrame{ //帮助主题类/////////////////
TextArea
helparea; myhelp() { super("我的帮助主题"); helparea=new
TextArea
("",45,25,
TextArea
.SCROLLBARS_NONE);//创建没有滚动条的帮助窗口 helparea.
set
Text(" "); helparea.append("我的JAVAX记事本是一个用来创建简单的文档的基本的文本编辑器.可用于打开查看TXT文档。"); helparea.append("要编辑文字,请先选定它,然后单击相应菜单上的相关命令。"); helparea.append("鼠标悬停在工具栏上会出现相关提示。本软件在SDK 1.40下编写,"); helparea.append("最好在1.4环境下运行,否则会出现一些小问题,但不影响正常使用……");
set
Resizable(false); getContentPane().add(helparea); show();pack(); helparea.
set
Font(new Font("宋体",Font.PLAIN,14)); helparea.
set
Enabled(false); addWindowListener(new xxxhelp(this)); } } class handlemouse extends MouseAdapter //处理右键弹出菜单类 { myfr fx; handlemouse(myfr fxx){ fx=fxx; } public void mouseReleased(MouseEvent n){ if(n.isPopupTrigger()) fx.popm.show((Component)n.getSource(),n.getX(),n.getY()); } }
java.awt.
TextArea
类
java.awt.
TextArea
是一个文本区组件1.构造
方法
TextArea
():创建的文本区有默认的行数和列数,并有水平和垂直滚动条
TextArea
(String s):创建的文本区的初始字符串为s,并有水平和垂直滚动条
TextArea
(
int
x,
int
y):创建的文本区有x行和y列,并有水平和垂直滚动条2.常用
方法
publi
Java文本区(
TextArea
)
http://blog.sina.com.cn/s/blog_63cefe150100gtek.html 文本区可以输入多行文本,其主要
方法
有:
TextArea
():构造
方法
,创建的文本区对象的行数和列数取默认值。
TextArea
(String s):构造
方法
,创建的文本区初始字符串为s,文本区有水平和垂直滚动条。
TextArea
(
int
x,
int
y):构造
方法
,创建的文
java
textarea
输出_如何将输出流设置为
TextArea
MadProgrammer的解决方案非常棒,我的基础是他的。但是,正如Loopkin所指出的,它不处理特殊字符(准确地说,它在每个非ASCII字符上都失败)。Loopkin的解决方案对我不起作用,但我最终提出了两个完成这项工作的解决方案。解决方案1:处理每1个字节的字符(最多U + 00FF)这个简单的解决方案可以处理U + 00FF(每个1字节字符)的每个字符。一切都与MadProgrammer...
Java SE
62,628
社区成员
307,259
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章