beginX = endX = me.getX(); beginY = endY = me.getY(); }
public void mouseReleased(MouseEvent me) { }
public void mouseMoved(MouseEvent me) {
showStatus(\ }
public void mouseDragged(MouseEvent me) {
endX = me.getX(); endY = me.getY();
showStatus(\ repaint(); } }
7-22 改写7-18题的程序,使用一个CANVAS及其上的字符串来显示各选择组件确定的显示效果。
import java.applet.*; import java.awt.*;
import java.awt.event.*;
public class ch7_e7_22 extends Applet implements ItemListener {
Checkbox ckb = new Checkbox(\背景色\ CheckboxGroup style = new CheckboxGroup(); Checkbox p = new Checkbox(\普通\ Checkbox b = new Checkbox(\黑体\ Checkbox i = new Checkbox(\斜体\ Choice size = new Choice();
MyCanvas myCanvas = new MyCanvas();
public void init() {
add(ckb); add(p); add(b); add(i);
size.add(\ size.add(\ size.add(\ add(size);
add(myCanvas);
//myCanvas.setVisible(true); //myCanvas.requestFocus();
myCanvas.setSize(new Dimension(400,200));
p.addItemListener(this); b.addItemListener(this); i.addItemListener(this); ckb.addItemListener(this); size.addItemListener(this); }
public void itemStateChanged(ItemEvent e) {
Checkbox temp;
Font oldF = myCanvas.getFont(); String s; int si;
if(e.getItemSelectable() instanceof Checkbox) {
temp = (Checkbox)(e.getItemSelectable()); if(temp.getLabel()==\背景色\ if(temp.getState())
myCanvas.setBackground(Color.cyan); else
myCanvas.setBackground(Color.gray);
else if(temp.getLabel()==\普通\
myCanvas.setFont(new Font(oldF.getName(),Font.PLAIN,oldF.getSize())); else if(temp.getLabel()==\黑体\
myCanvas.setFont(new Font(oldF.getName(),Font.BOLD,oldF.getSize())); else if(temp.getLabel()==\斜体\
myCanvas.setFont(new Font(oldF.getName(),Font.ITALIC,oldF.getSize())); repaint(); }
else if(e.getItemSelectable() instanceof Choice) {
Choice ctemp = (Choice)(e.getItemSelectable()); s = ctemp.getSelectedItem(); si = Integer.parseInt(s);
myCanvas.setFont(new Font(oldF.getName(),oldF.getStyle(),si)); repaint(); } } }
class MyCanvas extends Canvas {
public void paint(Graphics g) {
g.drawString(\ } }
7-23 什么是容器的布局策略?试例举并简述JAVA中常用的几种布局策略
容器的布局策略用来指定这个容器如何组织其中各控件的相互显示位置关系。Java中常用的布局策略有:FlowLayout,BorderLayout,CardLayout,GridLayout,GridBagLayout。
7-27 将7-15题改写为图形界面的Application程序。 import java.awt.*;
import java.awt.event.*;
public class ch7_e7_27 {
public static void main(String args[]) {
new MyFrame(); } }
class MyFrame extends Frame
implements ActionListener, WindowListener {
Label outputLbl = new Label(\ \ TextField inputTfd = new TextField(10); Button copyBtn = new Button(\复制\
MyFrame() {
super(\我的窗口\ inputTfd.setText(\
outputLbl.setText(\ \ setLayout(new FlowLayout()); add(inputTfd); add(copyBtn); add(outputLbl);
copyBtn.addActionListener(this); addWindowListener(this);
//setSize(300,200); //setVisible(true); pack(); show(); }
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == copyBtn) {
outputLbl.setText(inputTfd.getText()); } }
public void windowClosing(WindowEvent we) {
dispose();
System.exit(0); }
public void windowActivated(WindowEvent we){} public void windowClosed(WindowEvent we){}
public void windowDeactivated(WindowEvent we){} public void windowOpened(WindowEvent we){} public void windowIconified(WindowEvent we){} public void windowDeiconified(WindowEvent we){} }
7-30 编写图形界面的Application程序,包含一个菜单,选择这个菜单的“退出”选项可以关闭Application的窗口并结束程序。 import java.awt.*;
import java.awt.event.*;
public class ch7_e7_30 {
public static void main(String args[]) {
new MyFrame(); } }
class MyFrame extends Frame implements ActionListener {
MenuBar m_MenuBar; Menu menuFile;
MenuItem mi_File_Exit;
MyFrame() {
super(\我的窗口\
m_MenuBar = new MenuBar();
menuFile = new Menu(\文件\
mi_File_Exit = new MenuItem(\退出\ mi_File_Exit.setActionCommand(\退出\ mi_File_Exit.addActionListener(this); menuFile.add(mi_File_Exit); m_MenuBar.add(menuFile);
this.setMenuBar(m_MenuBar);
this.addWindowListener(new closeWindow());
setSize(300,200); setVisible(true); }
public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand().equals(\退出\ {
dispose();
System.exit(0); } } }
class closeWindow extends WindowAdapter {
public void windowClosing(WindowEvent we)