一个Frame中有多个Panel,想在其中一个指定的Panel中画图,怎么实现呢 50
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏50(财富值+成长值)
1个回答
展开全部
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
public class Test extends JFrame
{
private static final long serialVersionUID = 1L;
Test()
{
setSize(400, 300);
setLayout(new FlowLayout());
int num = 10;
int rand = (int) (Math.random() * num);
for(int i = 0; i < num; i++)
{
JPanel panel;
if(i == rand)
{
panel = new JPanel()
{
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.orange);
g.fillOval(15, 15, 20, 20);
g.dispose();
}
};
}
else
{
panel = new JPanel();
}
panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.red, Color.black));
panel.setPreferredSize(new Dimension(40, 50));
add(panel);
}
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new Test().setVisible(true);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询