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("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
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*/
/*OUTPUT Q4*/
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(...
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 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.*; 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...