1
public class MainMenu extends JFrame {
    private JPanel panel,file_list_panel;
    private JPanel contentPane;
    private JMenuBar menuBar;
    private JMenu mnNewMenu1,mnNewMenu2,mnNewMenu3;
    private JMenuItem mt1,mt2,mt3;
    private JPanel right,left ,bottom;
    private JSplitPane spver ,sphor;
    private JTabbedPane tabbedPane;
    private JLabel label;
    private JList<String> list_1;
    private JScrollPane jscroll_list;
    private DefaultListModel listmodel_1 = new DefaultListModel();


    public MainMenu() {




    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(0,0,screenSize.width, screenSize.height);
    setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
    int intValue = Integer.parseInt( "EEEEEE",16);
    Color aColor = new Color( intValue );
    UIManager.put("TabbedPane.background", new Color(230, 216, 174));
    UIManager.put("TabbedPane.selected", Color.WHITE); 
    UIManager.put("TabbedPane.contentAreaColor",aColor ); 
    UIManager.put("TabbedPane.focus", Color.WHITE); 

    setTitle("Main Menu");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //setBounds(100, 100, 848, 680);
    //setLocation(10, 10);
    setLocationRelativeTo(null); // set jFrame alignment to center

    //parent(first) panel 
    contentPane = new JPanel();
    contentPane.setBackground(Color.BLACK);
    contentPane.setBorder(new EmptyBorder(2, 2, 2, 2));
    setContentPane(contentPane);
    contentPane.setLayout(new BorderLayout(0, 0));

    //Main Menu BAR
    menuBar = new JMenuBar();
    menuBar.setFont(new Font("Tekton Pro Ext", Font.PLAIN, 11));
    setJMenuBar(menuBar);

    //Menu 1
    mnNewMenu1 = new JMenu("New menu");
    menuBar.add(mnNewMenu1);
    //Menu 1 MenuItem 1

    mt1 = new JMenuItem("Browse New Project");
    mt1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
        BrowseScreen frame = new BrowseScreen();
        frame.setVisible(true);
        }


    });


    mnNewMenu1.add(mt1);




    //second panel
    panel = new JPanel();
    contentPane.add(panel, BorderLayout.CENTER);
    panel.setLayout(new BorderLayout(0, 0));

    spver =new  JSplitPane(JSplitPane.VERTICAL_SPLIT);
    spver.setDividerLocation(500);
    spver.setEnabled(false);

    bottom=new JPanel();
    sphor =new  JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    sphor.setEnabled(false);
    spver.setTopComponent(sphor);
    spver.setBottomComponent(bottom);
    bottom.setLayout(null);
    panel.add(spver);

    sphor.setDividerLocation(180);
    left =new JPanel();
    right =new JPanel();
    sphor.setRightComponent(right);
    right.setLayout(new GridLayout(0, 1, 0, 0));

    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

    JPanel tab1 = new JPanel();
    tabbedPane.addTab("fffa", tab1);
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

    JPanel tab2 = new JPanel();
    tabbedPane.addTab("TAB1", tab2);
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);



    right.add(tabbedPane);
    sphor.setLeftComponent(left);
    left.setLayout(new BorderLayout(0, 0));

    file_list_panel= new JPanel();
    file_list_panel.setBackground(Color.BLACK);
    file_list_panel.setForeground(Color.WHITE);

    label = new JLabel("Java Files of Project");
    label.setBackground(Color.BLACK);
    label.setForeground(Color.WHITE);
    label.setFont(new Font("Garamond", Font.BOLD, 14));
    file_list_panel.add(label);
    left.add(file_list_panel, BorderLayout.NORTH);


    list_1 = new JList<String>(listmodel_1);



    jscroll_list = new JScrollPane(list_1);
    left.add(jscroll_list, BorderLayout.CENTER);


    }
    public  void setList(Vector<String> files){
        listmodel_1.removeAllElements();
        list_1.removeAll();
        for(int i=0;i<files.size();i++)
            listmodel_1. addElement(files.elementAt(i));
        list_1.setModel(listmodel_1);
        this.invalidate();
        this.validate();
        this.repaint();
    }

}

setList 在调用 setVisible(false) 之前从浏览窗口调用;IE 。当浏览窗口消失时,此方法被调用.. 它在方法中执行所有操作,但不在 MainMenu 中更新它 public void setFileList(){ MainMenu mm= new MainMenu(); mm.setList(java_files); }

list_1 -JList listmodel_1=DefaultListModel

我尝试刷新框架,但在将新列表添加到主窗口后不刷新...

在更新 JList 之前浏览另一个窗口中的文件然后将其设置为 setVisible(false) 然后 MainMenu 获得焦点并调用 setList 方法但它没有改变

4

2 回答 2

5

您正在创建一个新JList<String>的并将引用复制到您的实例变量 - 但您既没有JList从框架中删除旧的,也没有添加新的。仅更改变量不会这样做。

与其创建一个新的JList<String>,不如直接替换现有的模型?删除这一行:

list_1=new JList<String>(listmodel_1);

你可能会发现它确实有效。(您已经在后续行中设置模型...)

于 2013-08-07T07:01:55.267 回答
4

基本上,您正在创建一个新JList模型并将您的模型与它相关联,但这JList对屏幕上的模型没有影响

public  void setList(Vector<String> files){
    // Good...
    listmodel_1.removeAllElements();
    // Not required, has nothing to do with the items in the list
    //list_1.removeAll();
    // Good
    for(int i=0;i<files.size();i++)
        listmodel_1. addElement(files.elementAt(i));
    // This here is your problem
    //list_1=new JList<String>(listmodel_1);
    // Because I have no idea what model list_1 is actually using...
    list_1.setModel(listmodel_1);
    //list_1.setSelectedIndex(0);
    //this.invalidate();
    //this.validate();
    //this.repaint();
}
于 2013-08-07T07:02:52.040 回答