In: Computer Science
Put a JTextField in the north of the JFrame along with a JLabel that says: Year
Put a JSlider, vertically in the east side of the JFrame
Hints
Have typing in the TextField change the JSlider.
Hints
Add JRadioButtons with labels that are color names
Hints
//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