Create an application with a JFrame that holds five labels describing reasons th
ID: 3639303 • Letter: C
Question
Create an application with a JFrame that holds five labels describing reasons that
a customer might not buy your product (for example, “Too expensive”). Every time
the user clicks a JButton, remove one of the negative reasons. Save the file as
JDemoResistance.java.
This is what I have, but it's not working!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JDemoResistance extends JFrame implements ActionListener
{
Font firstFont = new Font("Times Roman", Font.ITALIC, 22);
JLabel demo = new JLabel("What reasons made you not purchase this item?");
JButton button1 = new JButton("Cost!");
JButton button2 = new JButton("Quality!");
JButton button3 = new JButton("Waranty!");
JButton button4 = new JButton("Features!");
JButton button5 = new JButton("Other!");
public JDemoResistance()
{
super("Demonstration!");
Container con = getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demo.setFont(firstFont);
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
con.setLayout(new FlowLayout());
con.add(pane1);
con.add(pane2);
pane1.add(demo);
pane2.add(button1);
pane2.add(button2);
pane2.add(button3);
pane2.add(button4);
pane2.add(button5);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
}
public static void main(String[] arg)
{
JDemoResistance aFrame = new JDemoResistance();
int width = 550;
int height = 200;
aFrame.setSize(width, height);
aFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == button1)
remove(button1);
else if(source == button2)
remove(button2);
else if(source == button3)
remove(button3);
else if(source == button4)
remove(button4);
else if(source == button5)
remove(button5);
validate();
}
}
Explanation / Answer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.