//************************************************** public class Main { public static void main(String[] args) { // JSlider = GUI component that lets user enter a value // by using an adjustable sliding knob on a track SliderDemo sliderDemo = new SliderDemo(); } } //************************************************** import java.awt.*; import javax.swing.*; import javax.swing.event.*; public class SliderDemo implements ChangeListener{ JFrame frame; JPanel panel; JLabel label; JSlider slider; SliderDemo(){ frame = new JFrame("Slider Demo"); panel = new JPanel(); label = new JLabel(); slider = new JSlider(0,100,50); slider.setPreferredSize(new Dimension(400,200)); slider.setPaintTicks(true); slider.setMinorTickSpacing(10); slider.setPaintTrack(true); slider.setMajorTickSpacing(25); slider.setPaintLabels(true); slider.setFont(new Font("MV Boli",Font.PLAIN,15)); label.setFont(new Font("MV Boli",Font.PLAIN,25)); //slider.setOrientation(SwingConstants.HORIZONTAL); slider.setOrientation(SwingConstants.VERTICAL); label.setText("°C = "+ slider.getValue()); slider.addChangeListener(this); panel.add(slider); panel.add(label); frame.add(panel); frame.setSize(420,420); frame.setVisible(true); } @Override public void stateChanged(ChangeEvent e) { label.setText("°C = "+ slider.getValue()); } }
@JoaoPedroPF182 жыл бұрын
why we cant use this. in frame. ?
@milton56823 жыл бұрын
Finally someone who really explains well this
@pgorellana25 күн бұрын
I have enjoyed doing the good example about Java Slider. Thanks for that from Chile
@FrizzleTheDizzle4 жыл бұрын
BEST JAVA SERIES ON YT
@pavelkvasnicka6856 Жыл бұрын
This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro
@hamlek69233 жыл бұрын
The best coding courses ever
@somerandomuser80052 жыл бұрын
Thanks for everything so far! Definitely will continue watching. This is like a game using the slider and a couple labels. (main method just has to have "new GetThere();" asides from the pre-generated stuff) import java.awt.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class GetThere implements ChangeListener{ //Game variables Random r = new Random(); //Edit Here int min = 0; // any number int max = 100; // must be greater than min int goal = r.nextInt(max-min)+min; int points; //GUI JFrame frame; JPanel panel; JLabel current; JLabel point; JLabel goalLabel; JSlider slider; GetThere(){ frame = new JFrame("Slider Demonstration"); panel = new JPanel(); current = new JLabel(); point = new JLabel(); goalLabel = new JLabel(); slider = new JSlider(min,max,(max+min)/2); // Low , High, Start slider.setPreferredSize(new Dimension(1000,200)); //size of the slider in the frame slider.setPaintTicks(true); // See ticks on the track slider.setPaintTrack(true); // See the track slider.setPaintLabels(true); // See number labels slider.setBackground(Color.black); panel.setBackground(Color.black); //Edit based on slider size slider.setMinorTickSpacing(5); slider.setMajorTickSpacing(25); //Fonts of ... slider.setFont(new Font("MV Boli",Font.BOLD,15)); //Slider numbers slider.setForeground(new Color(25,155,25)); // Text Color current.setFont(new Font("MV Boli",Font.BOLD,15)); //Current current.setForeground(new Color(25,155,25)); point.setFont(new Font("MV Boli",Font.BOLD,18)); //Point point.setForeground(new Color(35,175,35)); goalLabel.setFont(new Font("MV Boli",Font.BOLD,15)); //Goal goalLabel.setForeground(new Color(25,155,25)); //Default Texts current.setText("Current Pos: "+ slider.getValue()); point.setText("| Points: "+ points +" |"); goalLabel.setText("Goal: "+ goal); //slider.setOrientation(SwingConstants.VERTICAL); // Default is horizontal slider.addChangeListener(this); // when you move the thing /* * Only way I could keep things from shaking and having the labels under the slider * and all that stuff (high x-distance between things in FlowLayout). * A better way will prob show up */ panel.setLayout(new FlowLayout(FlowLayout.CENTER,1000,10)); //Add things to the panel panel.add(slider); panel.add(current); panel.add(point); panel.add(goalLabel); //Add things to the FRAME frame.add(panel); //Frame stuff frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(1250,750); //So frame isn't smol frame.setVisible(true); } @Override public void stateChanged(ChangeEvent e) { // TODO Auto-generated method stub //Format: Current Pos: 69 | Points: 1 | Goal: 420 //Updates Text current.setText("Current Pos: "+ slider.getValue()); point.setText(" | Points: "+ points +" | "); goalLabel.setText("Goal: "+ goal); if(slider.getValue() == goal) { goal = r.nextInt(max-min)+min; points ++; } } } //edit was for rearranging sentences
@chchong683 жыл бұрын
Thank you very much for sharing this video, easy to understand.
@colonelcanada-95002 жыл бұрын
Great channel, helped me with my grade 12 summative the whole way through!
@pratikshahegde85544 жыл бұрын
I have subscribed to your channel. You are amazing. I like the way u introduce yourself 🤩
@BroCodez4 жыл бұрын
It's ya bro!
@speklex779 Жыл бұрын
Nice video. Thank you
@rayyanasia43493 жыл бұрын
Thanks very useful tutorial
@irobot38682 жыл бұрын
nice ☺, for to write this " ° " tap "Alt + 248" , your explanation is the best thank you BRO
@babushaikh65824 жыл бұрын
Nice work
@kingkock12 жыл бұрын
early mornings with Bro just keep getting better xx
@yusuphmwaigomole31042 жыл бұрын
Your Guru bro
@sabermmirza4 жыл бұрын
Nice work thanks
@fieldsfury2 жыл бұрын
Another great lesson!
@kemann38153 жыл бұрын
Lovely
@FrizzleTheDizzle4 жыл бұрын
its awesome that i can just get ma bro to help me hue hue
@henriquefelinimena36933 жыл бұрын
thank you for the content
@VAWAV4 жыл бұрын
Keep it up😁
@user-alexkristal3 жыл бұрын
The degree sumbol Alt + 0176. By the way, keep going with making a videos! Thank you, bro! And LIKE, comment and subscribe are done!
@milton56823 жыл бұрын
I also do that with: Shift + | (The key to the left of the 1)
@greeneggsandmushrooms98553 жыл бұрын
i can't wait for the waterslides this summer
@VAWAV4 жыл бұрын
I love you😍
@MrLoser-ks2xn2 жыл бұрын
Thanks
@majid18853 жыл бұрын
Thank you very much
@ibrahimylmaz83782 жыл бұрын
thanks bro
@angelcastineira25614 жыл бұрын
//is there a significant difference between this code: slider.addChangeListener(this); @Override public void stateChanged(ChangeEvent e) { label.setText("°C = "+ slider.getValue()); } // and this one?: slider.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { label.setText("°C = "+ slider.getValue()); } });
@BroCodez4 жыл бұрын
I believe both would be fine. They perform the same task. The second one might be better for a larger program, because you're using an anonymous inner class. It's more efficient. I have a video on inner classes coming up in this playlist
@angelcastineira25614 жыл бұрын
@@BroCodez thanks!
@lequangnghi1943 жыл бұрын
I'm wondering why my label is not underneath the slider but on the right side, can someone help me please?
@Arthur-g5n8q8 күн бұрын
Why use the stateChanged rather than the actionListener?
@giahuypham185711 ай бұрын
breaking more utube algorithmsssssssssssssss
@MmdRsh Жыл бұрын
king
@uykerrykaberga2 жыл бұрын
thnx bruh
@Soyosan222 жыл бұрын
You forgot: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@nehalayaaz94064 жыл бұрын
thanks 😊
@nawfalnjm56993 жыл бұрын
thank you
@matheustavares45973 жыл бұрын
I rewrote the code i realized i don't need a panel to use JSlider, why did you use panel in this code? example: import javax.swing.JFrame; import javax.swing.JSlider; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; public class Main extends JFrame { JButton b; JPanel panel; JSlider slider; Main(){ b = new JButton(); b.setText("Click here"); b.addActionListener(new event()); slider = new JSlider(0,100); slider.setPaintTicks(true); slider.setMinorTickSpacing(10); slider.setPaintTrack(true); slider.setMajorTickSpacing(25); slider.setPaintLabels(true);// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(new FlowLayout()); this.add(b); this.add(slider); this.pack(); this.setVisible(true); } public static void main(String[] args){ new Main(); } class event implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.out.println("You have manipulated the temperature to " + slider.getValue()); } } }
@michalj1212 жыл бұрын
Hi! label.setText(“C “+slider.getValue()); label.setVerticalTextPosition(JLabel.Top); label.setHorizontalTextPosition(JLabel.Left); Whatever position I set (Left,Right,Top etc..), the text doesn’t move after restart the program. Why??
@sehejkumar99842 жыл бұрын
change the left to LEFT and Top to TOP cuz it shld be capitalized
@arpitchauhan52122 жыл бұрын
how to increase and decrease the Value by taking input from the User?
@jakubmrazek0012 жыл бұрын
You can do it with a scanner + method setValue(int value) Scanner scanner = new Scanner(System.in); JSlider slider = new JSlider(); int value = scanner.nextInt(); slider.setValue(value);