Je travaille sur un projet (oui c'est pour l'école, non je ne veux pas que quelqu'un l'écrive pour moi.) J'ai trop de fierté! certains des composants de l'interface graphique de Java. Il est encore dans une étape assez rude, et une seule chose m'empêche d'obtenir ce fini. J'essaie de ne pas demander de l'aide à moins que j'en ai vraiment besoin parce que d'habitude, quand je le demande, cela se révèle être une simple erreur, alors si c'est le cas ici, calmez-moi. Quoi qu'il en soit, donc sur le code. C'est un projet de groupe donc certains de mes commentaires sont à mon partenaire. Je leur demanderais, mais il est 4h du matin ... Bref, c'est ici. Je ne sais pas pourquoi c'est dans toutes ces boîtes séparées. L'auditeur avec lequel je joue est ActionPerformed, près du bas. Je vous remercie gracieusement d'avance pour toute aide.Ajout à un JList avec une ActionListener et une interface graphique rafraîchissante après avoir ajouté
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame
{
private JPanel panel;
private JFrame frame;
private JTextArea text;
private MP3List list = new MP3List();
private JList songList;
private JScrollPane scrollList;
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuAdd;
private String[] songs;
private static String mp3msg = "Project 2: MP3 Tracker"; // Header for JOptionPane
public GUI()
{
super("mp3");
panel = new JPanel();
createGUI();
add(panel);
}
public void createGUI()
{
//This creates the frame(createGUI)
frame = new JFrame();
//Here, I made an array of the song titles and gave them to a JList
//for display. Do you think we should sort the songs?
songs = new String[list.getSize()];
for (int i = 0; i < list.getSize(); i++)
{
songs[i] = list.get(i).getSongTitle();
}
songList = new JList(songs);
//Set the selection mode to single as I want to fill in fields with info on a clicked song. More on
//that to come.
songList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//Made a scroll bar(vertical and horizontal just in case)
scrollList = new JScrollPane(songList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
text = new JTextArea(30, 30);
text.setText("This section will hold information about songs and somesuch.");
menuBar = new JMenuBar();
menu = new JMenu("File");
menuAdd = new JMenuItem("Add Song");
menuAdd.addActionListener(new menuListener());
menu.add(menuAdd);
menuBar.add(menu);
frame.setLayout(new Border());
songList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
int selectedVar;
selectedVar = songList.getSelectedIndex();
text.setText(("Song Title: " + list.get(selectedVar).getSongTitle())
+ ("\nArtist: " + list.get(selectedVar).getArtistName())
+ ("\nPlayback Time: " + list.get(selectedVar).getPlayBackTime())
+ (" || Cost: " + list.get(selectedVar).getDownloadCost())
+ (" || Size: " + list.get(selectedVar).getFileSize()));
}
});
}
public class Border extends JFrame implements LayoutManager
{
private static final long serialVersionUID = 1L;
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 300;
public Border()
{
super("MP3 Editor");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(scrollList, BorderLayout.CENTER);
add(text, BorderLayout.SOUTH);
setJMenuBar(menuBar);
setVisible(true);
}
@Override
public void addLayoutComponent(String name, Component comp)
{
// TODO Auto-generated method stub
}
@Override
public void layoutContainer(Container parent)
{
// TODO Auto-generated method stub
}
@Override
public Dimension minimumLayoutSize(Container parent)
{
// TODO Auto-generated method stub
return null;
}
@Override
public Dimension preferredLayoutSize(Container parent)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void removeLayoutComponent(Component comp)
{
// TODO Auto-generated method stub
}
}
public class menuListener extends JMenuItem implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
/*public menuListener()
{
menuItem.addActionListener(this);
}*/
public void actionPerformed(ActionEvent e)
{
MP3 aSong = getInfo();
list.add(aSong);
songs = new String[list.getSize()];
for (int i = 0; i < list.getSize(); i++)
{
songs[i] = list.get(i).getSongTitle();
}
songList = new JList(songs);
scrollList = new JScrollPane(songList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
int index = 0;
songList.setSelectedIndex(index);
songList.ensureIndexIsVisible(index);
}
}
/**
* Input one set of MP3 file information and produce a report <br>
* <p/>
* <hr>
* Date created: Sep 22, 2010 <br>
* Date last modified: Sep 22, 2010 <br>
* <p/>
* <hr>
*/
public static MP3 getInfo()
{
// Gather all information using JOptionPane
String title = JOptionPane.showInputDialog(null,
"Enter Title: ",
mp3msg,
JOptionPane.QUESTION_MESSAGE);
String artist = JOptionPane.showInputDialog(null,
"Enter Artist: ",
mp3msg,
JOptionPane.QUESTION_MESSAGE);
int seconds = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter playback time in seconds: ",
mp3msg,
JOptionPane.QUESTION_MESSAGE));
double cost = Double.parseDouble(JOptionPane.showInputDialog(null,
"Enter download cost: ",
mp3msg,
JOptionPane.QUESTION_MESSAGE));
double size = Double.parseDouble(JOptionPane.showInputDialog(null,
"Enter file size in megabytes (MB): ",
mp3msg,
JOptionPane.QUESTION_MESSAGE));
MP3 asong = new MP3(title, artist, seconds, cost, size);
return asong;
}
}
vous venez de poster le code, quelle est exactement votre question? – yurib
Si c'est pour l'école, pensez à étiqueter la question avec l'étiquette de devoirs. –
Quelle est la question? vous ne voyez pas la nouvelle chanson après l'avoir ajoutée à la liste? –