为什么JPanel子类中paintcomponent被调用了但是画不出结果来???跪求啊。。。我没财富值了,帮帮
classPaintextendsJPanel{J_DrawCircledc=newJ_DrawCircle();intx,y,w,h;publicPaint(){x=I...
class Paint extends JPanel
{
J_DrawCircle dc = new J_DrawCircle();
int x, y, w, h;
public Paint()
{
x = Integer.parseInt(dc.rab[0]);
y = Integer.parseInt(dc.rab[1]);
w = Integer.parseInt(dc.rab[2]);
h = Integer.parseInt(dc.rab[3]);
}
protected void paintComponent(Graphics g)
{
g.drawOval(x, y, w, h);
}
}
public class J_DrawCircle extends JFrame implements ActionListener
{
JPanel p3 = new JPanel();
static String rab[] = new String[4];
JTextField jf[] = { new JTextField(6), new JTextField(6),
new JTextField(6), new JTextField(6) };
public J_DrawCircle()
{
super("画圆");
Container c = getContentPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JButton b = new JButton("确定");
JLabel label[] = { new JLabel("x=", JLabel.CENTER),
new JLabel("y=", JLabel.CENTER),
new JLabel("width=", JLabel.CENTER),
new JLabel("heigth=", JLabel.CENTER) };
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
p1.setLayout(new GridLayout(2, 2));
Dimension d1 = new Dimension(300, 40);
Dimension d2 = new Dimension(300, 40);
Dimension d3 = new Dimension(300, 260);
p1.setPreferredSize(d1);
p2.setPreferredSize(d2);
p3.setPreferredSize(d3);
c.add(p1);
c.add(p2);
c.add(p3);
for (int i = 0; i < 4; i++)
{
p1.add(label[i]);
p1.add(jf[i]);
}
p2.add(b, new FlowLayout());
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
for (int i = 0; i < 4; i++)
rab[i] = jf[i].getText();
p3.setFont(new Font("宋体", Font.PLAIN, 20));//为了是程序调用paintcomponent方法
p3.add(new Paint(), BorderLayout.CENTER);
}
}
主函数被我删掉了因为字数超了。加图片把 展开
{
J_DrawCircle dc = new J_DrawCircle();
int x, y, w, h;
public Paint()
{
x = Integer.parseInt(dc.rab[0]);
y = Integer.parseInt(dc.rab[1]);
w = Integer.parseInt(dc.rab[2]);
h = Integer.parseInt(dc.rab[3]);
}
protected void paintComponent(Graphics g)
{
g.drawOval(x, y, w, h);
}
}
public class J_DrawCircle extends JFrame implements ActionListener
{
JPanel p3 = new JPanel();
static String rab[] = new String[4];
JTextField jf[] = { new JTextField(6), new JTextField(6),
new JTextField(6), new JTextField(6) };
public J_DrawCircle()
{
super("画圆");
Container c = getContentPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JButton b = new JButton("确定");
JLabel label[] = { new JLabel("x=", JLabel.CENTER),
new JLabel("y=", JLabel.CENTER),
new JLabel("width=", JLabel.CENTER),
new JLabel("heigth=", JLabel.CENTER) };
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
p1.setLayout(new GridLayout(2, 2));
Dimension d1 = new Dimension(300, 40);
Dimension d2 = new Dimension(300, 40);
Dimension d3 = new Dimension(300, 260);
p1.setPreferredSize(d1);
p2.setPreferredSize(d2);
p3.setPreferredSize(d3);
c.add(p1);
c.add(p2);
c.add(p3);
for (int i = 0; i < 4; i++)
{
p1.add(label[i]);
p1.add(jf[i]);
}
p2.add(b, new FlowLayout());
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
for (int i = 0; i < 4; i++)
rab[i] = jf[i].getText();
p3.setFont(new Font("宋体", Font.PLAIN, 20));//为了是程序调用paintcomponent方法
p3.add(new Paint(), BorderLayout.CENTER);
}
}
主函数被我删掉了因为字数超了。加图片把 展开
1个回答
2013-11-17
展开全部
使用paint事件,并调用 super.paint(g); 再画自己的图
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询