In this step we will create a class for a new graphic element is an extended key
ID: 3761413 • Letter: I
Question
In this step we will create a class for a new graphic element is an extended key, ie a class that inherits from JButton. The button should have two different states. Pressing the button will change the state, and it should be visible. Two pressure will bring the key back to the starting state. The different states are displayed by color and / or text on the button changes. To use components and functionality described here must import javax.swing. *; Java.awt. * And java.awt.event. *.
Task E1.1 Define MyButton class that inherits from JButton. Add the necessary fields to keep track of the ignition key is in, and what information it should show in these conditions.
Task E1.2 Define the constructor MyButton (Color col1, col2 Color, text1 String, String text2) where you can set the color of each text to be displayed in each state. The constructor should set the switch to the first state, and bring color and text. Define also a constructor with no arguments, which it calls the first constructor with predetermined arguments.
Task E1.3 Define method toggleState (). A call to this method should replace the state that the button is in, and bring color and text in accordance with the new state.
In Java
Explanation / Answer
public class MyButton extends JButton {
int press;
Color c1,c2;
public MyButton(Color col1, Color col2,String text1,String text2){
super(text1);
c1=col1;
c2=col2;
press=0;
setBackground(col1);
addActionListener(new ActionListener()
}
@Override
public void actionPerformed(ActionEvent e) {
if (++press%2==0)
{
toggleState();
state=0;
press=0;
}
If (state=0)
setBackground(c1);
else
setBackground(c2);
}
}
void toggleState()
{
State=1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.