日期:2014-05-20  浏览次数:21153 次

JPanel刷新
我在一个JTabbedPane的一个选项卡上的JPanel上用JList写个表,不知道为什么不能刷新,只能显示一次。
Java code

/**
 * 主页窗口
 */
public class UsedToChangeMyFrame extends JFrame {
    public static FirstLabel firstPanel = new FirstLabel();
    public UsedToChangeMyFrame usedToChangeMyFrame;
//    JPanel fourthLabel;//刷新列表获得对象
//    JTabbedPane tabbedPane;
//    fourthLabel = new FourthLabel();
    public static FourthLabel fourthLabel;//刷新列表目标对象
    private static final long serialVersionUID = 1L;
    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP,
            JTabbedPane.WRAP_TAB_LAYOUT);
    // ImageIcon icon = new ImageIcon("F:\\icon\\iconDigit\\xiaoxin.jpg");
    // Image image = icon.getImage();
    // ImageIcon icon2 = new ImageIcon(
    // "E:\\Users\\hp\\workspace\\cdzProjectStartOn0908\\src\\test\\ico/open.gif");
    Map<String, String> books = new LinkedHashMap<String, String>();
    URL url = this.getClass().getResource("/icon/pro/win1.gif");// 参见cdzProjectStartOn0908\src\ztest\tempTest\NewMain.java
    ImageIcon iicon = new ImageIcon(url);

    /**
     * 主页窗口
     */
    public UsedToChangeMyFrame() {
        init();
//        InOn.setUsedToChangeMyFrame(usedToChangeMyFrame);
        StoreUsedToChangeMyFrame.setUsedToChangeMyFrame(usedToChangeMyFrame);

    }

    public void init() {
        usedToChangeMyFrame = this;
//        InOn.setUsedToChangeMyFrame(this.usedToChangeMyFrame);
        JScrollPane xxp1 = new MusicListTable().getJScrollPane();
//        FirstLabel firstPanel = new FirstLabel();
        SecondLabel secondPanel = new SecondLabel();
        // 第四个标签页,我的音乐文件夹
        //FourthLabel fourthLabel; //= new FourthLabel();
         fourthLabel = new FourthLabel();
        firstPanel.init();// 这句坑死爹,缺失引发空指针Exception

        // SecondLabel2 secondPanel2 = new SecondLabel2();
        // SecondLabel3 secondPanel3 = new SecondLabel3();

//         JPanel xxp = new MusicListTable().getPanelWithTable();
        // 第四个标签页:...要改为musicPlatform\client\labels_OfJTabbedPane\FourthLabel.java
        JPanel fourthJPabel = new FourthLabel();
        setVisible(true);
        setResizable(false);
        // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // setSize(800, 500);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(new Dimension(850, 550));
        this.setLocation(screenSize.width / 2 - 850 / 2,
                screenSize.height / 2 - 250);
        JPanel lowerPanel = new MyPanel2(); // 在底部添加一个Panel
        lowerPanel.setPreferredSize(new Dimension(600, 60));
        Container c = this.getContentPane();

        // JPanel jpanel = new MusicListTable().getPanelWithTable(); // ()?
        // books.put("歌曲列表", null);
        // books.put("下载列表", null);
        // books.put("我的音乐", jpanel);
        // 向JTabbedPane中添加4个Tab页面,指定了标题、图标和提示,但该Tab页面的组件为null
        // for (String bookName : books.keySet()) {
        // tabbedPane.addTab(bookName, icon, null);
        tabbedPane.addTab(" 主窗口  ", firstPanel);
        tabbedPane.addTab("歌曲列表", secondPanel);
        tabbedPane.addTab(" 下载列表 ", null);
//         tabbedPane.addTab(" 我的音乐 ", xxp);
        tabbedPane.addTab(" 我的音乐 ", fourthLabel);
        // tabbedPane.addTab(" 我的音乐 ", fourthJPabel);

        // }
        // tabbedPane.addTab(" 主窗口  ", jpanel, null, tip);
        // tabbedPane.add(jpanel,4);
        // addListTable();
        // tabbedPane.setComponentAt(3,new
        // MusicListTable().getPanelWithTable());
        tabbedPane.setPreferredSize(new Dimension(850, 464));
        tabbedPane.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent event) {
                // 如果被选择的组件依然是空
                if (tabbedPane.getSelectedComponent() == null) {// ?????????????????????
                    // 获取所选Tab页
                    int n = tabbedPane.getSelectedIndex();
                    // 为指定标前页加载内容
                    // loadTab(n);
                }
            }
        });
        // loadTab(0);
        // addListTable();
        for (int n = 0; n < 4; n++) {
            tabbedPane.setIconAt(n, iicon);
        }
        c.add(lowerPanel, BorderLayout.NORTH); // Frame上添加Panel
        c.add(tabbedPane, BorderLayout.SOUTH);
        try {
            System.out.println("标记");
            // 向服务器请求音乐列表,第二个标签页音乐列表
            secondPanel.setMusic();
            // 设置封面图片
            // ((FirstLabel) firstPanel).setCover();
            firstPanel.setCover();
            firstPanel.setInfo();
            firstPanel.setList(firstPanel.setVectorgequa());
            // 这里没有 fourthLabel.musicList();
            // 另建立fourthLabel.musicListFlushOnceOnly();
            // 登录成功刷新一次默认音乐文件夹文件
//            fourthLabel.musicListFlushOnceOnly();
            //↓↓↓↓↓■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■下面这个是初始化这个主窗口时用来显示第四个选项卡上列表的方法■■■■■■■■■■■■■■■■■■■■■■■■■■■■
            //需用路径参数刷新列表,登录时刷新与下载后刷新必须用同一函数
            UsedToChangeMyFrame.fourthLabel.musicList("F:\\muic");//如果把这行注释掉,那么第四个选项卡就是空的
//            UsedToChangeMyFrame.fourthLabel.musicList("F:\\music\\music2");
//            UsedToChangeMyFrame.fourthLabel.repaint();
            //↑↑↑↑↑■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // System.out.println("向服务器请求音乐列表失败");
        }
    }

    public void update() {

    }

    public void recommend() {

    }

    private void addListTable() {
        tabbedPane.setComponentAt(3, new MusicListTable().getPanelWithTable());
    }

    private void loadTab(int n) {
        String title = tabbedPane.getTitleAt(n);
        // 根据标签页的标题获取对应图
        ImageIcon bookImage = new ImageIcon("F:\\icon\\iconDigit\\"
                + books.get(title));
        tabbedPane.setComponentAt(n, new JLabel(bookImage));// 将 index 位置的组件设置为
                                                            // component。如果在该索引位置没有选项卡,则会引发一个内部异常
        // 改变标签页的图标
        // tabbedPane.setIconAt(n, new ImageIcon("F:\\icon\\icona\\root.gif"));
    }

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

    public JTabbedPane getit() {
        return tabbedPane;
    }

}

// 自定义面板 继承JPanel
class MyPanel2 extends JPanel {
    public void paintComponent(Graphics g) { // 重绘函数
        Graphics2D g2 = (Graphics2D) g;
        super.paintComponent(g);
        Image img = Toolkit.getDefaultToolkit().getImage(
                "F:\\icon\\pro\\zaixian3.jpg");
        g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this); // 在面板上绘制背景图片
    }
}