16.6

习题标准答案:

 

 1 import java.awt.*;
 2 import java.awt.event.*;
 3 import javax.swing.*;
 4 
 5 public class Exercise16_6 extends JFrame {
 6   private DisplayPanel panel = new DisplayPanel();
 7 
 8   public Exercise16_6() {
 9     add(panel, BorderLayout.CENTER);
10     panel.setFocusable(true);
11   }
12 
13   /** Main method */
14   public static void main(String[] args) {
15     JFrame frame = new Exercise16_6();
16     frame.setTitle("Exercise16_6");
17     frame.setSize(300, 300);
18     frame.setLocationRelativeTo(null); // Center the frame
19     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20     frame.setVisible(true);
21   }
22 
23   class DisplayPanel extends MessagePanel {
24     private String m1 = "Java is fun";
25     private String m2 = "Java is powerful";
26     private boolean isM1;
27 
28     public DisplayPanel() {
29       setCentered(true);
30       this.addMouseListener(new MouseAdapter() {
31         public void mouseClicked(MouseEvent e) {
32           if (isM1) setMessage(m1);
33           else setMessage(m2);
34 
35           isM1 = !isM1;
36         }
37       });
38     }
39   }
40 }
Exercise16_6.java

 

我的答案:

 1 import java.awt.*;
 2 import java.awt.event.*;
 3 import javax.swing.*;
 4 
 5 public class Test_16_6 extends JFrame {
 6     MessagePanel mPanel = new MessagePanel();
 7     
 8   public Test_16_6() {      
 9       add(mPanel);
10       mPanel.addMouseListener(new mouseListener());
11   }
12 
13   /** Main method */
14   public static void main(String[] args) {
15     JFrame frame = new Test_16_6();
16     frame.setTitle("Exercise16_6");
17     frame.setSize(300, 300);
18     frame.setLocationRelativeTo(null); // Center the frame
19     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20     frame.setVisible(true);
21   } 
22   
23   class mouseListener implements MouseListener{
24       boolean clicked = false;
25     @Override
26     public void mouseClicked(MouseEvent e) {
27         // TODO Auto-generated method stub
28         if(clicked == false)
29             { mPanel.setMessage("Java is fun"); clicked = true; }
30             else if(clicked == true)
31             { mPanel.setMessage("Java is powerful"); clicked = false;}
32         }
33     @Override
34     public void mouseEntered(MouseEvent e) {
35         // TODO Auto-generated method stub
36         
37     }
38     @Override
39     public void mouseExited(MouseEvent e) {
40         // TODO Auto-generated method stub
41         
42     }
43     @Override
44     public void mousePressed(MouseEvent e) {
45         // TODO Auto-generated method stub
46         
47     }
48     @Override
49     public void mouseReleased(MouseEvent e) {
50         // TODO Auto-generated method stub
51         
52     }      
53   }
54 }
Test_16_6.java

 

需要用到的类:

  1 // MessagePanel.java: Display a message on a JPanel
  2 import java.awt.Font;
  3 import java.awt.FontMetrics;
  4 import java.awt.Dimension;
  5 import java.awt.Graphics;
  6 import javax.swing.JPanel;
  7 
  8 public class MessagePanel extends JPanel {
  9   /** The message to be displayed */
 10   private String message = "Welcome to Java";
 11 
 12   /** The x coordinate where the message is displayed */
 13   private int xCoordinate = 20;
 14 
 15   /** The y coordinate where the message is displayed */
 16   private int yCoordinate = 20;
 17 
 18   /** Indicate whether the message is displayed in the center */
 19   private boolean centered;
 20 
 21   /** The interval for moving the message horizontally and vertically */
 22   private int interval = 10;
 23 
 24   /** Default constructor */
 25   public MessagePanel() {
 26   }
 27 
 28   /** Constructor with a message parameter */
 29   public MessagePanel(String message) {
 30     this.message = message;
 31   }
 32 
 33   /** Return message */
 34   public String getMessage() {
 35     return message;
 36   }
 37 
 38   /** Set a new message */
 39   public void setMessage(String message) {
 40     this.message = message;
 41     repaint();
 42   }
 43 
 44   /** Return xCoordinator */
 45   public int getXCoordinate() {
 46     return xCoordinate;
 47   }
 48 
 49   /** Set a new xCoordinator */
 50   public void setXCoordinate(int x) {
 51     this.xCoordinate = x;
 52     repaint();
 53   }
 54 
 55   /** Return yCoordinator */
 56   public int getYCoordinate() {
 57     return yCoordinate;
 58   }
 59 
 60   /** Set a new yCoordinator */
 61   public void setYCoordinate(int y) {
 62     this.yCoordinate = y;
 63     repaint();
 64   }
 65 
 66   /** Return centered */
 67   public boolean isCentered() {
 68     return centered;
 69   }
 70 
 71   /** Set a new centered */
 72   public void setCentered(boolean centered) {
 73     this.centered = centered;
 74     repaint();
 75   }
 76 
 77   /** Return interval */
 78   public int getInterval() {
 79     return interval;
 80   }
 81 
 82   /** Set a new interval */
 83   public void setInterval(int interval) {
 84     this.interval = interval;
 85     repaint();
 86   }
 87 
 88   /** Paint the message */
 89   protected void paintComponent(Graphics g) {
 90     super.paintComponent(g);
 91 
 92     if (centered) {
 93       // Get font metrics for the current font
 94       FontMetrics fm = g.getFontMetrics();
 95 
 96       // Find the center location to display
 97       int stringWidth = fm.stringWidth(message);
 98       int stringAscent = fm.getAscent();
 99       // Get the position of the leftmost character in the baseline
100       xCoordinate = getWidth() / 2 - stringWidth / 2;
101       yCoordinate = getHeight() / 2 + stringAscent / 2;
102     }
103 
104     g.drawString(message, xCoordinate, yCoordinate);
105   }
106 
107   /** Move the message left */
108   public void moveLeft() {
109     xCoordinate -= interval;
110     repaint();
111   }
112 
113   /** Move the message right */
114   public void moveRight() {
115     xCoordinate += interval;
116     repaint();
117   }
118 
119   /** Move the message up */
120   public void moveUp() {
121     yCoordinate -= interval;
122     repaint();
123   }
124 
125   /** Move the message down */
126   public void moveDown() {
127     yCoordinate -= interval;
128     repaint();
129   }
130 
131   /** Override get method for preferredSize */
132   public Dimension getPreferredSize() {
133     return new Dimension(200, 30);
134   }
135 }
MessagePanel.java

效果图:

 

posted on 2016-07-03 02:17  功夫茶茶  阅读(169)  评论(0编辑  收藏  举报

导航