我有一个代码。它工作正常,直到我在其中添加了一个额外的方法
I have a code. It works fine till I put an extra method in it
我的代码有两个 类。如果我启动代码,我会得到一个图像。图像的下方有那么长的白色条,直到我有了最后两个方法 getHeight 和 getWidth。
现在我的问题是:为什么没有这两种方法一切正常?我被告知我应该有这两个 API 方法以便稍后使用 JUnit 进行测试。
抱歉,我的英语不好 ;)
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();
}
}
getWidth
和getHeight
已经在Component
中定义了,是JFrame的超class。您不应覆盖这些方法。相反,您应该将您的方法命名为不同的东西
我的代码有两个 类。如果我启动代码,我会得到一个图像。图像的下方有那么长的白色条,直到我有了最后两个方法 getHeight 和 getWidth。
现在我的问题是:为什么没有这两种方法一切正常?我被告知我应该有这两个 API 方法以便稍后使用 JUnit 进行测试。
抱歉,我的英语不好 ;)
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();
}
}
getWidth
和getHeight
已经在Component
中定义了,是JFrame的超class。您不应覆盖这些方法。相反,您应该将您的方法命名为不同的东西