Assume the existence of the following Java GUI application. There are two text f
ID: 3773062 • Letter: A
Question
Assume the existence of the following Java GUI application. There are two text fields for user input. The first JTextField variable is named voltage and is used to input a voltage value in volts. The second JTextField variable named resistance is used to input a resistance value in ohms. All input values and calculations use floating point values. The user clicks a JButton to perform a calculation. One JButton called current causes the program to calculate and display the current flowing through the resistor, I = V / R. Another JButton called power causes the program to calculate the power being dissipated by the resistor, P = V * V / R. The result of the selected calculation is displayed in an output text area variable named output. The output string should say something like this: “10 volts dropped across 1000 ohms creates 0.1 amps.” Clicking one of the buttons generates the event which causes the program to do the selected calculation and update the output area. Assume the user interface has already been implemented and the member variables listed above exist. Your job is to write the ButtonHandler inner class which handles the events from the two buttons, does the requested calculation and displays the appropriate result.
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Current {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel outputLabel;
JButton currentButton = new JButton("Current");
JButton powerButton = new JButton("Power");
float current,volts,resistance,power;
JTextArea output=new JTextArea(20,30);
//private JTextArea output;
public Current(){
prepareGUI();
}
public static void main(String[] args){
Current swingControlDemo = new Current();
swingControlDemo.showEventDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Calculating Power and Current");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showEventDemo(){
headerLabel.setText("Assumend that values are already entered:");
currentButton.setActionCommand("Current");
powerButton.setActionCommand("Power");
currentButton.addActionListener(new ButtonClickListener());
powerButton.addActionListener(new ButtonClickListener());
controlPanel.add(currentButton);
controlPanel.add(powerButton);
controlPanel.add(output);
mainFrame.setVisible(true);
}
private class ButtonClickListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if( command.equals( "Current" )) {
/*here the code is to get the data from the two textboxes for voltage and resistance
convert them into float and calculate the current the append to Final Output label
which is supposed to be the final out put.
and assign the data to the textarea using 'append' method.*/
outputLabel.setText(/*current value in text format*/"");
}
else {
/*here the code is to get the data from the two textboxes for voltage and resistance
convert them into float and calculate the power the append to Final Output label
which is supposed to be the final out put.
and assign the data to the textarea using 'append' method.*/
outputLabel.setText(/*power value in text format*/"");
}
output.append(volts+" volts dropped across"+resistance+" ohms creates"+ power +"amps");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.