Make my java assignment using jframe at low price
Question - HelloViewer3 and HelloViewer4. Please demonstrate all programs to lab instructor and answer questions. This can be done by printing the checklist file 1.1. Problems 1- 4 in this lab are based on the HelloViewer program: import javax.swing.JFrame; { 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; { private String message; private JLabel label; private static int FRAME_WIDTH = 300; private static int FRAME_HEIGHT = 3
...Read More
00; private static int DEFAULT_SIZE = 20; { 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); } } 1
! JButton: private JButton largerButton; JButton objects: largerButton = new JButton("Larger"); southPanel.add(smallerButton); southPanel.add(largerButton); createSouthPanel: !2
ActionListener interface and override the actionPerformed method. 2. Make an object of that class. 3. Install that object as the action listener of the button. "Smaller" button is clicked, we want to decrease it. To avoid integer-rounding errors, keep a floating-point instance variable fontSizeFactor that is initialized with 1. Clicking on the buttons multiplies by 1.25 or 0.8 (because 0.8 = 1/ 1.25). { public void actionPerformed(ActionEvent event) { fontSizeFactor = 1.25 * fontSizeFactor; label.setFont(new Font("Serif", Font.PLAIN, (int) (DEFAULT_SIZE * fontSizeFactor))); label.repaint(); } } This listener must be added to one of the buttons. As you can see, the code for setting up the buttons is getting quite complex. Introduce a new method createSouthPanel that contains all button initialization code. Place this class inside the createSouthPanel method, because the actionPerformed method needs to access the label and fontSizeFactor variables. action listener of the button. Place the following instructions into the frame constructor: largerButton.addActionListener(largerListener); 3
1.3. Repeat the steps above for the "Smaller" button. Make an action listener class and add an instance of that class as an action listener for the smallerButton. Run your program. Both buttons should work. other. Change your program so that they are placed on the right of the frame, one above the other. 2.1. In this series of lab exercises, start a new project called HelloViewer2. We will use a menu instead of a set of buttons to increase and decrease the font size. How to connect actions to the menu items. menu bar is attached to a window. 2. The menu bar contains menus, rectangular panels with menu strings inside them. 3. Menus contain submenus and menu items. !4
setJMenuBar(menuBar); menuBar.add(fontMenu); fontMenu.add(largerItem); fontMenu.add(smallerItem); createMenu, and place these instructions inside this method. identical to attaching actions to buttons. Simply reuse the code from the part 1.3 above. items respectively increases and decreases the font size. 3.1. In this exercise, start a project called HelloViewer3. You will see how to offer a user a selection among multiple choices. You can place all choices into a combo box. 5
! comboBox.addItem("Small"); comboBox.addItem("Medium"); comboBox.addItem("Large"); comboBox.addItem("Extra Large"); add(comboBox, BorderLayout.SOUTH); createSouthPanel method and paste the lines indicated above inside the method. Try it out. Don't forget to call the createSouthPanel method from within the constructor. Run the program and compare it against the figure. box inside a panel, even though it is a single item. southPanel.add(comboBox); add(southPanel, BorderLayout.SOUTH); Try it out. Add the combo box to the frame of the HelloViewer3 program. Compile and run the program. Pull down the item list of the combo box to see the items. !6
actionPerformed method needs to determine which item the user selected. { public void actionPerformed(ActionEvent event) { String item = (String) comboBox.getSelectedItem(); . . . } } getSelectedItem method to a String because it is possible to put other objects, such as icons, into a combo box. 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(); public static final int MEDIUM_SIZE = 18; public static final int LARGE_SIZE = 24; public static final int EXTRA_LARGE_SIZE = 36; combo box, the message should be displayed in the appropriate size. selection among multiple alternatives: 7
! When you select a radio button, the previously selected radio button is turned off. best used when you need to offer users only a small selection. goodbyeButton = new JRadioButton("Goodbye"); Lay out the buttons on the screen: southPanel.add(helloButton); southPanel.add(goodbyeButton); add(southPanel, BorderLayout.SOUTH); Add the two radio buttons to the frame of the HelloViewer program. Add a createSouthPanel method, just like you did in the previous problems. Run the program. What happens when you click the radio buttons? Why is this behavior wrong? buttons is clicked, you need to place the buttons in a button group: group.add(helloButton); group.add(goodbyeButton); !8
Now what happens when you click the buttons? text, simply check which button is currently selected. { public void actionPerformed(ActionEvent event) { String message = ""; if (helloButton.isSelected()) message = "Hello"; else if (goodbyeButton.isSelected()) message = "Goodbye"; message = message + ", World!"; label.setText(message); } } same action object to both buttons. the buttons changes the message text. form "do X" or "do nothing". Here, a check box is used to allow the user to specify whether the word "cruel" should be added to the message text: !9
same action listener to the check box and the radio buttons. actionPerformed method of the MessageListener class, check !10 ...Read Less
Solution Preview - No Solution Preview Available