Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ex.2-4 num3:Change the ButtonComboBoxDemo demo program as follows. -Clear the text, modify the Icon to show three or four self-portraits and a description of the

ex.2-4 num3:Change the ButtonComboBoxDemo demo program as follows. -Clear the text, modify the Icon to show three or four self-portraits and a description of the picture along with the photo.

import java.awt.*; import java.awt.event.*; import java.net.URL; import javax.swing.*;

@SuppressWarnings("serial") public class ButtonComboBoxDemo extends JFrame { private JLabel lblForTest; // private String imgFilename = "Test/matthead.png"; private String lblText = "Matthead"; private Icon iconMatthead; private JRadioButton rbLeft, rbCenter, rbRight, rbTop, rbMiddle, rbBottom; private JCheckBox cbText, cbIcon; private JComboBox comboColor;

public ButtonComboBoxDemo() { // URL imgURL = getClass().getClassLoader().getResource(imgFilename); // if (imgURL != null) { // iconMatthead = new ImageIcon(imgURL); // } else { // System.err.println("Couldn't find file: " + imgCrossFilename); // } iconMatthead = new ImageIcon("matthead.png"); lblForTest = new JLabel(lblText, iconMatthead, SwingConstants.CENTER); lblForTest.setOpaque(true); lblForTest.setBackground(new Color(204, 238, 241)); lblForTest.setForeground(Color.RED); lblForTest.setFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18)); rbLeft = new JRadioButton("Left"); rbLeft.setMnemonic(KeyEvent.VK_L); rbCenter = new JRadioButton("Center", true); rbCenter.setMnemonic(KeyEvent.VK_C); rbRight = new JRadioButton("Right"); rbRight.setMnemonic(KeyEvent.VK_R);

ButtonGroup btnGroupH = new ButtonGroup(); btnGroupH.add(rbLeft); btnGroupH.add(rbRight); btnGroupH.add(rbCenter); JPanel pnlRbtnH = new JPanel(new GridLayout(1, 0)); pnlRbtnH.add(rbLeft); pnlRbtnH.add(rbCenter); pnlRbtnH.add(rbRight); pnlRbtnH.setBorder(BorderFactory.createTitledBorder("Horizontal Alignment")); ItemListener listener = new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { if (e.getSource() == rbLeft) { lblForTest.setHorizontalAlignment(SwingConstants.LEFT); } else if (e.getSource() == rbCenter) { lblForTest.setHorizontalAlignment(SwingConstants.CENTER); } else if (e.getSource() == rbRight) { lblForTest.setHorizontalAlignment(SwingConstants.RIGHT); } } } };

rbLeft.addItemListener(listener); rbCenter.addItemListener(listener); rbRight.addItemListener(listener); rbTop = new JRadioButton("Top"); rbTop.setMnemonic(KeyEvent.VK_T); rbMiddle = new JRadioButton("Middle", true); rbMiddle.setMnemonic(KeyEvent.VK_M); rbBottom = new JRadioButton("Bottom"); rbBottom.setMnemonic(KeyEvent.VK_B);

ButtonGroup btnGroupV = new ButtonGroup(); btnGroupV.add(rbTop); btnGroupV.add(rbMiddle); btnGroupV.add(rbBottom);

JPanel pnlRbtnV = new JPanel(new GridLayout(1, 0)); pnlRbtnV.add(rbTop); pnlRbtnV.add(rbMiddle); pnlRbtnV.add(rbBottom); pnlRbtnV.setBorder(BorderFactory.createTitledBorder("Vertical Alignment"));

rbTop.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { lblForTest.setVerticalAlignment(SwingConstants.TOP); } }); rbMiddle.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { lblForTest.setVerticalAlignment(SwingConstants.CENTER); } }); rbBottom.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { lblForTest.setVerticalAlignment(SwingConstants.BOTTOM); } }); cbText = new JCheckBox("Text", true); cbText.setMnemonic(KeyEvent.VK_T); cbIcon = new JCheckBox("Icon", true); cbIcon.setMnemonic(KeyEvent.VK_I); cbIcon.setSelected(true);

JPanel pnlCbox = new JPanel(new GridLayout(0, 1)); pnlCbox.add(cbText); pnlCbox.add(cbIcon);

cbText.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { lblForTest.setText(lblText); } else { lblForTest.setText(""); } } }); String[] strColors = {"Red", "Blue", "Green", "Cyan", "Magenta", "Yellow", "Black"}; final Color[] colors = {Color.RED, Color.BLUE, Color.GREEN, Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK}; comboColor = new JComboBox(strColors); comboColor.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { lblForTest.setForeground(colors[comboColor.getSelectedIndex()]); } } });

cbIcon.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { lblForTest.setIcon(iconMatthead); } else { lblForTest.setIcon(null); } } }); JPanel pnlCombo = new JPanel(new FlowLayout()); pnlCombo.add(comboColor); Container cp = this.getContentPane(); cp.setLayout(new BorderLayout()); cp.add(lblForTest, BorderLayout.CENTER); cp.add(pnlRbtnH, BorderLayout.NORTH); cp.add(pnlRbtnV, BorderLayout.SOUTH); cp.add(pnlCbox, BorderLayout.WEST); cp.add(pnlCombo, BorderLayout.EAST); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Button and ComboBox Demo"); setSize(400, 300); setLocationRelativeTo(null); setVisible(true); }

public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new ButtonComboBoxDemo(); } }); } }

write java code plz :) thank you

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions