JSlider program

+1 Akshay Pimpalkar · November 18, 2015
Though i haven't get any error in the program. I can't find the default small circle(oval) on my orange panel. I don't know why this happened. please someone help me!!

import java.awt.*;
import javax.swing.*;

public class apples extends JPanel{

private int d = 10;

public void painComponent(Graphics g){
super.paintComponent(g);
g.fillOval(10, 10, d, d);
}

public void setD(int newD){
d = (newD >= 0 ? newD : 10);
repaint();
}

public Dimension getPreferredSize(){
return new Dimension(200,200);
}

public Dimension getMinimumSize(){
return getPreferredSize();
}

}




import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class TheWindow extends JFrame{

private JSlider slider;
private apples MyPanel;

public TheWindow(){
super("The title");
MyPanel = new apples();
MyPanel.setBackground(Color.ORANGE);

slider = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);

slider.addChangeListener(
new ChangeListener(){
public void stateChanged(ChangeEvent e){
MyPanel.setD(slider.getValue());
}
}

);
add(slider, BorderLayout.SOUTH);
add(MyPanel, BorderLayout.CENTER);

}

}



import javax.swing.*;

public class akshay {

public static void main(String[] args) {

TheWindow w = new TheWindow();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setSize(230, 280);
w.setVisible(true);

}

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Akshay Pimpalkar · November 19, 2015
oh ! i got it.... i mistakenly spelled 'paintComponent' as 'painComponent' in 'apples' class...even though java compiler didn't give any syntax error.:D i have corrected it now and the program runs as expected. :)
  • 1

Java / Android Development

106,956 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator