我想在JPanel上绘制一个矩形。能够使用以下代码绘制。public class DrawingColor extends JFrame{ public static void main(String[] args) { DrawingColor d = new DrawingColor(); } public DrawingColor() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(new MyComponent()); setSize(400,400); setVisible(true); } public class MyComponent extends JComponent { @Override public void paint(Graphics g) { int height = 200; int width = 120; g.setColor(Color.red); g.drawRect(10, 10, height, width); g.setColor(Color.gray); g.fillRect(11, 11, height, width); g.setColor(Color.red); g.drawOval(250, 20, height, width); g.setColor(Color.magenta); g.fillOval(249, 19, height, width); } }}但是getContentPane()。add(new MyComponent()); 代替此声明,我需要在框架中添加一个基础面板。在基础面板上,我要添加MyComponent面板。 JPanel basePanel = new JPanel(); basePanel = new MyComponent(); getContentPane().add(basePanel);如果我这样做,该矩形将不可见。任何的想法?而且我还需要在运行时更改矩形的大小。可能吗?
添加回答
举报
0/150
提交
取消