77范文网 - 专业文章范例文档资料分享平台

Java语言程序设计习题答案(清华大学出版杜)张思民版-1-12章答案(7)

来源:网络收集 时间:2018-11-22 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton(\

c.weightx = 0.0; //reset to the default

makebutton(\

c.gridwidth = 2; //GridBagConstraints.RELATIVE; //next-to-last in row makebutton(\

c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton(\

c.gridwidth = 1; //reset to the default c.gridheight = 2; c.weighty = 1.0;

makebutton(\

c.weighty = 1.0; //reset to the default

c.gridwidth = GridBagConstraints.REMAINDER; //end row c.gridheight = 1; //reset to the default makebutton(\ makebutton(\ setSize(300, 100); }

public static void main(String args[]) {

final Frame f = new Frame(\演示\ f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent evt) { f.setVisible(false); f.dispose(); System.exit(0); } });

MyGridBagLayout gb = new MyGridBagLayout(); gb.go();

f.add(\ f.pack();

f.setVisible(true); } }

程序运行结果见图10-7。

图10-7 网格包(GridBag)布局

31

【12】根据本章所学的内容编程:设计一个模拟的文字编辑器,并用菜单实现退出的功能。 [解答]:Test4_12.java import java.awt.*;

import java.awt.event.*;

public class Test4_12 extends Frame implements ActionListener{ MenuBar mbar; Menu file,edit,help;

MenuItem file_open,file_new,file_save,file_exit; MenuItem edit_copy,edit_cut,edit_pase,edit_del; MenuItem help_about;

Button btn_copy,btn_pase,btn_cut,btn_del;

TextArea txtAr; boolean ifdo;

StringBuffer strtmp;

public Test4_12(){

super(\菜单Application程序\ setSize(400,300); setVisible(true);

setLayout(new BorderLayout());

mbar=new MenuBar(); setMenuBar(mbar);

strtmp=new StringBuffer();

file=new Menu(\文件\ edit=new Menu(\编辑\ help=new Menu(\帮助\ mbar.add(file); mbar.add(edit); mbar.add(help);

file_new=new MenuItem(\新建\ file_open=new MenuItem(\打开\ file_open.setEnabled(false);

file_save=new MenuItem(\保存\ file_save.setEnabled(false);

file_exit=new MenuItem(\退出\ file.add(file_new);

32

file.add(file_open); file.add(file_save); file.addSeparator(); file.add(file_exit);

edit_copy=new MenuItem(\复制\ edit_pase=new MenuItem(\粘贴\ edit_cut=new MenuItem(\剪切\

edit_del=new MenuItem(\删除\ edit.add(edit_copy); edit.add(edit_pase); edit.add(edit_cut); edit.add(edit_del);

help_about=new MenuItem(\关于\ help.add(help_about);

txtAr=new TextArea(8,10); Panel p1=new Panel();

btn_copy=new Button(\复制\ btn_cut=new Button(\剪切\ btn_pase=new Button(\粘贴\ btn_del=new Button(\删除\

p1.setLayout(new FlowLayout(FlowLayout.LEFT));

p1.add(btn_copy);p1.add(btn_cut);p1.add(btn_pase);p1.add(btn_del); add(\ add(\ validate();

file_new.addActionListener(this); file_open.addActionListener(this); file_save.addActionListener(this); file_exit.addActionListener(this); edit_copy.addActionListener(this); edit_pase.addActionListener(this); edit_cut.addActionListener(this); edit_del.addActionListener(this); help_about.addActionListener(this); btn_copy.addActionListener(this); btn_cut.addActionListener(this); btn_pase.addActionListener(this); btn_del.addActionListener(this);

addWindowListener(new WindowAdapter(){

33

public void windowClosing(WindowEvent we){ System.exit(0); }} ); }

public void actionPerformed(ActionEvent e){ if (e.getSource()==file_new){ txtAr.setText(null); }

else if (e.getSource()==file_open){ }

else if (e.getSource()==file_save){ }

else if (e.getSource()==file_exit){ System.exit(0); //退出 }

else if (e.getSource()==edit_copy||e.getSource()==btn_copy){ //复制的实现 strtmp.delete(0,strtmp.length());

strtmp.append(txtAr.getSelectedText()); }

else if (e.getSource()==edit_pase||e.getSource()==btn_pase){ //粘贴的实现 txtAr.insert(strtmp.toString(),txtAr.getSelectionEnd()); }

else if (e.getSource()==edit_cut||e.getSource()==btn_cut){ //剪切的实现 strtmp.delete(0,strtmp.length());

strtmp.append(txtAr.getSelectedText());

String strtmp1=new String(txtAr.getText().substring(0,txtAr.getSelectionStart())); String strtmp2=new String(txtAr.getText().substring(txtAr.getSelectionEnd(),txtAr.getText().length())); //返回一个新的 String,它包含此序列当前所包含的字符子序列。 txtAr.setText(strtmp1+strtmp2); }

else if(e.getSource()==edit_del||e.getSource()==btn_del){

String strtmp1=new String(txtAr.getText().substring(0,txtAr.getSelectionStart())); String strtmp2=new String(txtAr.getText().substring(txtAr.getSelectionEnd(),txtAr.getText().length())); //返回一个新的 String,它包含此序列当前所包含的字符子序列。 txtAr.setText(strtmp1+strtmp2); } }

public static void main(String[] args){ new Test4_12(); }

34

}

【13】创建一个输入对话框,从对话框中输入文字,当按下“确定”按钮后,能在屏幕上显示输入的文字。

[解答]:Test4_13.java import java.awt.*;

import java.awt.event.*;

class Test4_13 extends Frame implements ActionListener{ TextArea txtAr; Button btn1;

public Test4_13(){ super(\对话框练习\ setSize(300,400); setVisible(true); setLayout(new BorderLayout()); btn1=new Button(\打开对话框\ txtAr=new TextArea(8,10); add(\ add(\ validate(); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); }} ); btn1.addActionListener(this); }

public void actionPerformed(ActionEvent e){

DiaObj dia=new DiaObj(this,txtAr); //声明和实例话对话框,传递frame,和txtar参数 }

public static void main(String[] args){ new Test4_13(); } }

35

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Java语言程序设计习题答案(清华大学出版杜)张思民版-1-12章答案(7)在线全文阅读。

Java语言程序设计习题答案(清华大学出版杜)张思民版-1-12章答案(7).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/300115.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: