import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class FirstTry extends JPanel { Color color; public FirstTry(Color color) {
this.color = color;
this.setLayout(null);
} public void paintComponent(Graphics g) {
int width = getWidth();
int height = getHeight();
g.setColor(color);
//g.drawOval(0, 0, width, height);
for(int i=0; i<10;i++){
JLabel label = new JLabel();
label.setBounds(i*3,i*4,50,20);
label.setText("yyy");
label.paint(g);
}
} public static void main(String args[]) {
JFrame frame = new JFrame("Oval Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new GridLayout(2, 2)); Color colors[] = { Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW };
for (int i = 0; i < 4; i++) {
FirstTry panel = new FirstTry(colors[i]);
panel.setLayout(null);
frame.add(panel);
} frame.setSize(300, 200);
frame.setVisible(true);
}
}
我想要这些在paintComponent里建出来的label们可以在各自的位置。 可是setbounds完全被无视了啊。
请求大牛指点。比较急的。小女在此谢过各位
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货