-1

I tried searching for some sort of interactive JLabel. I want it to look like this:

I'm not sure what this is called or where I could find info on displaying this. I want it to print a refreshed number when the +/- is pressed. I have it working to print on the eclipse console but am unsure how to get it to print to the JFrame.

Here is some of the code:

String input = JOptionPane.showInputDialog("Please enter the number of laps.");

    numLaps = Integer.parseInt(input);

    //frame creation
    JFrame f = new JFrame("Number of Laps");
    f.setSize(550, 450);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(null);

    // label creation
    JLabel label = new JLabel("You entered " + numLaps + " laps. Press + to add a lap. Press - to subtract a lap.", SwingConstants.CENTER);
    label.setBounds(0,0,500,300);
    f.add(label);
    //display window

    f.setVisible(true);


    //button creation add
    JButton add = new JButton ("+");
    add.setBounds(350,250,50,50);
    add.setPreferredSize(new Dimension(50,50));
    add.addActionListener( new ActionListener()
    {

        @Override
        public void actionPerformed(ActionEvent e) {
            // what happens when button is pressed
            //numLaps++;
            addToLap();
            System.out.println(numLaps);
        }

    });
    f.add(add);

  //button creation subtract
        JButton sub = new JButton ("-");
        sub.setBounds(100,250,50,50);
        sub.setPreferredSize(new Dimension(50,50));

          sub.addActionListener( new ActionListener()
          {

            @Override
            public void actionPerformed(ActionEvent e) {
                // what happens when button is pressed
                //numLaps--;
                subToLap();
                System.out.println(numLaps);
            }

          });
          f.add(sub);

& the add/sub methods:

private static void addToLap() {
    // TODO Auto-generated method stub
    numLaps++;
}
private static void subToLap() {
    // TODO Auto-generated method stub
    numLaps--;
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Your question is a "here are some requirements and here is some code" type question, but there's no actual coherent specific question in your post. Please fix this. And also consider calling `setText(...)` on your JLabel when you want to change the text it displays. Please go through the [tour], the [help] and the [how to ask a good question](http://stackoverflow.com/help/how-to-ask) sections to see how this site works and to help you improve your current and future questions, thereby getting better answers. – Hovercraft Full Of Eels Dec 06 '14 at 04:16
  • Thank you for the response. I am asking what methods I can use to make a counter between the two buttons might be. I basically am trying to live print to that location each time the +/- is pressed. Does that help? Like I said, I do not know what this method would be called-- so I'm looking for suggestions. Thanks again. – proHamming Dec 06 '14 at 04:20
  • I'd use (and as a user, prefer to see) a `JSpinner` with a `SpinnerNumberModel` instead.. – Andrew Thompson Dec 06 '14 at 04:26
  • 1
    `f.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Dec 06 '14 at 04:27
  • I have already told you the method in my comment -- it's called `setText(...)`. – Hovercraft Full Of Eels Dec 06 '14 at 04:28
  • *"Does that help?"* Congrats on demonstrating that you know what a question is! Now can you add a specific question to the ..question? – Andrew Thompson Dec 06 '14 at 04:32
  • Yeesh, tough crowd. No need to be rude. – proHamming Dec 06 '14 at 15:00

1 Answers1

0

You could try the following:

private int numLaps = 0 // Or the number you want initialize with

Then in your methods should assing the numLaps value to the JLabel like this:

this.myLabel.setText(String.valueOf(numLaps));

I hope it helps.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Neniel
  • 163
  • 16
  • So using this I was able to add the label, but it does not change each time I press the add/subtract button. That is my goal here. Any suggestions? – proHamming Dec 07 '14 at 18:40
  • Can you post your current addToLap() and subToLap() methods, please? – Neniel Dec 08 '14 at 01:01