Would anyone be able to assist? Example 1: // 1. What are the following import s
ID: 3661945 • Letter: W
Question
Would anyone be able to assist?
Example 1:
// 1. What are the following import statements for?
// 2. Why do we need each one?
import javax.swing.*; // Why do we need this?
import java.awt.*; // Why do we need this?
import java.awt.event.*; // Why do we need this?
import java.util.*; // Why do we need this?
import java.math.*; // Why do we need this?
Example 2:
// Test field
// 1. What is a JLabel?
// 2. How do you create a JLabel?
// 3. What does “SwingConstants.RIGHT” mean?
t1L = new JLabel("Test score 1: ", SwingConstants.RIGHT);
t2L = new JLabel("Test score 2: ", SwingConstants.RIGHT);
t3L = new JLabel("Test score 3: ", SwingConstants.RIGHT);
t4L = new JLabel("Test score 4: ", SwingConstants.RIGHT);
Example 3:
// Text field
// 1. What is a JTextField?
// 2. How do you create a JTextField?
// 3. What does “setHorizontalAlignment(JTextField.CENTER);” mean?
t1TF = new JTextField(“0”, 5);
t1TF.setHorizontalAlignment(JTextField.CENTER);
t2TF = new JTextField(“0”, 5);
t2TF.setHorizontalAlignment(JTextField.CENTER);
t3TF = new JTextField(“0”, 5);
t3TF.setHorizontalAlignment(JTextField.CENTER);
t4TF = new JTextField(“0”, 5);
t4TF.setHorizontalAlignment(JTextField.CENTER);
Example 4:
// Calc button.
// 1. What is a JButton?
// 2. How do you create a JButton?
// 3. What is a CalculateButtonHandler()?
// 4. How do you create a CalculateButtonHandler()?
// 5. What is an ActionListener? Where is it in our textbook?
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
Explanation / Answer
Example 1:
// 1. What are the following import statements for?
Answer:
Import statement work for calling the class one package is not need to be import that is java.lang.import statement work for calling the class one package is not need to be import that is java.lang
An import statement is a way of making more of the functionality of Java available to your program. Java can do a lot of things, and not every program needs to do everything. So, to cut things down to size, so to speak, Java has its classes divided into "packages." Your own classes are part of packages, too.
// 2. Why do we need each one?
import javax.swing.*; // import all swing package class
import java.awt.*; // import all awt package class for test field and label
import java.awt.event.*; // for import the awt package sub package event all classes for awt package event handling for button ,text field etx
import java.util.*; // The * is a "regular expression operator" that will match any combination of characters. Therefore, this import statement will import everything in java.util.
import java.math.*; // for package all class of math function when we want to add the function then only need to
Example 2:
// Test field
// 1. What is a JLabel?
Answer:
JLabel is class of swing package for define the label.A JLabel object can display either text, an image, or both. You can specify where in the label's display area the label's contents are aligned by setting the vertical and horizontal alignment. By default, labels are vertically centered in their display area. Text-only labels are leading edge aligned, by default; image-only labels are horizontally centered, by default
// 2. How do you create a JLabel?
Answer:
JLabel jl = new JLabel("My first label");
// 3. What does “SwingConstants.RIGHT” mean?
Answer:
When we want to set label alignment in right ,left ,top, bottom then we use SwingConstants.RIGHT" so with the help we align the label in right
t1L = new JLabel("Test score 1: ", SwingConstants.RIGHT);
t2L = new JLabel("Test score 2: ", SwingConstants.RIGHT);
t3L = new JLabel("Test score 3: ", SwingConstants.RIGHT);
t4L = new JLabel("Test score 4: ", SwingConstants.RIGHT);
Example 3:
// Text field
// 1. What is a JTextField?
Answer:
JTextField is a lightweight component that allows the editing of a single line of text. JTextField has a method to establish the string used as the command string for the action event that gets fired. The java.awt.TextField used the text of the field as the command string for the ActionEvent. JTextField will use the command string set with the setActionCommand method if not null, otherwise it will use the text of the field as a compatibility with java.awt.TextField.
// 2. How do you create a JTextField?
Aswer:
JTextField na= new JTextField("name");
// 3. What does “setHorizontalAlignment(JTextField.CENTER);” mean?
Answer:
When we want to set text field alignment setHorizontalAlignment(JTextField.CENTER);
so with the help we align the textfield in center
t1TF = new JTextField(“0”, 5);
t1TF.setHorizontalAlignment(JTextField.CENTER);
t2TF = new JTextField(“0”, 5);
t2TF.setHorizontalAlignment(JTextField.CENTER);
t3TF = new JTextField(“0”, 5);
t3TF.setHorizontalAlignment(JTextField.CENTER);
t4TF = new JTextField(“0”, 5);
t4TF.setHorizontalAlignment(JTextField.CENTER);
Calc button.
// 1. What is a JButton?
Answer:
An implementation of a "push" button.JButton is swing class
// 2. How do you create a JButton?
Answer :
JButton click = new JButton("OK");
// 3. What is a CalculateButtonHandler()?
Answer:
This is used to register an action listener for the action; well, in fact it's overridden by every class that extends this, so it's not really!
it includes following operation
registerAction, displayElement , redisplayElement, removeDisplayComponent, insertDisplayElement
// 4. How do you create a CalculateButtonHandler()?
calculate = new JButton("calclate");
// 5. What is an ActionListener? Where is it in our textbook?
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
Answer:
ActionListener is interface when we need to implement or use must b implement and after that define the interface function actionPerformer(ActionEvent ae) method for the functionality of the button
after that we addActionListener method takes the current class object as a parameter. The "this" key word simply means "this object I'm working in right now". Or other wise we pass the parameter the class that if for use.
you can find this topic in last chapter or in event handling
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.