Java slider 🌡️

Поделиться
HTML-код
  • Опубликовано: 27 ноя 2024

Комментарии • 65

  • @BroCodez
    @BroCodez  4 года назад +30

    //**************************************************
    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());

    }
    }

    • @JoaoPedroPF18
      @JoaoPedroPF18 2 года назад

      why we cant use this. in frame. ?

  • @milton5682
    @milton5682 3 года назад +6

    Finally someone who really explains well this

  • @hamlek6923
    @hamlek6923 2 года назад +2

    The best coding courses ever

  • @somerandomuser8005
    @somerandomuser8005 2 года назад +3

    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

  • @FrizzleTheDizzle
    @FrizzleTheDizzle 4 года назад +4

    BEST JAVA SERIES ON YT

  • @pavelkvasnicka6856
    @pavelkvasnicka6856 Год назад +2

    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

  • @pgorellana
    @pgorellana 3 дня назад

    I have enjoyed doing the good example about Java Slider. Thanks for that from Chile

  • @chchong68
    @chchong68 2 года назад +1

    Thank you very much for sharing this video, easy to understand.

  • @colonelcanada-9500
    @colonelcanada-9500 2 года назад +1

    Great channel, helped me with my grade 12 summative the whole way through!

  • @babushaikh6582
    @babushaikh6582 4 года назад +1

    Nice work

  • @rayyanasia4349
    @rayyanasia4349 3 года назад

    Thanks very useful tutorial

  • @sabermmirza
    @sabermmirza 4 года назад

    Nice work thanks

  • @speklex779
    @speklex779 Год назад

    Nice video. Thank you

  • @pratikshahegde8554
    @pratikshahegde8554 4 года назад +3

    I have subscribed to your channel. You are amazing. I like the way u introduce yourself 🤩

  • @kemann3815
    @kemann3815 3 года назад

    Lovely

  • @irobot3868
    @irobot3868 2 года назад

    nice ☺, for to write this " ° " tap "Alt + 248" , your explanation is the best thank you BRO

  • @VAWAV
    @VAWAV 4 года назад

    Keep it up😁

  • @yusuphmwaigomole3104
    @yusuphmwaigomole3104 2 года назад

    Your Guru bro

  • @kingkock1
    @kingkock1 2 года назад

    early mornings with Bro just keep getting better xx

  • @fieldsfury
    @fieldsfury 2 года назад

    Another great lesson!

  • @henriquefelinimena3693
    @henriquefelinimena3693 3 года назад

    thank you for the content

  • @greeneggsandmushrooms9855
    @greeneggsandmushrooms9855 3 года назад

    i can't wait for the waterslides this summer

  • @FrizzleTheDizzle
    @FrizzleTheDizzle 4 года назад +1

    its awesome that i can just get ma bro to help me hue hue

  • @user-alexkristal
    @user-alexkristal 3 года назад +5

    The degree sumbol Alt + 0176. By the way, keep going with making a videos! Thank you, bro! And LIKE, comment and subscribe are done!

    • @milton5682
      @milton5682 3 года назад

      I also do that with: Shift + | (The key to the left of the 1)

  • @VAWAV
    @VAWAV 4 года назад +1

    I love you😍

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 2 года назад

    Thanks

  • @majid1885
    @majid1885 3 года назад

    Thank you very much

  • @ibrahimylmaz8378
    @ibrahimylmaz8378 2 года назад

    thanks bro

  • @sivanaintharaj2498
    @sivanaintharaj2498 Год назад

    👍

  • @MmdRsh
    @MmdRsh Год назад

    king

  • @giahuypham1857
    @giahuypham1857 10 месяцев назад

    breaking more utube algorithmsssssssssssssss

  • @lequangnghi194
    @lequangnghi194 3 года назад +1

    I'm wondering why my label is not underneath the slider but on the right side, can someone help me please?

  • @nawfalnjm5699
    @nawfalnjm5699 3 года назад

    thank you

  • @nehalayaaz9406
    @nehalayaaz9406 4 года назад

    thanks
    😊

  • @matheustavares4597
    @matheustavares4597 3 года назад +1

    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());
    }

    }

    }

  • @Soyosan22
    @Soyosan22 2 года назад +2

    You forgot: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  • @angelcastineira2561
    @angelcastineira2561 4 года назад +1

    //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());

    }
    });

    • @BroCodez
      @BroCodez  4 года назад +2

      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

    • @angelcastineira2561
      @angelcastineira2561 4 года назад +1

      @@BroCodez thanks!

  • @uykerrykaberga
    @uykerrykaberga 2 года назад

    thnx bruh

  • @michalj121
    @michalj121 2 года назад +1

    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??

    • @sehejkumar9984
      @sehejkumar9984 2 года назад

      change the left to LEFT and Top to TOP cuz it shld be capitalized

  • @Monsta1291
    @Monsta1291 2 года назад

    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());

    }


    }

  • @arpitchauhan5212
    @arpitchauhan5212 2 года назад

    how to increase and decrease the Value by taking input from the User?

    • @jakubmrazek001
      @jakubmrazek001 2 года назад +1

      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);

  • @akshaykumarsharma
    @akshaykumarsharma 4 года назад

    How about this. If you have any suggestions just tell me.

  • @tingping7684
    @tingping7684 3 года назад

    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 :(

  • @dogukan-u2n
    @dogukan-u2n Год назад

    I'm the 777th person to like :D

  • @karippaitech8963
    @karippaitech8963 Год назад

    My value is not changing

  • @Christopher-krd
    @Christopher-krd Год назад

    RUclips ALGORYTH

  • @tanishq_xin
    @tanishq_xin 3 года назад +1

    this comment is to help you, help ME

  • @eugenezuev7349
    @eugenezuev7349 6 месяцев назад

    noise

  • @zari_723
    @zari_723 2 года назад

    the next comment

  • @CheerfulBackpacker-hp1kn
    @CheerfulBackpacker-hp1kn 6 месяцев назад

    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!

  • @fillaliziad2702
    @fillaliziad2702 3 года назад

    thank you