2012-11-07 15 views
1

我已经编写了一个程序,它假设设置了应用程序属性。我正在使用JSplit Pane和JScrollPane。程序加载正常,但不会响应左侧的列表元素更改右侧面板的值。请让我知道如果我做错了什么。我已经refered following programJScrollPane不会将值更改为ListEvents

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JDialog; 
import javax.swing.JList; 
import javax.swing.JScrollPane; 
import javax.swing.JSplitPane; 
import javax.swing.JTabbedPane; 
import javax.swing.ListSelectionModel; 
import javax.swing.event.ListSelectionEvent; 
import javax.swing.event.ListSelectionListener; 

public class ApplicationPropertyDlg extends JDialog implements ActionListener,ListSelectionListener { 

    private DButton  pb_OK   = null ; 
    private DButton  pb_CANCEL  = null ; 
    private DButton  pb_APPLY  = null ; 
    private String[]   appProp  = null; 
    private JList   appList  = null; 
    private JSplitPane  appPanel = null; 
    private JScrollPane  listScrollPanel,appScrollPanel = null; 

    public ApplicationPropertyDlg (AppDefaultWin parent) { 
     super (parent, "Properties Application", true) ; 
     initializeAppProp(); 
     intializeAppList(); 
     initializeGUI(); 
     ButtonPanel buttonPanel = new ButtonPanel() ; 
     setSize (800,700) ; 
     WinUtil.centerChildInParent (this, parent) ;   
     pb_OK  = new JButton () ; 
     pb_APPLY = new JButton () ; 
     pb_CANCEL = new JButton () ; 

     pb_OK.addActionListener (this) ; 
     pb_APPLY.addActionListener (this) ; 
     pb_CANCEL.addActionListener (this) ; 
     GUISystem.setPreferredButton (pb_OK) ; 
     GUISystem.setPreferredButton (pb_CANCEL) ; 
     GUISystem.setPreferredButton (pb_APPLY) ; 
     getContentPane().setLayout (new BorderLayout (5,5)) ; 
     getContentPane().add(appPanel); 
     getContentPane().add (buttonPanel, BorderLayout.SOUTH) ; 
     buttonPanel.add (pb_OK) ; 
     buttonPanel.add (pb_APPLY) ; 
     buttonPanel.add (pb_CANCEL) ;         
     setVisible (true) ; 
    } 

    private void initializeGUI() { 
    // TODO Auto-generated method stub 
     listScrollPanel = new JScrollPane(appList); 
     appScrollPanel = new JScrollPane(new GeneralPage()); 
     appPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,listScrollPanel,appScrollPanel); 
     appPanel.setOneTouchExpandable(true); 
     appPanel.setDividerLocation(200); 
     //minimum size for individual Panel 
     Dimension minimumSize = new Dimension(100, 50); 
     listScrollPanel.setMinimumSize(minimumSize); 
     appScrollPanel.setMinimumSize(minimumSize); 
     //Provide a preferred size for the split pane. 
     appPanel.setPreferredSize(appPanel.getPreferredSize());  

} 


private void intializeAppList() { 
    // TODO Auto-generated method stub 
     appList = new JList(appProp); 
     appList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 
     appList.setSelectedIndex(0); 
     appList.addListSelectionListener(this); 


} 





private void initializeAppProp() { 
    // TODO Auto-generated method stub 
    appProp = new String []{"General","Task Bar", "Look and Feel","Country"}; 
} 

    public void propertyChanged (int property, Object value) { 
     if (property == PropertySystem.PROPERTY_LANGUAGE) 
     setText() ; 
     else if (property == PropertySystem.PROPERTY_LAF) 
     GUISystem.setLookAndFeel (this) ; 
     else 
     GUISystem.setPropertiesOnPanel (getContentPane()) ; 
    } 




    public void actionPerformed (ActionEvent e) { 
     dispose() ; 
    } 



@Override 
public void valueChanged(ListSelectionEvent e) { 
    // TODO Auto-generated method stub 
    JList list = (JList)e.getSource(); 
    updateAppPanel(appProp[list.getSelectedIndex()]); 

} 

private void updateAppPanel(String panelName) { 
    // TODO Auto-generated method stub 
    if(panelName.equalsIgnoreCase("General"){ 
     appScrollPanel.removeAll(); 
     appScrollPanel.add(new GeneralPage()); 
    } 
    else if (panelName.equalsIgnoreCase("Task Bar"){ 
     appScrollPanel.removeAll(); 
     appScrollPanel.setViewportView(new TaskBarPage()); 
    } 
    else if (panelName.equalsIgnoreCase("Language"){ 
     appScrollPanel.removeAll(); 
     appScrollPanel.setViewportView(new LanguagePage()); 
    } 
    else if (panelName.equalsIgnoreCase("Look and Feel"){ 
     appScrollPanel.removeAll(); 
     appScrollPanel.setViewportView(new LookFeelPage()); 
    } 
    else if (panelName.equalsIgnoreCase("Country"){ 
     appScrollPanel.removeAll(); 
     appScrollPanel.setViewportView(new SelectCountryPage()); 
    } 
    appScrollPanel.revalidate(); 
    appScrollPanel.repaint(); 
    } 
} 
+0

''“程序未加载正常”',您的意思是''程序加载正常吗? – higuaro

+0

是程序加载正常,它只是不会改变侦听的响应值 – Ashish

回答

3

首先,添加一个新的控制容器到类(优选一个JPanel):

... 
    private JSplitPane  appPanel = null; 
    private JScrollPane  listScrollPanel,appScrollPanel = null; 
    // End of your controls... 

    private JPanel tabContainer; 

然后,在代码部分,在那里代码初始化appScrollPanel变量,而不是传递GeneralPane的新实例,通过tabContainer这样的:

 tabContainer = new JPanel(); 
    tabContainer.setLayout(new CardLayout()); 
    tabContainer.add(new GeneralPage()); 
    // Instead of adding GeneralPane directly, add the GeneralPane container 
    appScrollPanel = new JScrollPane(tabContainer); 

然后,在updateAppPanel方法,更换appScrollPanel.setViewportView调用是这样的:

private void updateAppPanel(String panelName) { 
    // TODO Auto-generated method stub 
    if(panelName.equalsIgnoreCase("General")){ 
     tabContainer.removeAll(); 
     tabContainer.add(new GeneralPage()); 
    } 
    else if (panelName.equalsIgnoreCase("Task Bar")){ 
     tabContainer.removeAll(); 
     tabContainer.add(new TaskBarPage()); 
    } 
    else if (panelName.equalsIgnoreCase("Language")){ 
     tabContainer.removeAll(); 
     tabContainer.add(new LanguagePage()); 
    } 
    else if (panelName.equalsIgnoreCase("Look and Feel")){ 
     tabContainer.removeAll(); 
     tabContainer.add(new LookFeelPage()); 
    } 
    else if (panelName.equalsIgnoreCase("Country")){ 
     tabContainer.removeAll(); 
     tabContainer.add(new CountryPage()); 
    } 
    appScrollPanel.revalidate(); 
    appScrollPanel.repaint(); 
} 

,它应该工作:

enter image description here

enter image description here

我建议你把updateAppPanel代码的循环里面?如果您需要修改每个else if区块的内部主体

+0

添加一个建议使用[CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html)和你有自己一个非常好的答案 – MadProgrammer

+0

@MadProgrammer谢谢!,的确,我在测试程序中使用了'CardLayout',谢谢你指出,我在编辑答案! – higuaro

+0

反正你已经得到了一个+1 – MadProgrammer