gpt4 book ai didi

java - 使用 ActionListener 添加到 JList 并在添加后刷新 GUI 框架

转载 作者:太空宇宙 更新时间:2023-11-04 08:45:39 28 4
gpt4 key购买 nike

我正在开发一个项目(是的,这是为了学校,不,我不想让任何人为我编写它。我太骄傲了!),使用 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;
}


}

最佳答案

您的代码存在许多小问题,但我认为对您来说最重要的是在操作监听器中添加新 MP3 的部分(您说需要帮助的部分)。在这里,您创建一个全新的 JList 并覆盖以前的 JList,并为其创建一个新的滚动 Pane ,但您将新的 JList 添加到 GUI,因此旧的 JList 仍然存在(因此您永远看不到任何更改)。

理想情况下,您应该始终保持相同的列表,并且只更新内容。有多种方法可以做到这一点,但最简单的方法之一是简单地调用 songList.setListData(songs)。或者,不要创建字符串数组,而是创建一个Vector,因为 JList 也可以与 vector 一起使用。

您可能需要查看的其他问题:

  • 私有(private) JFrame 框架; 从未使用过。
  • 您的类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);
}
}
}

}

关于java - 使用 ActionListener 添加到 JList 并在添加后刷新 GUI 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4396489/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com