need help fixing my code please! package stupidbutton; import javax.swing.JLabel
ID: 3788922 • Letter: N
Question
need help fixing my code please!
package stupidbutton;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
*
*/
public class MessagePanel {
private JLabel message;
public MessagePanel(){
message = new JLabel("jump on it");
add(message);
}
public void changeLabel(String newLabel){
message.setText(newLabel);
}
}
package stupidbutton;
/**
*
*
*/
public class StupidButton {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
StupidFrame me = new StupidFrame();
}
}
package stupidbutton;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
*
*
*/
public class StupidFrame extends JFrame {
private MessagePanel myPanel;
public StupidFrame(){
setTitle("Hello");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(100, 100);
myPanel = new MessagePanel();
add(myPanel, BorderLayout.CENTER);
JButton theTurth = new JButton("The Truth");
add(theTruth.BorderLayout.SOUTH);
coltsHandler myHandler = new coltsHandler();
theTruth.addActionListener(handler);
setVisible(true);
}
private class coltsHandler implements ActionListener{
public void actionPreformed(ActionEvent event){
System.out.println("Krusty crab pizza" + event.getActionCommand());
myPanel.changeLabel("Money Money Money");
}
}
}
Explanation / Answer
Hi, I have fixed compile time issue.
You can now add/modify your program.
please let me know in case of any issue.
import java.awt.Component;
import javax.swing.JLabel;
public class MessagePanel extends Component {
private JLabel message;
public MessagePanel(){
message = new JLabel("jump on it");
//add(message);
}
public void changeLabel(String newLabel){
message.setText(newLabel);
}
}
##########
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
*
*
*/
public class StupidFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private MessagePanel myPanel;
public StupidFrame(){
setTitle("Hello");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(100, 100);
myPanel = new MessagePanel();
add(myPanel, BorderLayout.CENTER);
JButton theTurth = new JButton("The Truth");
add(theTurth);
coltsHandler myHandler = new coltsHandler();
theTurth.addActionListener(myHandler);
setVisible(true);
}
private class coltsHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent event) {
System.out.println("Krusty crab pizza" + event.getActionCommand());
myPanel.changeLabel("Money Money Money");
}
}
}
############
public class StupidButton {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
StupidFrame me = new StupidFrame();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.