2016-04-28 30 views
1

我有我的代碼與兩個類。如果我啓動代碼,我會得到一個圖像。圖像有一個長長的白色條,直到我有兩個最後的方法getHeight和getWidth。我有一個代碼。它工作正常,直到我把一個額外的方法在它

現在我的問題:爲什麼一切工作正常,沒有這兩種方法?我被告知應該有這兩種API方法以便稍後用JUnit進行測試。

Sry基因,我的英文不好;)

package mydraw; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 


public class DrawImageMini { 

public static void main(String[] args) throws ColorException {new DrawImageMini();} 

/** Application constructor: create an instance of our GUI class */ 
public DrawImageMini() throws ColorException { window = new DrawMiniGUI(this); } 

protected JFrame window; 
} 

class DrawMiniGUI extends JFrame { 
DrawImageMini app; 
Container  cp; 
NavigationPanel navigationPanel; 
JPanel   drawPanel; 

/** 
* The GUI constructor does all the work of creating the GUI and setting 
* up event listeners. Note the use of local and anonymous classes. 
*/ 
public DrawMiniGUI(DrawImageMini application) throws ColorException { 
    super("Draw");  // Create the window 
    app = application; // Remember the application reference 

    // selector for drawing modes 
    JComboBox shape_chooser = new JComboBox(); 
    shape_chooser.addItem("Scribble"); 
    shape_chooser.addItem("Rectangle"); 
    shape_chooser.addItem("Oval"); 

    // selector for drawing colors 
    JComboBox color_chooser = new JComboBox(); 
    color_chooser.addItem("Black"); 
    color_chooser.addItem("Blue"); 
    color_chooser.addItem("Red"); 
    color_chooser.addItem("Green"); 

    // Create two buttons 
    JButton clear = new JButton("Clear"); 
    JButton quit = new JButton("Quit"); 

    // Set a LayoutManager, and add the choosers and buttons to the window. 
    cp = this.getContentPane(); 
    cp.setLayout(new BorderLayout()); 

    // Setzt einen Panel, die Buttons in einer Leiste hat. 
    navigationPanel = new NavigationPanel(new FlowLayout()); 
    navigationPanel.add(new JLabel("Shape:")); 
    navigationPanel.add(shape_chooser); 
    navigationPanel.add(new JLabel("Color:")); 
    navigationPanel.add(color_chooser); 
    navigationPanel.add(quit); 
    navigationPanel.add(clear); 
    navigationPanel.setBackground(Color.magenta); 

    // Setzt den Panel, auf dem gemalt wird 
    drawPanel = new JPanel(); 

    cp.add(navigationPanel, BorderLayout.NORTH, 0); 
    cp.add(drawPanel, BorderLayout.CENTER, 1); 

    // Handle the window close request similarly 
    this.addWindowListener(new WindowAdapter() { 
     public void windowClosing(WindowEvent e) { 
      app.window.dispose(); 
      System.exit(0); 
     } 
    }); 

    // Finally, set the size of the window, and pop it up 
    drawPanel.setPreferredSize(new Dimension(600, 600)); 
    this.pack(); 
    drawPanel.setBackground(Color.red); 
    this.setVisible(true); 
} 
public int getHeight(){ 
    return drawPanel.getHeight(); 
} 

public int getWidth(){ 
    return drawPanel.getWidth(); 
} 
} 
+0

你得到的錯誤是什麼?你怎麼知道它不起作用?即使ControlAltDel的答案可能是解決您的問題的方法,請更新您的問題以包含該信息。 – dchayka

回答

7

getWidthgetHeightComponent已定義,一個超類JFrame的。你不應該重寫這些方法。相反,你應該爲你的方法命名一些不同的東西

+0

甚至更​​好:不要擴展'JFrame':D。但你的回答仍然是正確的:)。 – Tom

相關問題