在选择面板之前,列表选择侦听器不会显示

时间:2014-05-12 21:54:47

标签: java user-interface jsplitpane

以下代码在左侧显示列表,在右侧显示内容。因此,当选择列表中的项目时,它应该在右侧显示一些内容(这使用拆分窗格)。问题是,当选择某个项目时它不会显示,而是在您单击分隔符时显示它。我想知道以下代码有什么问题,它不会自动显示内容时选择列表中的项目。提前谢谢。

下面显示了我遇到麻烦的部分代码:

public class FrequentQuestions implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    frame = new MyInternalFrame("Frequently Asked Questions");

    String [] options = {"How to open/save images", "formats", "info"};
    JList list = new JList(options);
    panelQuestions.add(list);
    list.addListSelectionListener(new ListSelectionListener(){

        @Override
        public void valueChanged(ListSelectionEvent e) {
            // TODO Auto-generated method stub
            if(e.getValueIsAdjusting())
                return;

            //String [] options2 = {"hello","bye"};
            //JList list2 = new JList(options2);
            JList list = (JList) e.getSource();


            if (list.isSelectionEmpty()) {
                System.out.println("list selection is empty!");
            }
            else{
                int index = ((JList)e.getSource()).getSelectedIndex();
                if(index == 0){
                    readFile();
                }
                else if(index == 1){
                    System.out.println("2nd item selected");

                }
                else{
                    System.out.println("3rd item selected");
                }
            }
        }

    });

    scroll = new JScrollPane(panelAnswers,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelQuestions, scroll);
    pane.setAutoscrolls(true);
    pane.setOpaque(true);

    panelQuestions.setMinimumSize(new Dimension(200,50));
    panelQuestions.setBackground(new Color(0,0,0,0));
    panelAnswers.setMinimumSize(new Dimension(100,30));
    pane.setOneTouchExpandable(true);
    pane.setDividerLocation(250);

    frame.add(pane);
    frame.setVisible(true);
    desk.add(frame);

     try {
            frame.setSelected(true);
    } catch (java.beans.PropertyVetoException e1) {

    }

}
 }

这是完整的代码(它可能无法编译,因为我有文本文件,我不知道这是否有用):

 public class FrequentQuestions implements ActionListener{

private int openFrameCount;
private JDesktopPane desk;
private JTextArea Tarea;
private JScrollPane scroll;
private BufferedReader in ;
JPanel panelQuestions = new JPanel();
JPanel panelAnswers = new JPanel();
JTextArea text = new JTextArea();
//private FrequentQuestions quest;
JSplitPane pane ;
MyInternalFrame frame;

public FrequentQuestions(JDesktopPane desktop) {
    // TODO Auto-generated constructor stub
    desk = desktop;
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    frame = new MyInternalFrame("Frequently Asked Questions");

    String [] options = {"How to open/save images", "formats", "Information"};
    JList list = new JList(options);
    //panelQuestions.add(list);
    list.addListSelectionListener(new ListSelectionListener(){

        @Override
        public void valueChanged(ListSelectionEvent e) {
            // TODO Auto-generated method stub
            if(e.getValueIsAdjusting() == false)
                return;

            String [] options2 = {"hello","bye"};
            JList list2 = new JList(options2);
            JList list = (JList) e.getSource();


            if (list.isSelectionEmpty()) {
                System.out.println("list selection is empty!");
            }
            else{
                int index = ((JList)e.getSource()).getSelectedIndex();
                if(index == 0){
                    readFile();
                    System.out.println("I am outputting!");
                }
                else if(index == 1){
                    readFormatFile();
                    System.out.println("2nd item selected");

                }
                else{
                    System.out.println("3rd item selected");
                }
            }
        }

    });

    JScrollPane scroll1 = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scroll = new JScrollPane(panelAnswers,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scroll1, scroll);
    pane.setAutoscrolls(true);
    pane.setOpaque(true);

    panelQuestions.setMinimumSize(new Dimension(259,50));
    panelQuestions.setBackground(new Color(0,0,0,0));
    panelAnswers.setMinimumSize(new Dimension(340,30));
    pane.setOneTouchExpandable(true);
    pane.setDividerLocation(290);


    frame.add(pane);
    frame.setVisible(true);
    desk.add(frame);

     try {
            frame.setSelected(true);
    } catch (java.beans.PropertyVetoException e1) {

    }

}

private class MyInternalFrame extends JInternalFrame {

    static final int xPosition = 30, yPosition = 30;
    public MyInternalFrame(String title) {
        super(title, true,true, true, true);
        setSize(600,500);

        // Set the window's location.
        setLocation(xPosition * openFrameCount, yPosition * openFrameCount);
    }
}

public void readFile(){
    panelAnswers.removeAll();
    try {
        in = new BufferedReader(new FileReader("openSave.txt"));
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    String line;
    String file = ""; 
    try {
        while((line = in.readLine()) != null)
        {
            System.out.println(line);
            file += line;
            file +="\n";

        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    JTextArea Tarea = new JTextArea();
    //System.out.println(file);

    Tarea.setText(file);
    Font f = new Font("TimesNewRoman", Font.ROMAN_BASELINE, 12);
    Tarea.setFont(f);
    Tarea.setBackground(new Color(0,0,0,0));
    Tarea.setEditable(false);
    panelAnswers.add(Tarea);

}

  public void readFormatFile(){
    panelAnswers.removeAll();
    try {
        in = new BufferedReader(new FileReader("format.txt"));
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    String line;
    String file = ""; 
    try {
        while((line = in.readLine()) != null)
        {
            System.out.println(line);
            file += line;
            file +="\n";

        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    JTextArea Tarea = new JTextArea();
    //System.out.println(file);

    Tarea.setText(file);
    Font f = new Font("TimesNewRoman", Font.ROMAN_BASELINE, 12);
    Tarea.setFont(f);
    Tarea.setBackground(new Color(0,0,0,0));
    Tarea.setEditable(false);
    panelAnswers.add(Tarea);

}
 }

1 个答案:

答案 0 :(得分:0)

这是一个有效的ListSelectionListener

public class FrequentQuestions extends JFrame {

    final JLabel label = new JLabel("TEXT");

    public FrequentQuestions() {

        JList<String> list = new JList<String>(new String[]{"A", "B"});
        list.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {

                if (e.getValueIsAdjusting())
                    return;
                JList<String> list = (JList<String>) e.getSource();
                label.setText(list.getSelectedValue());
            }
        });

        JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list, label);
        add(split);

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {

        new FrequentQuestions();
    }
}