I need to create a JSlider for the message the user entered. You are able to ent
ID: 3688632 • Letter: I
Question
I need to create a JSlider for the message the user entered. You are able to enter a message and the message is displayed and you can edit the colors, italic and bold. I also need to to be able to use a jslider to adjust the position of the text within the text field. Here is my code so far.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class GUIEventDemo extends JFrame {
private JLabel jlblMessage = new JLabel("Hello", JLabel.CENTER);
// Create check boxes to set the font for the message
private JCheckBox jchkBold = new JCheckBox("Bold");
private JCheckBox jchkItalic = new JCheckBox("Italic");
// Create three radio buttons to set message colors
private JRadioButton jrbRed = new JRadioButton("Red");
private JRadioButton jrbGreen = new JRadioButton("Green");
private JRadioButton jrbBlue = new JRadioButton("Blue");
// Create a text field for setting a new message
private JTextField jtfMessage = new JTextField(10);
public static void main(String[] args) {
GUIEventDemo frame = new GUIEventDemo();
frame.pack();
frame.setTitle("GUIEventDemo");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public GUIEventDemo() {
jlblMessage.setBorder(new LineBorder(Color.BLACK, 2));
add(jlblMessage, BorderLayout.CENTER);
// Create a panel to hold check boxes
JPanel jpCheckBoxes = new JPanel();
jpCheckBoxes.setLayout(new GridLayout(2, 1));
jpCheckBoxes.add(jchkBold);
jpCheckBoxes.add(jchkItalic);
add(jpCheckBoxes, BorderLayout.EAST);
// Create a panel to hold radio buttons
JPanel jpRadioButtons = new JPanel();
jpRadioButtons.setLayout(new GridLayout(3, 1));
jpRadioButtons.add(jrbRed);
jpRadioButtons.add(jrbGreen);
jpRadioButtons.add(jrbBlue);
add(jpRadioButtons, BorderLayout.WEST);
// Create a radio-button group to group three buttons
ButtonGroup group = new ButtonGroup();
group.add(jrbRed);
group.add(jrbGreen);
group.add(jrbBlue);
// Set initial message color to blue
jrbBlue.setSelected(true);
jlblMessage.setForeground(Color.blue);
// Create a panel to hold label and text field
JPanel jpTextField = new JPanel();
jpTextField.setLayout(new BorderLayout(5, 0));
jpTextField.add(
new JLabel("Enter a new message"), BorderLayout.WEST);
jpTextField.add(jtfMessage, BorderLayout.CENTER);
jtfMessage.setHorizontalAlignment(JTextField.RIGHT);
add(jpTextField, BorderLayout.NORTH);
// Set mnemonic keys for check boxes and radio buttons
jchkBold.setMnemonic('B');
jchkItalic.setMnemonic('I');
jrbRed.setMnemonic('E');
jrbGreen.setMnemonic('G');
jrbBlue.setMnemonic('U');
// Register listeners with check boxes
jchkBold.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setNewFont();
}
});
jchkItalic.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setNewFont();
}
});
// Register listeners for radio buttons
jrbRed.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jlblMessage.setForeground(Color.red);
}
});
jrbGreen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jlblMessage.setForeground(Color.green);
}
});
jrbBlue.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jlblMessage.setForeground(Color.blue);
}
});
// Register listener for text field
jtfMessage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jlblMessage.setText(jtfMessage.getText());
jtfMessage.requestFocusInWindow();
}
});
}
private void setNewFont() {
// Determine a font style
int fontStyle = Font.PLAIN;
fontStyle += (jchkBold.isSelected() ? Font.BOLD : Font.PLAIN);
fontStyle += (jchkItalic.isSelected() ? Font.ITALIC : Font.PLAIN);
// Set font for the message
Font font = jlblMessage.getFont();
jlblMessage.setFont(
new Font(font.getName(), fontStyle, font.getSize()));
}
}
Explanation / Answer
GUIEventDemo.java:
/*in this program the text field along with three colors and two formats will be there if we change the text filed value the value get updater*/
package chegg;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class GUIEventDemo extends JFrame {
private static JLabel jlblMessage = new JLabel("Hello", JLabel.CENTER);
// Create check boxes to set the font for the message
private JCheckBox jchkBold = new JCheckBox("Bold");
private JCheckBox jchkItalic = new JCheckBox("Italic");
// Create three radio buttons to set message colors
private JRadioButton jrbRed = new JRadioButton("Red");
private JRadioButton jrbGreen = new JRadioButton("Green");
private JRadioButton jrbBlue = new JRadioButton("Blue");
// Create a text field for setting a new message
private JTextField jtfMessage = new JTextField(10);
private JSlider s1, s2, s3;
private int red=0, green=0, blue=0;
public static void main(String[] args) {
GUIEventDemo frame = new GUIEventDemo();
frame.pack();
frame.setTitle("GUIEventDemo");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
SliderPanel s=new SliderPanel();
}
public GUIEventDemo() {
jlblMessage.setBorder(new LineBorder(Color.BLACK, 2));
add(jlblMessage, BorderLayout.CENTER);
// Create a panel to hold check boxes
JPanel jpCheckBoxes = new JPanel();
jpCheckBoxes.setLayout(new GridLayout(2, 1));
jpCheckBoxes.add(jchkBold);
jpCheckBoxes.add(jchkItalic);
add(jpCheckBoxes, BorderLayout.EAST);
// Create a panel to hold radio buttons
JPanel jpRadioButtons = new JPanel();
jpRadioButtons.setLayout(new GridLayout(3, 1));
jpRadioButtons.add(jrbRed);
jpRadioButtons.add(jrbGreen);
jpRadioButtons.add(jrbBlue);
add(jpRadioButtons, BorderLayout.WEST);
// Create a radio-button group to group three buttons
ButtonGroup group = new ButtonGroup();
group.add(jrbRed);
group.add(jrbGreen);
group.add(jrbBlue);
// Set initial message color to blue
jrbBlue.setSelected(true);
jlblMessage.setForeground(Color.blue);
// Create a panel to hold label and text field
JPanel jpTextField = new JPanel();
jpTextField.setLayout(new BorderLayout(5, 0));
jpTextField.add(
new JLabel("Enter a new message"), BorderLayout.WEST);
jpTextField.add(jtfMessage, BorderLayout.CENTER);
jtfMessage.setHorizontalAlignment(JTextField.RIGHT);
add(jpTextField, BorderLayout.NORTH);
// Set mnemonic keys for check boxes and radio buttons
jchkBold.setMnemonic('B');
jchkItalic.setMnemonic('I');
jrbRed.setMnemonic('E');
jrbGreen.setMnemonic('G');
jrbBlue.setMnemonic('U');
// Register listeners with check boxes
jchkBold.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setNewFont();
}
});
jchkItalic.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setNewFont();
}
});
// Register listeners for radio buttons
jrbRed.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jlblMessage.setForeground(Color.red);
}
});
jrbGreen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jlblMessage.setForeground(Color.green);
}
});
jrbBlue.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jlblMessage.setForeground(Color.blue);
}
});
// Register listener for text field
jtfMessage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jlblMessage.setText(jtfMessage.getText());
jtfMessage.requestFocusInWindow();
}
});
}
private void setNewFont() {
// Determine a font style
int fontStyle = Font.PLAIN;
fontStyle += (jchkBold.isSelected() ? Font.BOLD : Font.PLAIN);
fontStyle += (jchkItalic.isSelected() ? Font.ITALIC : Font.PLAIN);
// Set font for the message
Font font = jlblMessage.getFont();
jlblMessage.setFont(
new Font(font.getName(), fontStyle, font.getSize()));
}
public static class SliderPanel extends JPanel
implements ChangeListener {
private JSlider s1, s2, s3;
private int red, green, blue;
public SliderPanel() {
red=0;green=0;blue=0;
s1=new JSlider(0,255,0);s2=new JSlider(0,255,0);
s3=new JSlider(0,255,0);s1.addChangeListener(this);
s2.addChangeListener(this);s3.addChangeListener(this);
JPanel p1=new JPanel(new GridLayout(3,1));
p1.add(s1); p1.add(s2); p1.add(s3); add(p1); }
public void stateChanged(ChangeEvent e) {
if(e.getSource()==s1) red=s1.getValue();
if(e.getSource()==s2) green=s2.getValue();
if(e.getSource()==s3) blue=s3.getValue();
repaint();
}
}
}
/*above we inlcude slider demo inorder to move the text in the text field here is the sample program which can slide the text field*/
SliderDemo.java and MessagePanel.java:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class SliderDemo extends JFrame {
// Create horizontal and vertical sliders
private JSlider jsldHort = new JSlider(JSlider.HORIZONTAL);
private JSlider jsldVert = new JSlider(JSlider.VERTICAL);
// Create a MessagePanel
private MessagePanel messagePanel =
new MessagePanel("Welcome to Java");
public static void main(String[] args) {
SliderDemo frame = new SliderDemo();
frame.setTitle("SliderDemo");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public SliderDemo() {
// Add sliders and message panel to the frame
setLayout(new BorderLayout(5, 5));
add(messagePanel, BorderLayout.CENTER);
add(jsldVert, BorderLayout.EAST);
add(jsldHort, BorderLayout.SOUTH);
// Set properties for sliders
jsldHort.setMaximum(50);
jsldHort.setPaintLabels(true);
jsldHort.setPaintTicks(true);
jsldHort.setMajorTickSpacing(10);
jsldHort.setMinorTickSpacing(1);
jsldHort.setPaintTrack(false);
jsldVert.setInverted(true);
jsldVert.setMaximum(10);
jsldVert.setPaintLabels(true);
jsldVert.setPaintTicks(true);
jsldVert.setMajorTickSpacing(10);
jsldVert.setMinorTickSpacing(1);
// Register listener for the sliders
jsldHort.addChangeListener(new ChangeListener() {
/** Handle scroll bar adjustment actions */
public void stateChanged(ChangeEvent e) {
// getValue() and getMaximumValue() return int, but for better
// precision, use double
double value = jsldHort.getValue();
double maximumValue = jsldHort.getMaximum();
double newX = (value * messagePanel.getWidth() /
maximumValue);
messagePanel.setXCoordinate((int)newX);
}
});
jsldVert.addChangeListener(new ChangeListener() {
/** Handle scroll bar adjustment actions */
public void stateChanged(ChangeEvent e) {
// getValue() and getMaximumValue() return int, but for better
// precision, use double
double value = jsldVert.getValue();
double maximumValue = jsldVert.getMaximum();
double newY = (value * messagePanel.getHeight() /
maximumValue);
messagePanel.setYCoordinate((int) newY);
}
});
}
}
//Message Panel.java:
// MessagePanel.java: Display a message on a JPanel
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class MessagePanel extends JPanel
{
private String message = "Welcome to Java"; // Message to display
// (x, y) coordinates where the message is displayed
private int xCoordinate = 20;
private int yCoordinate = 20;
// Indicating whether the message is displayed in the center
private boolean centered;
// Default constructor
public MessagePanel()
{
}
// Contructor with a message parameter
public MessagePanel(String message)
{
this.message = message;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
public int getXCoordinate()
{
return xCoordinate;
}
public void setXCoordinate(int x)
{
this.xCoordinate = x;
}
public int getYCoordinate()
{
return yCoordinate;
}
public void setYCoordinate(int y)
{
this.yCoordinate = y;
}
public boolean isCentered()
{
return centered;
}
public void setCentered(boolean centered)
{
this.centered = centered;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (centered)
{
// Get font metrics for the current font
FontMetrics fm = g.getFontMetrics();
// Find the center location to display
int w = fm.stringWidth(message); // Get the string width
int h = fm.getAscent(); // Get the string height
xCoordinate = (getWidth()-w)/2;
yCoordinate = (getHeight()+h)/2;
}
g.drawString(message, xCoordinate, yCoordinate);
}
public Dimension getPreferredSize()
{
return new Dimension(200, 100);
}
public Dimension getMinimumSize()
{
return new Dimension(200, 100);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.