ActionListener不跨方法链接列表

时间:2016-07-13 21:03:54

标签: java swing

我对编程很新,但是我已经阅读了本书的一半"首先进入java"。它显示了一个我试图写下来并编译但我无法通过编译器的例子。我的valueChanged方法不会将列表从go()链接到" String selection =(String)list.getSelectedValue(); "列表"用红线加下划线,我似乎无法获得正确的语法来将主语法与我的听众联系起来。任何帮助都会得到满足。 `

package Test1;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;

public class GUi extends JPanel implements ListSelectionListener {

    public static void main(String [] args){
        GUi gui = new GUi();
        gui.go();


    }

    public void go() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setBackground(Color.DARK_GRAY);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        JList list = new JList();
        String[] listEntries = {"alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta"};

        list = new JList(listEntries);

        JScrollPane scroller = new JScrollPane(list);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        panel.add(scroller);

        list.setVisibleRowCount(4);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.addListSelectionListener(this);

        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    public void valueChanged(ListSelectionEvent lse) {
        if (!lse.getValueIsAdjusting()) {
            String selection = (String) list.getSelectedValue();
            System.out.println(selection);
        }
    }

}

`

1 个答案:

答案 0 :(得分:0)

只需将列表添加为类字段,这样两种方法都可以这样显示

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;

public class GUi extends JPanel implements ListSelectionListener {

    JList list;
    public static void main(String [] args){
        GUi gui = new GUi();
        gui.go();


    }

    public void go() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setBackground(Color.DARK_GRAY);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        String[] listEntries = {"alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta"};

        list = new JList(listEntries);

        JScrollPane scroller = new JScrollPane(list);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        panel.add(scroller);

        list.setVisibleRowCount(4);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.addListSelectionListener(this);

        frame.add(panel);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    public void valueChanged(ListSelectionEvent lse) {
        if (!lse.getValueIsAdjusting()) {
            String selection = (String) list.getSelectedValue();
            System.out.println(selection);
        }
    }

}