Question

Simple java questions

Q2: Download Q2.java from Canvas Font Application Enter a word Enter a word Enter Enter Sans-serif Red Sans-serif Red Green o

Q2.java:

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Q2 extends JFrame {

public static void createAndShowGUI() {

JFrame frame = new JFrame("Lab");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Font font = new Font("Sans-serif", Font.BOLD, 20);

JLabel label = new JLabel("Enter a word");

label.setFont(font);

JTextField textField = new JTextField(10);

textField.setFont(font);

JButton button = new JButton("Enter");

button.setFont(font);

Font font1 = new Font("Sans-serif", Font.BOLD, 30);

JCheckBox sansSerif = new JCheckBox("Sans-serif");

sansSerif.setFont(font1);

JCheckBox serif= new JCheckBox("Serif");

serif.setFont(font1);

Font font2 = new Font("Sans-serif", Font.ITALIC, 15);

JRadioButton radioButtonRed = new JRadioButton("Red");

radioButtonRed.setFont(font2);

JRadioButton radioButtonGreen = new

JRadioButton("Green"); radioButtonGreen.setFont(font2);

JRadioButton radioButtonBlue = new JRadioButton("Blue");

radioButtonBlue.setFont(font2);

JPanel p = new JPanel();

p.add(label);

p.add(textField);

p.add(button);

JPanel checkBoxPanel = new JPanel();

checkBoxPanel.setLayout(new GridBagLayout());

GridBagConstraints c1 = new GridBagConstraints();

c1.gridx = 0; c1.gridy = GridBagConstraints.RELATIVE; c1.anchor =

GridBagConstraints.WEST;

checkBoxPanel.add(sansSerif, c1);

checkBoxPanel.add(serif, c1);

p.add(checkBoxPanel);

JPanel radioButtonPanel = new JPanel();

radioButtonPanel.setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints(); c.gridx

= 0; c.gridy = GridBagConstraints.RELATIVE; c.anchor =

GridBagConstraints.WEST;

radioButtonPanel.add(radioButtonRed, c);

radioButtonPanel.add(radioButtonGreen, c);

radioButtonPanel.add(radioButtonBlue, c);

p.add(radioButtonPanel);

JLabel label1 = new JLabel(); p.add(label1);

//ButtonGroup checkBoxes = //complete this

Q4: Rewrite Q2 using any other layout managers instead of the FlowLayout (i.e. you can choose BoxLayout, GridLayout or Border

Q2: Download Q2.java from Canvas Font Application Enter a word Enter a word Enter Enter Sans-serif Red Sans-serif Red Green o GreenSerif Serif Bue Blue Multiple check boxes can be checked at the same time, but for this exercise we set our check boxes' behavior so that only one may be checked at a time. We do the same for the radio buttons. Complete the program. What should you use? You are only required to insert the missing code fragment into your lab report.
Q4: Rewrite Q2 using any other layout managers instead of the FlowLayout (i.e. you can choose BoxLayout, GridLayout or BorderLayout etc). Insert a screenshot of your program into your lab report.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Q2.java

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Q2 extends JFrame {

      public static void createAndShowGUI() {

            JFrame frame = new JFrame("Lab");

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Font font = new Font("Sans-serif", Font.BOLD, 20);

            JLabel label = new JLabel("Enter a word");

            label.setFont(font);

            JTextField textField = new JTextField(10);

            textField.setFont(font);

            JButton button = new JButton("Enter");

            button.setFont(font);

            Font font1 = new Font("Sans-serif", Font.BOLD, 30);

            JCheckBox sansSerif = new JCheckBox("Sans-serif");

            sansSerif.setFont(font1);

            JCheckBox serif = new JCheckBox("Serif");

            serif.setFont(font1);

            Font font2 = new Font("Sans-serif", Font.ITALIC, 15);

            JRadioButton radioButtonRed = new JRadioButton("Red");

            radioButtonRed.setFont(font2);

            JRadioButton radioButtonGreen = new JRadioButton("Green");

            radioButtonGreen.setFont(font2);

            JRadioButton radioButtonBlue = new JRadioButton("Blue");

            radioButtonBlue.setFont(font2);

            JPanel p = new JPanel();

            p.add(label);

            p.add(textField);

            p.add(button);

            JPanel checkBoxPanel = new JPanel();

            checkBoxPanel.setLayout(new GridBagLayout());

            GridBagConstraints c1 = new GridBagConstraints();

            c1.gridx = 0;

            c1.gridy = GridBagConstraints.RELATIVE;

            c1.anchor = GridBagConstraints.WEST;

            checkBoxPanel.add(sansSerif, c1);

            checkBoxPanel.add(serif, c1);

            p.add(checkBoxPanel);

            JPanel radioButtonPanel = new JPanel();

            radioButtonPanel.setLayout(new GridBagLayout());

            GridBagConstraints c = new GridBagConstraints();

            c.gridx = 0;

            c.gridy = GridBagConstraints.RELATIVE;

            c.anchor = GridBagConstraints.WEST;

            radioButtonPanel.add(radioButtonRed, c);

            radioButtonPanel.add(radioButtonGreen, c);

            radioButtonPanel.add(radioButtonBlue, c);

            p.add(radioButtonPanel);

            JLabel label1 = new JLabel();

            p.add(label1);

           

            // creating a ButtonGroup

            ButtonGroup checkBoxes = new ButtonGroup();

            // adding both checkboxes to the group, so that only one can be

            // selected at a time

            checkBoxes.add(sansSerif);

            checkBoxes.add(serif);

            frame.add(p);

            frame.setSize(300, 300);

            frame.setVisible(true);

      }

      public static void main(String[] args) {

            createAndShowGUI();

      }

}

//Q4.java

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Q4 extends JFrame {

      public static void createAndShowGUI() {

            JFrame frame = new JFrame("Lab");

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Font font = new Font("Sans-serif", Font.BOLD, 20);

            JLabel label = new JLabel("Enter a word");

            label.setFont(font);

            JTextField textField = new JTextField(10);

            textField.setFont(font);

            JButton button = new JButton("Enter");

            button.setFont(font);

            Font font1 = new Font("Sans-serif", Font.BOLD, 30);

            JCheckBox sansSerif = new JCheckBox("Sans-serif");

            sansSerif.setFont(font1);

            JCheckBox serif = new JCheckBox("Serif");

            serif.setFont(font1);

            Font font2 = new Font("Sans-serif", Font.ITALIC, 15);

            JRadioButton radioButtonRed = new JRadioButton("Red");

            radioButtonRed.setFont(font2);

            JRadioButton radioButtonGreen = new JRadioButton("Green");

            radioButtonGreen.setFont(font2);

            JRadioButton radioButtonBlue = new JRadioButton("Blue");

            radioButtonBlue.setFont(font2);

            JPanel p = new JPanel();

            p.add(label);

            p.add(textField);

            p.add(button);

            JPanel checkBoxPanel = new JPanel();

           

            // using a grid layout with any number of rows and 1 column

            checkBoxPanel.setLayout(new GridLayout(0, 1));

            checkBoxPanel.add(sansSerif);

            checkBoxPanel.add(serif);

            p.add(checkBoxPanel);

            JPanel radioButtonPanel = new JPanel();

           

            // using a grid layout with any number of rows and 1 column

            radioButtonPanel.setLayout(new GridLayout(0, 1));

            radioButtonPanel.add(radioButtonRed);

            radioButtonPanel.add(radioButtonGreen);

            radioButtonPanel.add(radioButtonBlue);

            p.add(radioButtonPanel);

            JLabel label1 = new JLabel();

            p.add(label1);

           

            // creating a ButtonGroup

            ButtonGroup checkBoxes = new ButtonGroup();

            // adding both checkboxes to the group, so that only one can be

            // selected at a time

            checkBoxes.add(sansSerif);

            checkBoxes.add(serif);

            // using a vertical BoxLayout as layout for panel p

            p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));

            frame.add(p);

            frame.setSize(400, 350);

            frame.setVisible(true);

      }

      public static void main(String[] args) {

            createAndShowGUI();

      }

}

/*OUTPUT Q2*/

Lab Enter a word Enter Sans-serif Red Serif O Green O Blue

/*OUTPUT Q4*/

進」Lab Enter a word Enter Sans-serif Serif Red O Green Blue

Add a comment
Know the answer?
Add Answer to:
Simple java questions Q2.java: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Q2 extends JFrame { public static void createAndShowGUI() { JFrame frame = new JFrame(&#3...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.BorderFactory; import javax.swing.border.Border; public class Q1 extends JFrame {...

    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);       ...

  • import javax.swing.*; import java.awt.event.*; import java.awt.*; public class BookReview extends JFrame implements ActionListener {       private JLabel...

    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...

  • ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*;...

    ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class DrawingPanel implements ActionListener { public static final int DELAY = 50; // delay between repaints in millis private static final String DUMP_IMAGE_PROPERTY_NAME = "drawingpanel.save"; private static String TARGET_IMAGE_FILE_NAME = null; private static final boolean PRETTY = true; // true to anti-alias private static boolean DUMP_IMAGE = true; // true to write DrawingPanel to file private int width, height; // dimensions...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT