简体   繁体   English

Java GUI组件

[英]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. 所以我创建了3个不同的类,一个用于构造文本字段,一个用于标签,一个用于滑块。 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. 我遇到的问题是,在滑块监听器中,如果设置为1(总共3个),则将标签设置为“randomInt + randomInt”,我知道如何随机化数字,它只是改变标签的文本,这是在不同的班级。 here is my main fnctn followed by the two classes: 这是我的主要fnctn,然后是两个类:

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 现在我只需将colorfulLabel的文本设置为lblString,我无法弄清楚如何在不创建新对象的情况下执行此操作

I think you can just use .setText("") method and it should do it since you have extended the JLabel class. 我认为你可以使用.setText(“”)方法,它应该这样做,因为你扩展了JLabel类。 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. 但坦率地说,你可以使用Builder设计模式,你不需要扩展任何类,它应该适合你。

"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" “现在我只需要将colorfulLabel的文本设置为lblString,如果不创建新对象,我无法弄清楚如何做到这一点”

What you want to do is pass the ColorfulLabel as reference to RatingSlider . 你想要做的是传递ColorfulLabel作为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 . 所以你永远不会在new ColotrfulLabel中创建一个new ColotrfulLabelRatingSlider ,你只需使用你在main创建的同一个。 Then you can use it in the listener 然后你可以在监听器中使用它

cLabel.setText(lblString);

Just instantiate the RatingSlider passing the reference of the ColorfulLabel 只需实例化通过ColorfulLabel引用的RatingSlider

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. 它需要像invokeLater一样,以保证它在事件派发线程中运行。 You'll need to get a reference of the label object down to the method that calls this, somehow. 您需要以某种方式获取标签对象的引用,直到调用它的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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