简体   繁体   中英

Java GUI components

So I've made 3 different classes, one to construct a text field, one for a label, and one for a slider. The problem I'm having is that in the slider listener, if set on 1(out of 3) set the label to " randomInt + randomInt" which I know how to randomize the numbers, it's just altering the text of the label which is made in a different class. here is my main fnctn followed by the two classes:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class driver
{
    public static void main(String[] args)
    {
        colorfulLabel blueLabel = new colorfulLabel("", Color.yellow);
        colorfulTextField redTextField = new colorfulTextField(Color.red,15);
        RatingSlider mathSlide = new RatingSlider(JSlider.HORIZONTAL , 0, 3, 0);

        JFrame frame = new JFrame("Math");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.add(blueLabel);
        panel.add(redTextField);
        panel.add(mathSlide);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

import javax.swing.*;
import java.awt.*;
public class colorfulLabel extends JLabel
{
    //constructor uses one color parameter to respresent bg color
    //creates label using bg color
    //calls parent constructor using super()
    //private Color color;
    public colorfulLabel(String text,Color bg){
        super(text);
        setBackground(bg);
        setOpaque(true);
    }
}

//RatingSlider
import javax.swing.*;
import java.awt.*;
import java.util.Random;
import javax.swing.event.*;
public class RatingSlider extends JSlider
{
    Random generator = new Random();
    private JSlider difficultySlider;
    //accepts horizontal paramater, a min, a max, and starting point for slider
    public RatingSlider(int horiz, int start, int max, int min){
        super(horiz, start, max, min);

        difficultySlider = new JSlider(horiz, start, max, min);
        difficultySlider.setMajorTickSpacing(1);
        difficultySlider.setMinorTickSpacing(0);

        setPaintTicks(true);
        setPaintLabels(true);
        difficultySlider.setAlignmentX(Component.LEFT_ALIGNMENT);

        JPanel Slide = new JPanel();
        BoxLayout layout = new BoxLayout(Slide, BoxLayout.Y_AXIS);
        Slide.setLayout(layout);
        Slide.add(difficultySlider);
        SliderListener listener = new SliderListener();

        difficultySlider.addChangeListener(listener);
    }
    private class SliderListener implements ChangeListener
    {
        public void stateChanged(ChangeEvent event)
        {
            int num1, num2;
            String lblString = "", num_1, num_2;
            if(difficultySlider.getValue() ==1)
            {
                num1 = generator.nextInt();
                num2 = generator.nextInt();
                lblString = (num1 + " + " + num2);
            }

        }
    }

}

Now I just have to set the text of the colorfulLabel to lblString, and I can't figure out how to do that without creating a new object

I think you can just use .setText("") method and it should do it since you have extended the JLabel class. But to be frank you can use Builder design pattern you don't need to extend the any of the class and it should work for you basically.

"Now I just have to set the text of the colorfulLabel to lblString, and I can't figure out how to do that without creating a new object"

What you want to do is pass the ColorfulLabel as reference to RatingSlider . Like this

public class RatingSlider extends JSlider {
    ColorfulLabel cLabel;

    public RatingSlider(int horiz, int start, int max, int min, ColorfulLabel cLabel){
         super(horiz, start, max, min);
         this.cLabel = cLabel;
}

So you'd never be creating a new ColotrfulLabel in the RatingSlider , you just use the same one that you created in the main . Then you can use it in the listener

cLabel.setText(lblString);

Just instantiate the RatingSlider passing the reference of the ColorfulLabel

new RatingSlider(JSlider.HORIZONTAL , 0, 3, 0, blueLabel);

something like:

SwingUtilities.invokeLater
(
  new Runnable
  (
    public void run()
    {
      label.setText(lblText);
    }
  )
);

It needs to be in something like invokeLater to guarantee that it runs in the event dispatch thread. You'll need to get a reference of the label object down to the method that calls this, somehow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM