Question

In: Computer Science

Put a JTextField in the north of the JFrame along with a JLabel that says: Year...

  1. Put a JTextField in the north of the JFrame along with a JLabel that says: Year

  2. Put a JSlider, vertically in the east side of the JFrame

    1. Have the JSlider range from 1990 to 2019
    2. Have the JSlider labeled Choose Year
    3. Add a ChangeListener to the JSlider
    4. Have the JSlider's value displayed in the TextField

    Hints

    1. to get the JSlider to be vertical, make the JSlider's orientation JSlider.VERTICAL.
    2. label the JSlider
      1. tell the JSlider to set the tick spacing:setMajorTickSpacing(4), and setMinorTickSpacing(2)
      2. tell the JSlider to make the tick marks show up: setPaintTicks(true)
      3. tell the JSlider to make the labels show up use setPaintLabels(true)
    3. for the changeListener to work, the class has to implement the ChangeListener interface. If the class implements more than one interface, they should be noted in a comma seperated list.
    4. the changeListener has to have a stateChanged method which takes a ChangeEvent object as an argument.
    5. The int returned by the slider can be converted to a string by concatenating it with the empty String as in:  "" + value
  3. Have typing in the TextField change the JSlider.

    Hints

    1. The value from the TextField is obtained as a String. To get an int use Integer.parseInt()
  4. Add JRadioButtons with labels that are color names

    1. Group the JRadioButtons into a ButtonGroup
    2. Hook your JRadioButtons to your program (use addActionListener()) so that the JFrame's background color is changed to the color chosen with the JRadioButton

    Hints

    1. The ButtonGroup just coordinates the buttons so, when one is pressed, the others turn off.
    2. Use setBackground() to set the JFrame's color. If setting the JFrame's color doesn't work for you, try setting the background color of each of the JPanels you have.

Solutions

Expert Solution

//Java code

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame {

    private JSlider slider;
    private JTextField textField;
    private JLabel  label;
    private JRadioButton rdbRed,rdbYellow, rdbBlue;
    private  ButtonGroup buttonGroup;
    public Main()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setSize(300,500);
        setTitle("JSlider");
        setVisible(true);
        //text field
        textField = new JTextField(10);
            //Label
        label = new JLabel("Choose Year: ");
        JPanel panel = new JPanel(new BorderLayout());
        panel.setOpaque(true);
        panel.add(label,BorderLayout.NORTH);
        panel.add(textField,BorderLayout.SOUTH);
        add(panel,BorderLayout.NORTH);
        //Radio Buttons
        rdbRed = new JRadioButton("RED");
        rdbBlue = new JRadioButton("BLUE");
        rdbYellow = new JRadioButton("YELLOW");

        rdbRed.setOpaque(false);
        rdbBlue.setOpaque(false);
        rdbYellow.setOpaque(false);
        //add to group
        buttonGroup = new ButtonGroup();
        buttonGroup.add(rdbBlue);
        buttonGroup.add(rdbRed);
        buttonGroup.add(rdbYellow);

        //Add to panel
        JPanel panel1 = new JPanel(new BorderLayout());
        panel1.setOpaque(false);
        panel1.add(rdbBlue,BorderLayout.NORTH);
        panel1.add(rdbRed,BorderLayout.CENTER);
        panel1.add(rdbYellow,BorderLayout.SOUTH);
        //add panel1 to frame
        add(panel1, BorderLayout.CENTER);

        //action listener
        rdbRed.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                getContentPane().setBackground(Color.red);
            }
        });

        rdbYellow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                getContentPane().setBackground(Color.YELLOW);
            }
        });
        rdbBlue.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            getContentPane().setBackground(Color.BLUE);
        }
    });
        //slider
        slider = new JSlider();
        slider.setMinimum(1990);
        slider.setMaximum(2019);
        slider.setOrientation(JSlider.VERTICAL);
        slider.setMajorTickSpacing(4);
        slider.setMinorTickSpacing(2);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        add(slider,BorderLayout.EAST);
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                int value  = slider.getValue();
                textField.setText(String.valueOf(value));
            }
        });
    }
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Main();
            }
        });
    }

}

//output

//If you need any help regarding this solution ............. please leave a comment ....... thanks


Related Solutions

On October 23, 2019 Bill’s warehouse, located along the Dallas North Tollway, was damaged by a...
On October 23, 2019 Bill’s warehouse, located along the Dallas North Tollway, was damaged by a tornado. Bill’s tax basis in this warehouse at the time of the tornado was $800,000. Bill received insurance proceeds of $1,700,000. Complete the following: If Bill wants to just keep the money, what will he report in 2019? If Bill wants to replace the warehouse, and he expects that it will cost $1,900,000 to do so: Does Bill need to report any gain in...
Suppose that potholes along the motorway north of Las Angles have been found to occur according...
Suppose that potholes along the motorway north of Las Angles have been found to occur according to a Poisson process with rate parameter λ. (a) Out of 46 three kilometer stretches of motorway, the average number of potholes was found to be 6.2. Find the maximum likelihood estimate for the rate parameter, λˆ MLE, and the standard deviation of the estimate, SD(λˆ MLE). (b) Using the maximum likelihood estimate as the rate parameter, what is the probability that a randomly...
Many regions along the coast in North and South Carolina and Georgia have experienced rapid population...
Many regions along the coast in North and South Carolina and Georgia have experienced rapid population growth over the last 10 years. It is expected that the growth will continue over the next 10 years. This has motivated many of the large grocery store chains to build new stores in the region. The Kelley’s Super Grocery Stores Inc. chain is no exception. The director of planning for Kelley's Super Grocery Stores wants to study adding more stores in this region....
A pair of radio transmitters separated by 118m along a north-south line each transmit an in-phase...
A pair of radio transmitters separated by 118m along a north-south line each transmit an in-phase 5.55MHz wave. Using small angle approximations, the smallest angle (with respect to the easterly direction from the pair of transmitters) of destructive interference is closest to A.16° B.19° C.6.4° D.9.7° E.13°
Many regions along the coast in North and South Carolina and Georgia have experienced rapid population...
Many regions along the coast in North and South Carolina and Georgia have experienced rapid population growth over the last 10 years. It is expected that the growth will continue over the next 10 years. The director gathered the following sample information. Family Food Income Size 1 $3.84 $73.98 1 2 4.08 54.90 2 3 5.76 53.20 4 4 3.48 52.02 1 5 4.20 65.70 2 6 4.80 53.64 4 7 4.32 79.74 3 8 5.04 68.58 4 9 6.12...
After doing a chest x-ray, your doctor says the results are inconclusive. So, you are put...
After doing a chest x-ray, your doctor says the results are inconclusive. So, you are put on a six-month course of isoniazid to be safe. About six months later, while sitting in your medical microbiology class during a lecture on tuberculosis, you suddenly realize why you had that positive reaction to the skin test six months earlier. It had nothing to do with being infected, but was because you were born in Norway and your family moved to the United...
After doing a chest x-ray, your doctor says the results are inconclusive. So, you are put...
After doing a chest x-ray, your doctor says the results are inconclusive. So, you are put on a six-month course of isoniazid to be safe. About six months later, while sitting in your medical microbiology class during a lecture on tuberculosis, you suddenly realize why you had that positive reaction to the skin test six months earlier. It had nothing to do with being infected, but was because you were born in Norway and your family moved to the United...
Exercise 14-26 (LO14-1, LO14-2, LO14-4) Many regions along the coast in North and South Carolina and...
Exercise 14-26 (LO14-1, LO14-2, LO14-4) Many regions along the coast in North and South Carolina and Georgia have experienced rapid population growth over the last 10 years. It is expected that the growth will continue over the next 10 years. This has motivated many of the large grocery store chains to build new stores in the region. The Kelley’s Super Grocery Stores Inc. chain is no exception. The director of planning for Kelley's Super Grocery Stores wants to study adding...
PLEASE ANSWER 5-8 The​ Coca-Cola Company​ (ticker: KO), along with its North American anchor bottler​ Coca-Cola...
PLEASE ANSWER 5-8 The​ Coca-Cola Company​ (ticker: KO), along with its North American anchor bottler​ Coca-Cola Refreshments, is considering launching a new product. This new​ product, Coca-Cola​ YOU!, will allow customers to customize the flavor of their soda at the point of sale. This project requires an initial investment at​ t=0 of​$350 million. KO expects that cash flows in the first year of the project will be​ $20 million, growing to​ $30 million in year​ 2, and growing by​ 3%...
Listed below is a company’s sales in Year 1 through Year 12 along with the national...
Listed below is a company’s sales in Year 1 through Year 12 along with the national income of the country, where the business is set up. Year National Income (in millions of dollars) x Company's sales (in thousands of dollars) y Year 1 305 470 Year 2 316 485 Year 3 358 499 Year 4 350 515 Year 5 375 532 Year 6 392 532 Year 7 400 556 Year 8 398 576 Year 9 430 583 Year 10 456...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT