如何在JFrame中调整JScrollPane的大小?以及如何从Java文件中读取内容? [英] How do I resize a JScrollPane within a JFrame? And how do I read from a file in Java?

查看:272
本文介绍了如何在JFrame中调整JScrollPane的大小?以及如何从Java文件中读取内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个歌曲库,并且我希望此选择列表仅位于窗口的左侧,因为我想将有关歌曲的其他信息放在右侧.我不确定如何更改JFrame内部的JScrollPane的大小.

I have a song library, and I would like this selection list to only be on the left hand side of the window because I want to put other information about the song on the righthand side. I'm not sure how to change the size of JScrollPane, which is inside the JFrame.

在此库中,我希望能够将文件中存储的歌曲导入到我的歌曲库中.现在,我的代码中有一个数组,但是我希望能够从文本文件中读取而不是使用这种方法.在文件中,我希望能够存储有关歌曲的艺术家和专辑信息,但我不希望它显示在歌曲列表中.

In this library, I want to be able to import the songs stored in a file to my song library. Right now, I have an array within my code, but I want to be able to read from a text file instead of using this approach. In the file, I want to be able to store artist and album information about the song, but I don't want it to display in the song list.

    String songs[] = {"Song1", "Song2", "Song3", "Song4", "Song5"};
    JList list = new JList(songs);

    public SongLib(){

        JFrame songLibrary = new JFrame("Song Library");
        songLibrary.setLocationRelativeTo(null);
        songLibrary.setResizable(true);
        songLibrary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

        list.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent evt){
                int i = list.getSelectedIndex();
                if (i != -1)
                    System.out.println("Selected: " + songs[i]);
                else
                    System.out.println("Choose a song");    
            }
        });

        JScrollPane JSPane = new JScrollPane(list);
        JSPane.setPreferredSize(new Dimension(100,100));
        songLibrary.add(JSPane);
        songLibrary.setSize(400,400);
        songLibrary.setVisible(true);
    }

    public static void main(String[] args){
        new SongLib();
    }

推荐答案

  1. (在您的整个余生中)停止通话setPreferredSize().这意味着此调用:JSPane.setPreferredSize(new Dimension(100,100));应该绝对被删除.
  2. 如果要并排放置两个带有可拖动分隔符的面板,请使用JSplitPane.如果您不希望使用可拖动的分隔线,请使用带有适当LayoutManagerJPanel(GridBagLayout可能是一个不错的选择)
  3. 从文件读取非常容易,只需在SO上进行搜索,您就会发现数百个响应.如果您想解析csv文件,则周围有一些库可以帮助您做到这一点.最终,如果您考虑让它成为一种应用程序,那么有些小型的纯Java可嵌入数据库将比简单的文本文件在读取/存储/搜索信息方面做得更好.
  4. 学习并遵循Java命名约定:变量始终以小写字母开头.
  1. Stop calling (for the rest of your entire life) setPreferredSize(). Meaning that this call: JSPane.setPreferredSize(new Dimension(100,100)); should definitely be removed.
  2. If you want to have 2 panels side-by-side with a draggable separator: use JSplitPane. If you don't want the draggable divider, use a JPanel with an appropriate LayoutManager (GridBagLayout may be a good choice)
  3. Reading from a file is pretty easy, just make a search on SO and you will find hundreds of response. If you want to parse csv-files, there are some libraries around that can help you do that. Eventually, if you consider making this an application for a while, there are some small pure-java, embeddable, databases which will do a much better job at reading/storing/searching information than a simple text-file.
  4. Learn the Java naming conventions and stick to them: variables always start with a lower-case letter.

这篇关于如何在JFrame中调整JScrollPane的大小?以及如何从Java文件中读取内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆