Chapter 14 Question 5 PE a. Write an application that allows a user to select on
ID: 3762095 • Letter: C
Question
Chapter 14 Question 5 PE
a. Write an application that allows a user to select one of at least 12 songs from a
combo box. Display the purchase price, which is different for each song. Save the
file as JTunes.java.
b. Change the JTunes application to include an editable combo box. Allow the user
to type the name of a song to purchase. Display an appropriate error message if
the desired song is not available. Save the file as JTunes2.java.
I have the following code:
import javax.swing.*;
import java.awt.*;
public class JTunes
{
public static void main(String[] args)
{
JFrame aFrame = new JFrame("Tunes");
final int HEIGHT = 100;
final int WIDTH = 250;
aFrame.setSize(WIDTH, HEIGHT);
aFrame.setVisible(true);
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* JLabel lyrics = new JLabel("I'm not listening when you say");
JLabel lyrics2 = new JLabel("Good-bye! ");
aFrame.setLayout(new FlowLayout());
aFrame.add(lyrics);
aFrame.add(lyrics2);*/
String[] tunesArray = {"Stained - For You", "James - Laid", "Chumbawamba - Tubthumping"};
JComboBox tunesChoice = new JComboBox(tunesArray);
}
When I compile it I get the following errors:
Note: JTunes.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTunes {
JFrame mainFrame;
JLabel headerLabel;
JLabel statusLabel;
JPanel controlPanel;
public JTunes(){
prepareGUI();
}
public static void main(String[] args){
JTunes jt = new JTunes();
swingControlDemo.showComboboxDemo();
}
void prepareGUI(){
mainFrame = new JFrame("Music Sell");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
void showComboboxDemo(){
headerLabel.setText("Control in action: JComboBox");
DefaultComboBoxModel songs = new DefaultComboBoxModel();
songs.add("7 things $100")
songs.add("A place in the sun $100");
songs.add("6 things $100")
songs.add("2 place in the sun $100");
songs.add("5 things $100")
songs.add("3 place in the sun $100");
songs.add("4 things $100")
songs.add("4 place in the sun $100");
songs.add("5 things $100")
songs.add("5 place in the sun $100");
songs.add("6 things $100")
songs.add("6 place in the sun $100");
JComboBox dis = new JComboBox(fruitsName);
dis.setSelectedIndex(0);
controlPanel.add(dis);
controlPanel.add(showButton);
mainFrame.setVisible(true);
}
}
When I compile it I get the following errors:
Note: JTunes.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
This are not errors, these are the warnings
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.