私は netbean のグラフィック システムにまったく慣れていないので、Java の教科書に苦労しています。私はいくつかのものを表示するための簡単なプログラムを作成しようとしています. 私の調査で、同様の問題を抱えている他の多くの人々を見つけました。これらの人々は、寸法とpreferredSizeメソッドを使用するように言われる傾向があります. 以下は私のコードです:
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);
}
}