在我添加了一个对象之后,我重新修改了一个类。
public class Window extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private MediaHandler myMediaHandler = new MediaHandler(this);
class MenuActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String buttonText = e.getActionCommand();
switch(buttonText) {
case "Add Movie":
AddMovieWindow addMovieGUI = new AddMovieWindow(myMediaHandler);
addMovieGUI.setVisible(true);
break;
case "Add TV-Show":
AddTVShowWindow addTVShowGUI = new AddTVShowWindow(myMediaHandler);
addTVShowGUI.setVisible(true);
break;
}
}
}
public Window() {
// myMediaHandler.addMovie("Nineteen Eighty-Four", 113.00, 1984, "Michael Radford", "mediaPath", "https://dl.dropboxusercontent.com/u/16670644/Projekt/1984.png", "HD", false, "Eng", "George Orwell (novel)");
addComponents();
configurFrame();
addMenu();
validate();
repaint();
}
private void addMenu() {
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Menu");
bar.add(menu);
JMenuItem item = new JMenuItem("Add Movie");
item.addActionListener(new MenuActionListener());
menu.add(item);
item = new JMenuItem("Add TV-Show");
item.addActionListener(new MenuActionListener());
menu.add(item);
this.setJMenuBar(bar);
}
private void configurFrame() {
this.setSize(1205, 850);
this.setTitle("Video Library");
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setIconImage(new ImageIcon("E:\\Dropbox\\Dropbox\\Programmering\\Java\\Projekt IV 1\\icon.png").getImage());
try {
java.net.URL where = new URL("https://dl.dropboxusercontent.com/u/16670644/Projekt/BackGround.png");
ImageIcon image = new ImageIcon(where);
JLabel imageLabel = new JLabel(image);
this.add(imageLabel);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
System.out.println("Error loading Background that makes the background image");
e.printStackTrace();
}
}
private void addComponents() {
JPanel shelfs = buildShelfs();
this.add(shelfs);
}
private JPanel buildShelfs() {
JPanel mediaPanel = new JPanel();
int nrOfMedias = myMediaHandler.mediaList.size();
mediaPanel.setOpaque(false);
mediaPanel.setLayout(new GridLayout(3,6,22,30));
mediaPanel.setBounds(90, 25, 1020, 745);
java.net.URL where;
for(int i = 0; i < nrOfMedias; i++) {
String temp = myMediaHandler.mediaList.get(i).getImagePath();
try {
where = new URL(temp);
ImageIcon image = new ImageIcon(where);
JLabel imageLabel = new JLabel(image);
mediaPanel.add(imageLabel);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
System.out.println("Error loading user media picture");
e.printStackTrace();
}
}
java.net.URL where1;
try {
for(int i = nrOfMedias; i < 18; i++) {
where1 = new URL("https://dl.dropboxusercontent.com/u/16670644/Projekt/TempPic.png");
ImageIcon image1 = new ImageIcon(where1);
JLabel imageLabel1 = new JLabel(image1);
mediaPanel.add(imageLabel1);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
System.out.println("Error loading TempPic that shows allowed media spots");
e.printStackTrace();
}
return mediaPanel;
}
public static void main(String[] arg) {
Window mainWindowGUI = new Window();
mainWindowGUI.setVisible(true);
}
}
我现在要做的是来自另一个调用rapaint函数的类。
public class MediaHandler {
ArrayList<Media> mediaList;
private Window myWindow;
public MediaHandler(Window window) {
this.mediaList = new ArrayList<Media>();
myWindow = window;
}
public void addMovie(String title, Double playTime, int year,
String directory, String mediaPath, String imagePath, String quality,
boolean subtitles, String language, String writer) {
mediaList.add(new Movie(title, playTime, year, false, directory, mediaPath, imagePath, rating.unrated, quality, subtitles, language, writer));
myWindow.getContentPane().invalidate();
myWindow.invalidate();
}
}
如果有任何不清楚的地方,请告诉我!我已经被困在这个问题上几天了。
答案 0 :(得分:0)
它有很多代码,而且结构不是很干净。但据我所知,问题不能只通过&#34;重新获得&#34;任何东西。问题是标签(显示媒体图像)没有添加到架子上。
当构建框架时,您当前正在调用buildShelfs
方法一次。当您稍后通过MediaHandler
添加新电影时,您要将此电影添加到mediaList
。但您不向mediaPanel
添加新标签/图片。
这可以解决如下:您可以将mediaPanel
声明为实例变量。并且您可以提供一种向此面板添加新ImageIcon
的方法,其中包含从特定URL(以字符串形式给出)读取的图像。一方面可以在初始化期间(即,在第一次调用buildShelfs
期间)使用此方法。但更重要的是:您也可以从MediaHandler
调用此方法,这样只要在mediaPanel
中添加新电影,就会将新的ImageIcon添加到MovieHandler
。
我试图在下面的代码中描绘基本思想。但同样,当前代码的结构并不是很好,也很干净,因此需要进行一些猜测......
class Window ...
{
// Store this as an instance variable
private JPanel mediaPanel;
private JPanel buildShelfs()
{
mediaPanel = new JPanel();
mediaPanel.setOpaque(false);
mediaPanel.setLayout(new GridLayout(3, 6, 22, 30));
mediaPanel.setBounds(90, 25, 1020, 745);
for (int i=0; i<18; i++)
{
addMedia(i);
}
return mediaPanel;
}
private void addMedia(int index)
{
// You should store these images LOCALLY!!!
String urlString = "https://dl.dropboxusercontent.com/u/16670644/Projekt/TempPic.png";
int nrOfMedias = myMediaHandler.mediaList.size();
if (index < nrOfMedias)
{
urlString = myMediaHandler.mediaList.get(i).getImagePath();
}
addMedia(urlString);
}
void addMedia(String urlString)
{
try
{
java.net.URL where = new URL(urlString);
ImageIcon image = new ImageIcon(where);
JLabel imageLabel = new JLabel(image);
mediaPanel.add(imageLabel);
mediaPanel.invalidate();
mediaPanel.validate();
}
catch (MalformedURLException e)
{
System.out.println("Error loading user media picture from "+urlString);
e.printStackTrace();
}
}
}
class MediaHandler
{
ArrayList<Media> mediaList;
private Window myWindow;
public MediaHandler(Window window)
{
this.mediaList = new ArrayList<Media>();
myWindow = window;
}
public void addMovie(String title, Double playTime, int year,
String directory, String mediaPath, String imagePath, String quality,
boolean subtitles, String language, String writer)
{
mediaList.add(new Movie(title, playTime, year,
false, directory, mediaPath, imagePath, rating.unrated,
quality, subtitles, language, writer));
myWindow.addMedia(mediaPath);
}
}