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)在线全文阅读。
相关推荐: