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
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.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);
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
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);
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()); }
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
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??
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);
//**************************************************
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());
}
}
why we cant use this. in frame. ?
Finally someone who really explains well this
The best coding courses ever
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
BEST JAVA SERIES ON YT
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
I have enjoyed doing the good example about Java Slider. Thanks for that from Chile
Thank you very much for sharing this video, easy to understand.
Great channel, helped me with my grade 12 summative the whole way through!
Nice work
Thanks very useful tutorial
Nice work thanks
Nice video. Thank you
I have subscribed to your channel. You are amazing. I like the way u introduce yourself 🤩
It's ya bro!
Lovely
nice ☺, for to write this " ° " tap "Alt + 248" , your explanation is the best thank you BRO
Keep it up😁
Your Guru bro
early mornings with Bro just keep getting better xx
Another great lesson!
thank you for the content
i can't wait for the waterslides this summer
its awesome that i can just get ma bro to help me hue hue
The degree sumbol Alt + 0176. By the way, keep going with making a videos! Thank you, bro! And LIKE, comment and subscribe are done!
I also do that with: Shift + | (The key to the left of the 1)
I love you😍
Thanks
Thank you very much
thanks bro
👍
king
breaking more utube algorithmsssssssssssssss
I'm wondering why my label is not underneath the slider but on the right side, can someone help me please?
thank you
thanks
😊
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());
}
}
}
You forgot: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//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());
}
});
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
@@BroCodez thanks!
thnx bruh
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??
change the left to LEFT and Top to TOP cuz it shld be capitalized
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
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.setPaintTicks(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.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());
}
}
how to increase and decrease the Value by taking input from the User?
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);
How about this. If you have any suggestions just tell me.
It's a great video , I like this . But , I don't know when we should use panel , label . I usually think frame can add everything we want :(
I'm the 777th person to like :D
My value is not changing
RUclips ALGORYTH
this comment is to help you, help ME
noise
the next comment
My slider shows the progress of read pages off a book:
public class Main {
public static void main(String[] args) {
MySlider mySlider = new MySlider();
}
}
//************************************************
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
public class MySlider implements ChangeListener {
JFrame frame;
JPanel panel;
JLabel label;
JSlider slider;
MySlider() {
frame = new JFrame("Page Counter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel("Page #: 0");
frame.pack();
panel = new JPanel();
slider = new JSlider(0, 500, 0);
slider.setPreferredSize(new Dimension(500, 50));
slider.setPaintTicks(true);
slider.setMinorTickSpacing(10);
slider.setPaintLabels(true);
slider.setFont(new Font("MV Boli", Font.PLAIN, 20));
slider.setOrientation(SwingConstants.HORIZONTAL);
slider.addChangeListener(this);
panel.add(slider);
panel.add(label);
frame.add(panel);
frame.setSize(700, 80);
frame.setVisible(true);
}
@Override
public void stateChanged(ChangeEvent e) {
int pageNumber = slider.getValue();
label.setText("Page # = " + pageNumber);
}
}
Thank you, Bro!
thank you