主题:画图问题,多多指教呀!
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.BorderFactory;
public class javademo extends JFrame {
JTabbedPane tb = new JTabbedPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JSlider s = new JSlider();
public javademo() {
this.getContentPane().add(tb, java.awt.BorderLayout.CENTER);
tb.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
if(tb.getSelectedIndex()==1){
if(s.getValue()==0){
paintCircle pc=new paintCircle(Color.white);
pc.setBounds(new Rectangle(6, 10, 280, 230));
pc.setBorder(BorderFactory.createLineBorder(Color.red));
p2.add(pc);
}else{
paintCircle pc=new paintCircle(new Color(s.getValue(),100,100));
pc.setBounds(new Rectangle(6, 10, 280, 230));
pc.setBorder(BorderFactory.createLineBorder(Color.red));
p2.add(pc);
}
}
}
});
tb.add(p1, "p1");
tb.add(p2, "p2");
p1.add(s);
p2.setLayout(null);
s.setMaximum(255);
s.setValue(0);
this.setSize(300,300);
}
public static void main(String[] args) {
javademo jd = new javademo();
jd.setVisible(true);
}
}
class paintCircle extends JPanel{
Color color;
public paintCircle(Color c){
color=c;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(color);
g.fillOval(30,30,180,180);
}
}
这是全部代码.我想实现 JSider变动,带动圆的颜色也变。
但实现不成,只变一次,甚至一次都不变。
请大家多指正。
import java.awt.*;
import javax.swing.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.BorderFactory;
public class javademo extends JFrame {
JTabbedPane tb = new JTabbedPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JSlider s = new JSlider();
public javademo() {
this.getContentPane().add(tb, java.awt.BorderLayout.CENTER);
tb.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
if(tb.getSelectedIndex()==1){
if(s.getValue()==0){
paintCircle pc=new paintCircle(Color.white);
pc.setBounds(new Rectangle(6, 10, 280, 230));
pc.setBorder(BorderFactory.createLineBorder(Color.red));
p2.add(pc);
}else{
paintCircle pc=new paintCircle(new Color(s.getValue(),100,100));
pc.setBounds(new Rectangle(6, 10, 280, 230));
pc.setBorder(BorderFactory.createLineBorder(Color.red));
p2.add(pc);
}
}
}
});
tb.add(p1, "p1");
tb.add(p2, "p2");
p1.add(s);
p2.setLayout(null);
s.setMaximum(255);
s.setValue(0);
this.setSize(300,300);
}
public static void main(String[] args) {
javademo jd = new javademo();
jd.setVisible(true);
}
}
class paintCircle extends JPanel{
Color color;
public paintCircle(Color c){
color=c;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(color);
g.fillOval(30,30,180,180);
}
}
这是全部代码.我想实现 JSider变动,带动圆的颜色也变。
但实现不成,只变一次,甚至一次都不变。
请大家多指正。