java在窗口中有一个面板和三个按钮,单击相应的按钮,在画布上绘制相应颜色的填充圆。怎么也显示不出来圆
importjava.applet.Applet;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;...
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestThree {
public static void main(String[] args) {
WindonwPanel win= new WindonwPanel();
win.setBounds(500, 100, 400, 400);
win.setTitle("画板");
}
}
class WindonwPanel extends JFrame implements ActionListener{
MyJPanel jp;
JButton button1,button2,button3;
JPanel p1,p2;
Color co;
public WindonwPanel(){
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init(){
setLayout(new BorderLayout());
jp = new MyJPanel(co);
p2=new JPanel();
button1=new JButton("红色");
button2=new JButton("黄色");
button3=new JButton("绿色");
button1.setBackground(Color.RED);
button2.setBackground(Color.yellow);
button3.setBackground(Color.GREEN);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
p2.add(button1);
p2.add(button2);
p2.add(button3);
add(jp);
add("South",p2);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1){
jp.c=Color.red;
}
else if(e.getSource()==button2){
jp.c=Color.yellow;
}
else if(e.getSource()==button3){
jp.c=Color.GREEN;
}
repaint();
}
class MyJPanel extends JPanel{
Color c;
public MyJPanel(Color c) {
this.c = c ;
}
public void Paint(Graphics g) {
g.setColor(c);
g.fillOval(200, 100, 100, 100);
}
}
} 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestThree {
public static void main(String[] args) {
WindonwPanel win= new WindonwPanel();
win.setBounds(500, 100, 400, 400);
win.setTitle("画板");
}
}
class WindonwPanel extends JFrame implements ActionListener{
MyJPanel jp;
JButton button1,button2,button3;
JPanel p1,p2;
Color co;
public WindonwPanel(){
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init(){
setLayout(new BorderLayout());
jp = new MyJPanel(co);
p2=new JPanel();
button1=new JButton("红色");
button2=new JButton("黄色");
button3=new JButton("绿色");
button1.setBackground(Color.RED);
button2.setBackground(Color.yellow);
button3.setBackground(Color.GREEN);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
p2.add(button1);
p2.add(button2);
p2.add(button3);
add(jp);
add("South",p2);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1){
jp.c=Color.red;
}
else if(e.getSource()==button2){
jp.c=Color.yellow;
}
else if(e.getSource()==button3){
jp.c=Color.GREEN;
}
repaint();
}
class MyJPanel extends JPanel{
Color c;
public MyJPanel(Color c) {
this.c = c ;
}
public void Paint(Graphics g) {
g.setColor(c);
g.fillOval(200, 100, 100, 100);
}
}
} 展开
1个回答
展开全部
//更改设置后记得repait(),类似刷新
public void actionPerformed(ActionEvent e) {
if (e.getSource() == butt1) {
can.huanc(Color.red);
can.repaint();//加上这个
} else if (e.getSource() == butt2) {
can.huanc(Color.green);
can.repaint();//加上这个
} else if (e.getSource() == butt3) {
can.huanc(Color.blue);
can.repaint();//加上这个
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == butt1) {
can.huanc(Color.red);
can.repaint();//加上这个
} else if (e.getSource() == butt2) {
can.huanc(Color.green);
can.repaint();//加上这个
} else if (e.getSource() == butt3) {
can.huanc(Color.blue);
can.repaint();//加上这个
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询