3.1 - In this exercise, you will see how to offer a user a selection among multiple choices. You can place all choices into a combo box using the following code:
/** File HelloViewer.java */
import javax.swing.JFrame;
public class HelloViewer
{
public static void main(String[] args)
{
JFrame frame = new HelloFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("HelloViewer");
frame.setVisible(true);
}
}
----------------------------
/** File HelloFrame.java */
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Font;
public class HelloFrame extends JFrame
{
private String message;
private JLabel label;
private static int FRAME_WIDTH = 300;
private static int FRAME_HEIGHT = 300;
private static int DEFAULT_SIZE = 20;
public HelloFrame()
{
message = "Hello, World!";
label = new JLabel(message);
label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE));
add(label, BorderLayout.CENTER);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
}
To build such a combo box, you add items to it:
final JComboBox comboBox = new JComboBox();
comboBox.addItem("Small");
comboBox.addItem("Medium");
comboBox.addItem("Large");
comboBox.addItem("Extra Large");
Then place the combo box in the south end of the frame.
add(comboBox, BorderLayout.SOUTH);
Add a createSouthPanel method and paste the lines indicated above inside the method.
3.2 - The border layout grows all items to their maximum size. To avoid this, enclose the combo box inside a panel, even though it is a single item.
JPanel southPanel = new JPanel();
southPanel.add(comboBox);
add(southPanel, BorderLayout.SOUTH);
Add the combo box to the frame of the HelloViewer program.
3.3 - To activate the combo box, you need to attach an action listener. The actionPerformed method needs to determine which item the user selected.
class ComboListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String item = (String) comboBox.getSelectedItem();
. . .
}
}
You need to cast the return value of the getSelectedItem method to a String because it is possible to put other objects, such as icons, into a combo box.
Now you simply set the correct font size, depending on the item string.
int messageSize = DEFAULT_SIZE;
if (item.equals("Small")) { messageSize = SMALL_SIZE; }
else if (item.equals("Medium")) { messageSize = MEDIUM_SIZE; }
else if (item.equals("Large")) { messageSize = LARGE_SIZE; }
else if (item.equals("Extra Large")) { messageSize = EXTRA_LARGE_SIZE; }
label.setFont(new Font("Serif", Font.PLAIN, messageSize));
label.repaint();
Define the size constants as follows:
public static final int SMALL_SIZE = 12;
public static final int MEDIUM_SIZE = 18;
public static final int LARGE_SIZE = 24;
public static final int EXTRA_LARGE_SIZE = 36;
When you select an item from the combo box, the message should be displayed in the appropriate size.
What is the complete code now?
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
3.1 - In this exercise, you will see how to offer a user a selection among...
Can you please help me to run this Java program? I have no idea why it is not running. import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; @SuppressWarnings({"unchecked", "rawtypes"}) public class SmartPhonePackages extends JFrame { private static final long serialVersionID= 6548829860028144965L; private static final int windowWidth = 400; private static final int windowHeight = 200; private static final double SalesTax = 1.06; private static JPanel panel;...
Can you fix my error? I created a program that changes labels every second in Java. But, it does not change. And, it will starts with second label. This is also an error. import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class Map2 extends JFrame{ JPanel panel; JLabel pic; Timer tm; int x = 0; String ly = "<html> " + "<br> <font size='10' color='red'> <b> 111111111 <b/> </font>...
I want the content of the combo box updates with the
corresponding radiobutton, how can I write the two radioButton
actionListeners? and mycomboBox addItemListener?
There are 4 GUI components at the top (NORTH) of the ContentPane of the JFrame, label, two radio buttons, and a combo box. The combo box is empty. When a user has clicked a radio button, the content of the combo box will be update You will need to use the setModel method and set the...
Can someone modify my code so that I do not get this error: Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1043) at java.awt.Container.add(Container.java:363) at RatePanel.<init>(RatePanel.java:64) at CurrencyConverter.main(CurrencyConverter.java:16) This code should get the amount of money in US dollars from user and then let them select which currency they are trying to convert to and then in the textfield print the amount //********************************************************************* //File name: RatePanel.java //Name: Brittany Hines //Purpose: Panel for a program that convers different currencyNamerencies to use dollars //*********************************************************************...
I want the content of the combo box updates with the
corresponding radiobutton, how can I write the two radioButton
actionListeners? and mycomboBox addItemListener?
There are 4 GUI components at the top (NORTH) of the ContentPane of the JFrame, label, two radio buttons, and a combo box. The combo box is empty. When a user has clicked a radio button, the content of the combo box will be update You will need to use the setModel method and set the...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
public class Q1 extends JFrame {
public static void createAndShowGUI() {
JFrame frame = new
JFrame("Q1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font courierFont = new
Font("Courier", Font.BOLD, 40);
Font arialFont = new Font("Arial",
Font.BOLD, 40);
Font sansFont = new
Font("Sans-serif", Font.BOLD, 20);
JLabel label = new JLabel("Enter a
word"); label.setFont(sansFont);
Font font1 = new Font("Sans-serif",
Font.BOLD, 40);
...
In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class BookReview extends JFrame implements ActionListener { private JLabel titleLabel; private JTextField titleTxtFd; private JComboBox typeCmb; private ButtonGroup ratingGP; private JButton processBnt; private JButton endBnt; private JButton clearBnt; private JTextArea entriesTxtAr; private JRadioButton excellentRdBnt; private JRadioButton veryGoodRdBnt; private JRadioButton fairRdBnt; private JRadioButton poorRdBnt; private String ratingString; private final String EXCELLENT = "Excellent"; private final String VERYGOOD = "Very Good"; private final String FAIR = "Fair"; private final String POOR = "Poor"; String...
Please fix and test my code so that all the buttons and function are working in the Sudoku. CODE: SudokuLayout.java: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class SudokuLayout extends JFrame{ JTextArea txtArea;//for output JPanel gridPanel,butPanel;//panels for sudoku bord and buttons JButton hint,reset,solve,newPuzzel;//declare buttons JComboBox difficultyBox; SudokuLayout() { setName("Sudoku Bord");//set name of frame // setTitle("Sudoku Bord");//set...
With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...