?? driver.java
字號:
import java.awt.*;import java.awt.event.*;import javax.swing.*;/** Traversal of Oval List Program (Figure 11.11) * Author: David D. Riley * Date: JANUARY, 2005 */public class Driver implements ActionListener { private JFrame window; private JButton greenBtn, blueBtn, coordinateBtn; private SimpleList<Oval> ovalList; /** post: window is created * and a row of black ovals spans window horizontally * and the row of ovals is stored in ovalList */ public Driver() { Oval tempOval; int horizontalPos; window = new JFrame("Manipulate the Ovals"); window.setBounds(10, 10, 600, 500); window.setVisible(true); window.setLayout(null); greenBtn = newButton(20, 430, "Color Green"); blueBtn = newButton(210, 430, "Color Blue"); coordinateBtn = newButton(400, 430, "Print Coordinates"); ovalList = new SimpleList<Oval>(); horizontalPos = 5; ovalList.reset(); while ( horizontalPos < window.getWidth() ) { tempOval = new Oval(horizontalPos, 100, 10, 10); window.add(tempOval, 0); ovalList.add( tempOval ); horizontalPos = horizontalPos + 15; } window.repaint(); } /** pre: window != null <br> * post: result is a newly created button * and result.getX()==x and result.getY()==y * and result.getWidth()==180 and result.getHeight()==30 * and result.getParent() == window's pane * and this is the action listener for result */ public JButton newButton(int x, int y, String s) { JButton button; button = new JButton(s); button.setBounds(x, y, 180, 30); button.addActionListener(this); window.add(button, 0); return button; } /** post: greenBtn was clicked * implies all ovals are recolored green * and blueBtn was clicked * implies all ovals are recolored blue * and coordinateBtn was clicked * implies the X coordinate of all ovals are output */ public void actionPerformed(ActionEvent e) { if (e.getSource() == coordinateBtn) { for (Oval tempOval : ovalList) { System.out.println( "X coordinate of oval: " + tempOval.getX() ); } } else { // either greenBtn or blueBtn was clicked for (Oval tempOval : ovalList) { if (e.getSource()==greenBtn) tempOval.setBackground( Color.green ); else tempOval.setBackground( Color.blue ); tempOval.repaint(); } } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -