Java 我有密码。在我加入一个额外的方法之前,它工作得很好
我有两个类的代码。如果我启动代码,我会得到一个图像。图像的下方有一个很长的白色条,直到我有最后两个方法getHeight和getWidth 现在我的问题是:没有这两种方法,为什么一切都能正常工作?有人告诉我应该有这两个API方法,以便稍后使用JUnit进行测试 Sry,为我糟糕的英语Java 我有密码。在我加入一个额外的方法之前,它工作得很好,java,swing,awt,Java,Swing,Awt,我有两个类的代码。如果我启动代码,我会得到一个图像。图像的下方有一个很长的白色条,直到我有最后两个方法getHeight和getWidth 现在我的问题是:没有这两种方法,为什么一切都能正常工作?有人告诉我应该有这两个API方法,以便稍后使用JUnit进行测试 Sry,为我糟糕的英语 package mydraw; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DrawImage
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的一个超类。您不应该重写这些方法。相反,您应该给您的方法命名一些不同的名称,getWidth和getHeight已经在Component中定义了,这是JFrame的一个超类。您不应该重写这些方法。相反,你应该给你的方法命名一些不同的或者更好的名称:不要扩展JFrame:D。但是你的答案仍然正确:。或者更好:不要扩展JFrame:D。但是你的答案仍然正确:。你得到了什么错误?你怎么知道它不起作用?请更新您的问题以包含该信息,即使ControlAltDel的答案可能是您问题的解决方案。您得到的错误是什么?你怎么知道它不起作用?请更新您的问题以包含该信息,即使ControlAltDel的答案可能是您问题的解决方案。