我正在研究一个项目(是的,这是为了学校,不,我不想让任何人为我写。我有太多的自豪感!)使用java的一些GUI组件。它仍处于相当粗糙的阶段,只有一件事让我无法完成这项工作。我试着不去寻求帮助,除非我真的需要它,因为通常当我确实认为这是一个简单的错误,所以如果是这样的情况,请放轻松我。无论如何,等等代码。这是一个小组项目,所以我的一些评论是给我的合作伙伴的。我会问他们,但现在是凌晨4点......无论如何,这里是。不知道为什么它在所有这些单独的盒子里。我正在搞砸的听众是ActionPerformed,靠近底部。我提前慷慨地感谢你的帮助。
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;
}
}
答案 0 :(得分:1)
您的代码存在许多小问题,但我认为最重要的一个问题是您在动作监听器中添加新MP3的部分(您说要帮助的部分)。在这里,您创建了一个全新的JList
并覆盖了之前的JList
,并为其创建了一个新的滚动窗格,但是不将新的JList添加到你的GUI所以旧的遗留下来(所以你永远不会看到任何变化)。
理想情况下,您应始终保持相同的列表,并只更新内容。有几种方法可以做到这一点,但最简单的方法之一就是简单地调用songList.setListData(songs)
。或者,不是创建字符串数组,而是创建Vector
,因为JLists也可以使用向量。
您可能想要了解的其他问题:
private JFrame frame;
从未使用过。Border
是JFrame
,但也被用作LayoutManager
,这非常令人困惑和错误。你应该摆脱它。就个人而言,我建议你从头开始重写GUI。这是一个简单的例子,有一个工作的JList供你检查:
package examples;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
public class GUI extends JFrame {
private final Vector<String> myVector = new Vector<String>();
private final JList myList = new JList();
public static void main(String... args) {
new GUI().setVisible(true);
}
public GUI() {
super("List");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(new JButton(new AddItemAction()), BorderLayout.NORTH);
add(new JScrollPane(myList), BorderLayout.CENTER);
pack();
}
private class AddItemAction extends AbstractAction {
public AddItemAction() {
super("Add Item");
}
@Override
public void actionPerformed(ActionEvent e) {
String newItem = JOptionPane.showInputDialog("Add a new item:");
if (newItem != null) {
myVector.add(newItem);
myList.setListData(myVector);
}
}
}
}
答案 1 :(得分:0)
阅读JList API并按照Swing教程中关于“如何使用列表”的secton的链接,该链接提供了在单击按钮时向JList添加值的工作示例。将您的代码与工作代码进行比较,看看有什么区别。