首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java JCheckBox ActionListener

Java JCheckBox ActionListener
EN

Stack Overflow用户
提问于 2013-11-17 09:04:26
回答 1查看 1.2K关注 0票数 0

我有一个程序,这是一个可以由用户添加的笔记列表。每个注释都是通过一个for循环加载的,并在运行时从notes.txt获得一个JCheckBox。当我的JCheckBoxes只基于实例而不是永久代码时,我该如何为它们添加一个动作侦听器呢?

(例如,它们没有自己的变量名?)

我需要根据JCheckBox是否被选中来使用0或1来更新notes.txt。下面是我的代码:

代码语言:javascript
复制
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class SideNotes {

public static JPanel panel = new JPanel();
private static JButton add = new JButton("Add note");
JCheckBox[] notes;

public SideNotes() {
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.add(add);
    loadNotes();
    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                addNote();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });
}

public void addNote() throws IOException {
    String note = JOptionPane.showInputDialog("Enter note: ", null);
    JCheckBox jcb = new JCheckBox(note, false);
    panel.add(jcb);
    panel.revalidate();
    panel.repaint();

    File file = new File("notes.txt");
    FileWriter writer = new FileWriter(file, true);
    BufferedWriter brw = new BufferedWriter(writer);

    brw.write(note);
    brw.newLine();
    brw.close();
}

private void loadNotes() {
    File file = new File("notes.txt");
    if (file.exists()) {
        try {
            FileInputStream fs = new FileInputStream(file);
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(fs));

            BufferedReader reader = new BufferedReader(new FileReader(
                    "notes.txt"));
            int lines = 0;
            while (reader.readLine() != null)
                lines++;
            reader.close();
            notes = new JCheckBox[lines];

            for (int i = 0; i < notes.length; i++) {
                String note = br.readLine();
                int checked = note.charAt(note.length() - 1);
                System.out.println(checked);
                if (checked == 49) {
                    note = note.substring(0, note.length() - 1);
                    notes[i] = new JCheckBox(note, true);   
                } else {
                    note = note.substring(0, note.length() - 1);
                    notes[i] = new JCheckBox(note, false);
                }
                panel.add(notes[i]);
                panel.revalidate();
                panel.repaint();
            }
            br.close();

        } catch (Exception e1) {
        }
    } else {
        System.out.println("File does not exist");
    }
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(200, 400);
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.add(add);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    new SideNotes();
}

}
EN

回答 1

Stack Overflow用户

发布于 2013-11-17 09:40:56

在创建复选框的循环外部创建通用ActionListener:

代码语言:javascript
复制
ActionListener al = new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        JCheckBox checkbox = (JCheckBox)e.getSource();
        // do something with the checkbox
    }
}

然后,在创建复选框后,将ActionListener添加到复选框中

代码语言:javascript
复制
notes[i].addActionListener(al);
panel.add(notes[i]);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20025950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档