私はnetbeanのグラフィックスシステムを完全に新しくしており、Javaの教科書では苦労しています。私はいくつかのことを表示するための簡単なプログラムを作りたいと思っています。私の研究では、同様の問題を抱える他の人々がいました。これらの人々はdimensionとpreferredSizeメソッドを使用するように言われる傾向がありますが、どちらもJavaで再現しようとしている本のセクションで言及されていません。以下は私のコードです:PaintComponentはnetbeansと呼ばれていませんGUI
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame frame = new JFrame(); //create frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //makes x button end program
frame.setSize(300,200); //determine the size of the frame
ImageIcon image = new ImageIcon("panda.jpg");
ColorPanel p = new ColorPanel(Color.pink, image);
Container pane = frame.getContentPane();
pane.add(p);
frame.setVisible(true); //make frame show up
}
}
public class ColorPanel extends JPanel {
ImageIcon image;
public ColorPanel(Color c, ImageIcon i){
setBackground(c);
image = i;
}
@Override
public void paintComponents(Graphics g){
super.paintComponents(g);
setPreferredSize(new Dimension(100,100));
System.out.println("Blah!");
g.setColor(Color.blue);
g.drawRect(10,25,40,30);
}
}
ありがとう、ありがとう、ありがとうすべては今完璧です:) – Anonymous