17.1
1 import java.awt.*; 2 import java.awt.event.ActionEvent; 3 import java.awt.event.ActionListener; 4 import javax.swing.*; 5 6 public class ButtonDemo extends JFrame{ 7 8 protected MessagePanel messagePanel = new MessagePanel("Welcome to Java"); 9 10 private JButton jbtLeft = new JButton("<="); 11 private JButton jbtRight = new JButton("=>"); 12 13 //add color raido buttons 14 ButtonGroup group = new ButtonGroup(); 15 JRadioButton jrb1 = new JRadioButton("red"); 16 JRadioButton jrb2 = new JRadioButton("blue"); 17 JRadioButton jrb3 = new JRadioButton("yellow"); 18 19 public ButtonDemo(){ 20 messagePanel.setBackground(Color.white); 21 22 JPanel jpButtons = new JPanel(); 23 jpButtons.add(jbtLeft); 24 jpButtons.add(jbtRight); 25 26 JPanel jgButtons = new JPanel(); 27 jgButtons.setLayout(new GridLayout(1,3)); 28 jgButtons.add(jrb1); 29 jgButtons.add(jrb2); 30 jgButtons.add(jrb3); 31 32 group.add(jrb1); 33 group.add(jrb2); 34 group.add(jrb3); 35 36 37 jbtLeft.setMnemonic('L'); 38 jbtRight.setMnemonic('R'); 39 40 jbtLeft.setToolTipText("Move message to left"); 41 jbtRight.setToolTipText("Move message to right"); 42 43 setLayout(new BorderLayout()); 44 add(jgButtons,BorderLayout.NORTH); 45 add(messagePanel,BorderLayout.CENTER); 46 add(jpButtons,BorderLayout.SOUTH); 47 48 jbtLeft.addActionListener(new ActionListener(){ 49 50 @Override 51 public void actionPerformed(ActionEvent e) { 52 // TODO Auto-generated method stub 53 messagePanel.moveLeft(); 54 } 55 56 }); 57 jbtRight.addActionListener(new ActionListener(){ 58 59 @Override 60 public void actionPerformed(ActionEvent e) { 61 // TODO Auto-generated method stub 62 messagePanel.moveRight(); 63 } 64 65 }); 66 jrb1.addActionListener(new ActionListener(){ 67 68 @Override 69 public void actionPerformed(ActionEvent arg0) { 70 // TODO Auto-generated method stub 71 messagePanel.setColor(Color.RED); 72 } 73 74 }); 75 jrb2.addActionListener(new ActionListener(){ 76 77 @Override 78 public void actionPerformed(ActionEvent arg0) { 79 // TODO Auto-generated method stub 80 messagePanel.setColor(Color.BLUE); 81 } 82 83 }); 84 jrb3.addActionListener(new ActionListener(){ 85 86 @Override 87 public void actionPerformed(ActionEvent arg0) { 88 // TODO Auto-generated method stub 89 messagePanel.setColor(Color.YELLOW); 90 } 91 92 }); 93 } 94 95 public static void main(String[] args){ 96 ButtonDemo frame = new ButtonDemo(); 97 frame.setTitle("ButtonDemo"); 98 frame.pack(); 99 frame.setLocationRelativeTo(null); 100 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 101 frame.setVisible(true); 102 } 103 }
1 // MessagePanel.java: Display a message on a JPanel 2 import java.awt.Font; 3 import java.awt.FontMetrics; 4 import java.awt.Color; 5 import java.awt.Dimension; 6 import java.awt.Graphics; 7 import javax.swing.JPanel; 8 9 public class MessagePanel extends JPanel { 10 /** The message to be displayed */ 11 private String message = "Welcome to Java"; 12 13 /** The x coordinate where the message is displayed */ 14 private int xCoordinate = 20; 15 16 /** The y coordinate where the message is displayed */ 17 private int yCoordinate = 20; 18 19 /** Indicate whether the message is displayed in the center */ 20 private boolean centered; 21 22 /** The interval for moving the message horizontally and vertically */ 23 private int interval = 10; 24 25 private Color myColor = Color.BLACK; 26 /** Default constructor */ 27 public MessagePanel() { 28 } 29 30 /** Constructor with a message parameter */ 31 public MessagePanel(String message) { 32 this.message = message; 33 } 34 35 /** Return message */ 36 public String getMessage() { 37 return message; 38 } 39 40 /** Set a new message */ 41 public void setMessage(String message) { 42 this.message = message; 43 repaint(); 44 } 45 46 /** Return xCoordinator */ 47 public int getXCoordinate() { 48 return xCoordinate; 49 } 50 51 /** Set a new xCoordinator */ 52 public void setXCoordinate(int x) { 53 this.xCoordinate = x; 54 repaint(); 55 } 56 57 /** Return yCoordinator */ 58 public int getYCoordinate() { 59 return yCoordinate; 60 } 61 62 /** Set a new yCoordinator */ 63 public void setYCoordinate(int y) { 64 this.yCoordinate = y; 65 repaint(); 66 } 67 68 /** Return centered */ 69 public boolean isCentered() { 70 return centered; 71 } 72 73 /** Set a new centered */ 74 public void setCentered(boolean centered) { 75 this.centered = centered; 76 repaint(); 77 } 78 79 /** Return interval */ 80 public int getInterval() { 81 return interval; 82 } 83 84 /** Set a new interval */ 85 public void setInterval(int interval) { 86 this.interval = interval; 87 repaint(); 88 } 89 90 /** Paint the message */ 91 protected void paintComponent(Graphics g) { 92 super.paintComponent(g); 93 94 if (centered) { 95 // Get font metrics for the current font 96 FontMetrics fm = g.getFontMetrics(); 97 98 // Find the center location to display 99 int stringWidth = fm.stringWidth(message); 100 int stringAscent = fm.getAscent(); 101 // Get the position of the leftmost character in the baseline 102 xCoordinate = getWidth() / 2 - stringWidth / 2; 103 yCoordinate = getHeight() / 2 + stringAscent / 2; 104 } 105 g.setColor(myColor); 106 g.drawString(message, xCoordinate, yCoordinate); 107 } 108 109 /** Move the message left */ 110 public void moveLeft() { 111 xCoordinate -= interval; 112 repaint(); 113 } 114 115 /** Move the message right */ 116 public void moveRight() { 117 xCoordinate += interval; 118 repaint(); 119 } 120 121 /** Move the message up */ 122 public void moveUp() { 123 yCoordinate -= interval; 124 repaint(); 125 } 126 127 /** Move the message down */ 128 public void moveDown() { 129 yCoordinate -= interval; 130 repaint(); 131 } 132 133 /** Override get method for preferredSize */ 134 public Dimension getPreferredSize() { 135 return new Dimension(200, 30); 136 } 137 138 public void setColor(Color color){ 139 this.myColor = color; 140 repaint(); 141 } 142 }
效果图: