2016-04-28 6 views
1

Ich habe meinen Code mit zwei Klassen. Wenn ich den Code starte, erhalte ich ein Image. Das Bild hat so lange einen weißen Balken auf dem Down, bis ich die beiden letzten Methoden getHeight und getWidth habe.Ich habe einen Code. Es funktioniert gut, bis ich eine zusätzliche Methode in es

Jetzt meine Frage: Warum funktioniert alles gut ohne diese beiden Methoden? Mir wurde gesagt, dass ich diese zwei API-Methoden haben sollte, um später mit JUnit zu testen.

Sry, für mein schlechtes Englisch;)

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

Was ist der Fehler, den Sie bekommen? Woher weißt du, dass es nicht funktioniert? Bitte aktualisieren Sie Ihre Frage, um diese Informationen aufzunehmen, obwohl die Antwort von ControlAltDel wahrscheinlich die Lösung für Ihr Problem ist. – dchayka

Antwort

7

getWidth und getHeight sind bereits in Component, eine Superklasse von JFrame definiert. Sie sollten diese Methoden nicht überschreiben. Stattdessen sollten Sie Ihre Methoden etwas anderes benennen

+0

Oder besser noch: 'JFrame' nicht verlängern: D. Aber deine Antwort ist immer noch korrekt :). – Tom