In: Computer Science
Create a for which will have the following controls.
1. Label for First Name and TextBox for the First Name
2. Label for Last Name and TextBox for the Last Name
3. Label for bank account balance and TextBox for the bank account balance (assume $1000 in the bank account)
4. Create a button Name. The user will click the button and the first name and last name will be displayed on the label. You will create a label called lblFirstLastName to display the First and last Name.
5. Create a button bankbalance. The user will click the button and the balance will be displayed on the label. You will create a label called lblBankbalance to display the bank balance.
6. Create a Cancel button to cancel the form
------------------------------------------------------------------------------------------------
package swing;
import javax.swing.*;
public class SwingInheritance extends JFrame
{
private static final long serialVersionUID = 1L;
SwingInheritance()
{
JTextField userName = new
JTextField();
//userName.setBounds(x, y, width,
height);
userName.setBounds(80,100,100,
40);
userName.setText("Hello");
JLabel lblText = new
JLabel();
lblText.setBounds(80,150,300,
40);
lblText.setText("You Entered: " +
userName.getText());
JButton button=new
JButton("click");
//create button
button.setBounds(80,200,100,
40);
JButton close = new
JButton("Close");
close.setBounds(80,250,100,
40);
close.addActionListener(e
->
{
dispose();
});
//add to JFrame
add(button);
add(close);
add(userName);
add(lblText);
//set frame properties
setSize(400,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args)
{
new SwingInheritance();
}
}
/*************** below is the code ***************/
import javax.swing.*;
public class SwingInheritance extends JFrame {
private static final long serialVersionUID = 1L;
SwingInheritance() {
JLabel firstNameLabel = new JLabel("First Name: ");
JLabel lastNameLabel = new JLabel("Last Name: ");
JLabel bankBalanceLabel = new JLabel("Account Balance: ");
firstNameLabel.setBounds(15, 20, 150, 20);
lastNameLabel.setBounds(15, 45, 150, 20);
bankBalanceLabel.setBounds(15, 70, 150, 20);
JTextField lastNameField = new JTextField();
JTextField firstNameField = new JTextField();
JTextField bankBalanceField = new JTextField();
firstNameField.setBounds(175, 20, 200, 20);
lastNameField.setBounds(175, 45, 200, 20);
bankBalanceField.setBounds(175, 70, 200, 20);
final String DEFAULT_TEXT = "Click above";
JLabel lblFirstLastName = new JLabel(DEFAULT_TEXT, JLabel.CENTER);
JLabel lblBankBalance = new JLabel(DEFAULT_TEXT, JLabel.CENTER);
lblFirstLastName.setBounds(10, 185, 180, 20);
lblBankBalance.setBounds(210, 185, 180, 20);
JButton nameButton = new JButton("Name");
JButton bankBalanceButton = new JButton("Balance");
nameButton.setBounds(50, 150, 100, 30);
bankBalanceButton.setBounds(250, 150, 100, 30);
JButton close = new JButton("Close");
close.setBounds(140, 280, 100, 40);
close.addActionListener(e -> {
dispose();
});
nameButton.addActionListener(e -> {
String fName = firstNameField.getText();
String lName = lastNameField.getText();
if (!fName.equals("") || !lName.equals(""))
lblFirstLastName.setText(fName + " " + lName);
else
lblFirstLastName.setText(DEFAULT_TEXT);
});
bankBalanceButton.addActionListener(e -> {
String balance = bankBalanceField.getText();
if (!balance.equals(""))
lblBankBalance.setText(balance);
else
lblFirstLastName.setText(DEFAULT_TEXT);
});
//add to JFrame
add(close);
add(firstNameLabel);
add(lastNameLabel);
add(bankBalanceLabel);
add(firstNameField);
add(lastNameField);
add(bankBalanceField);
add(lblFirstLastName);
add(lblBankBalance);
add(nameButton);
add(bankBalanceButton);
//set frame properties
setSize(400, 400);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new SwingInheritance();
}
}
/************** Output ***************/
/************ If you have any questions, you may ask in comments **************/
/********************** Thanks for reading *************************/