要求是画一个圆,并能设置圆的颜色,圆受到滑动条的控制,初写程序如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class J_drawOval
{
JFrame app = new JFrame();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(800,200);
Container c = app.getContentPane();
c.setLayout(new FlowLayout()); JSlider s = new JSlider(JSlider.HORIZONTAL,0,50,25);
int diameter = s.getValue();
c.add(s);//添加滑动条 pubilc void paint(Graphics g)
{
g.drawOval(20,20,diameter,diameter);
JButton b1 = new JButton("红色");
JButton b2 = new JButton("绿色");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton b1 = (JButton)e.getSource();
g.setColor(Color.red);
g.fillOval(20,20,diameter,diameter);//设置圆的颜色为红
}
}
);
b2.addActionListerner(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton b2 = (JButton)e.getSource();
g.setColor(Color.green);
g.fillOval(20,20,diameter,diameter);
}
}
);
c.add(g);
c.add(b1);
c.add(b2)
} app.setVisible(true);
} public class J_Circle
{
public static void main(String args[])
{
J_drawOval do = new JdrawOval();
}
} 在构造圆的时候查了些方法但还是有些用不来,程序编译不能通过,可能还是画圆的问题,麻烦大家帮个小忙,谢谢~
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class J_drawOval
{
JFrame app = new JFrame();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(800,200);
Container c = app.getContentPane();
c.setLayout(new FlowLayout()); JSlider s = new JSlider(JSlider.HORIZONTAL,0,50,25);
int diameter = s.getValue();
c.add(s);//添加滑动条 pubilc void paint(Graphics g)
{
g.drawOval(20,20,diameter,diameter);
JButton b1 = new JButton("红色");
JButton b2 = new JButton("绿色");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton b1 = (JButton)e.getSource();
g.setColor(Color.red);
g.fillOval(20,20,diameter,diameter);//设置圆的颜色为红
}
}
);
b2.addActionListerner(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton b2 = (JButton)e.getSource();
g.setColor(Color.green);
g.fillOval(20,20,diameter,diameter);
}
}
);
c.add(g);
c.add(b1);
c.add(b2)
} app.setVisible(true);
} public class J_Circle
{
public static void main(String args[])
{
J_drawOval do = new JdrawOval();
}
} 在构造圆的时候查了些方法但还是有些用不来,程序编译不能通过,可能还是画圆的问题,麻烦大家帮个小忙,谢谢~
public class J_drawOval extends JFrame implements ActionListener{}还是自己改吧!
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;public class J_drawOval extends JFrame implements ActionListener,ChangeListener
{
JFrame app;
Container c;
JSlider s;
int diameter;
JButton b1 = new JButton("红色");
JButton b2 = new JButton("绿色");
Color color = Color.RED;
public J_drawOval()
{
app = this;
c = app.getContentPane();
s = new JSlider(JSlider.HORIZONTAL,0,300,150);
s.addChangeListener(this);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(1024,768);
b1.addActionListener(this);
b2.addActionListener(this);
c.setLayout(new FlowLayout());
c.add(s);//添加滑动条
c.add(b1);
c.add(b2);
app.setVisible(true);
} public void paint(final Graphics g)
{
super.paint(g);
g.clearRect(490, 490, diameter + 100, diameter + 100);
g.setColor(color);
g.fillOval(400, 200, diameter, diameter);
}
public static void main(String args[])
{
new J_drawOval();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(b1))
{
color = Color.RED;
}
else
{
color = Color.GREEN;
}
repaint();
} public void stateChanged(ChangeEvent e)
{
diameter = s.getValue();
repaint();
}
}
接分来了