我有一个代码.它工作正常,直到我在其中放入一个额外的方法

Son*_*ius 1 java swing awt

我有两个类的代码.如果我启动代码,我会得到一个图像.Image有一个长长的白色条,直到我有两个最后的方法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();
}
}
Run Code Online (Sandbox Code Playgroud)

Con*_*Del 7

getWidth并且getHeight已经在ComponentJFrame的超类中定义.您不应该覆盖这些方法.相反,你应该为你的方法命名不同的东西