简体   繁体   English

JCheckBox的ItemListener不起作用

[英]ItemListener for JCheckBox doesn't work

I'm just a beginner and I'm having a problem with implementing an item listener as an inner class. 我只是一个初学者,在将项目侦听器实现为内部类时遇到问题。

What I want to happen is that if someone selects the checkbox t1 , the text above it, written in the TextArea txtTop , becomes bold. 我要发生的是,如果有人选中复选框t1 ,则其上方的文本(写在TextArea txtTop )将变为粗体。

However, Eclipse tells me that there are multiple problems with my listener. 但是,Eclipse告诉我侦听器存在多个问题。

  1. It wants me to declare t1 and txtTop as final. 它希望我将t1和txtTop声明为final。
  2. It wants me to not make the listener class private. 它希望我不要将侦听器类设为私有。
  3. It doesn't find the listener anyway. 无论如何,它找不到侦听器。 The line t1.addItemListener(new cl()); t1.addItemListener(new cl()); doesn't work. 不起作用。

So yeah, I don't know what to do. 是的,我不知道该怎么办。 Hopefully one of you can help me out! 希望你们中的一个可以帮助我! :) :)

Here is the code: 这是代码:

import java.awt.BorderLayout;


public class WindowBuilderTest extends JFrame 
{

private JPanel contentPane;


//Launch the application.
public static void main(String[] args) 
{
    EventQueue.invokeLater(new Runnable() 
    {
        public void run() 
        {
            try 
            {
                WindowBuilderTest frame = new WindowBuilderTest();
                frame.setVisible(true);
            } catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
    });
}


// Creating Frame
public WindowBuilderTest() 
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1280, 720 );
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


    // Create Font
    Font headfont = new Font("Serif", Font.PLAIN, 15);


    // Role Headlines
    final JTextArea txtTop = new JTextArea();
    txtTop.setEditable(false);
    txtTop.setText("TOP");
    txtTop.setBounds(180, 95, 32, 23);
    txtTop.setFont(headfont);
    contentPane.add(txtTop);

    JTextArea txtMid = new JTextArea();
    txtMid.setEditable(false);
    txtMid.setText("MID");
    txtMid.setBounds(252, 95, 32, 23);
    contentPane.add(txtMid);

    JTextArea txtJng = new JTextArea();
    txtJng.setEditable(false);
    txtJng.setText("JNG");
    txtJng.setBounds(320, 95, 32, 23);
    contentPane.add(txtJng);

    JTextArea txtAdc = new JTextArea();
    txtAdc.setEditable(false);
    txtAdc.setText("ADC");
    txtAdc.setBounds(389, 95, 32, 23);
    contentPane.add(txtAdc);

    JTextArea txtSup = new JTextArea();
    txtSup.setEditable(false);
    txtSup.setText("SUP");
    txtSup.setBounds(453, 95, 32, 23);
    contentPane.add(txtSup);

    // Checkbox 1st row
    final JCheckBox t1 = new JCheckBox("");
    t1.setBounds(183, 143, 23, 23);
    t1.addItemListener(new cl());
    contentPane.add(t1);

    JCheckBox m1 = new JCheckBox("");
    m1.setBounds(255, 143, 23, 23);
    contentPane.add(m1);

    JCheckBox j1 = new JCheckBox("");
    j1.setBounds(322, 143, 23, 23);
    contentPane.add(j1);

    JCheckBox a1 = new JCheckBox("");
    a1.setBounds(393, 143, 23, 23);
    contentPane.add(a1);

    JCheckBox s1 = new JCheckBox("");
    s1.setBounds(457, 143, 23, 23);
    contentPane.add(s1);


     class cl implements ItemListener
    {
        @Override
        public void itemStateChanged(ItemEvent e) 
        {
            if(t1.isSelected())
            {
                //Font headfont = txtTop.getFont().deriveFont(Font.BOLD, 40);
                //txtTop.setFont(headfont);
                System.out.println("HURRA!");
            }
            else
            {
                //Font headfont = txtTop.getFont().deriveFont(Font.PLAIN, 40);
                //txtTop.setFont(headfont);
                System.out.println("JUHU!");
            }

        }
    }



}


}

This works for you: 这为您工作:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

public class WindowBuilderTest extends JFrame
{

    private JPanel  contentPane;

//Launch the application.
    public static void main(final String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    final WindowBuilderTest frame = new WindowBuilderTest();
                    frame.setVisible(true);
                }
                catch (final Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    // Creating Frame
    public WindowBuilderTest()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1280, 720);
        this.contentPane = new JPanel();
        this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(this.contentPane);
        this.contentPane.setLayout(null);

        // Create Font
        final Font headfont = new Font("Serif", Font.PLAIN, 15);

        // Role Headlines
        final JTextArea txtTop = new JTextArea();
        txtTop.setEditable(false);
        txtTop.setText("TOP");
        txtTop.setBounds(180, 95, 32, 23);
        txtTop.setFont(headfont);
        this.contentPane.add(txtTop);

        final JTextArea txtMid = new JTextArea();
        txtMid.setEditable(false);
        txtMid.setText("MID");
        txtMid.setBounds(252, 95, 32, 23);
        this.contentPane.add(txtMid);

        final JTextArea txtJng = new JTextArea();
        txtJng.setEditable(false);
        txtJng.setText("JNG");
        txtJng.setBounds(320, 95, 32, 23);
        this.contentPane.add(txtJng);

        final JTextArea txtAdc = new JTextArea();
        txtAdc.setEditable(false);
        txtAdc.setText("ADC");
        txtAdc.setBounds(389, 95, 32, 23);
        this.contentPane.add(txtAdc);

        final JTextArea txtSup = new JTextArea();
        txtSup.setEditable(false);
        txtSup.setText("SUP");
        txtSup.setBounds(453, 95, 32, 23);
        this.contentPane.add(txtSup);

        // Checkbox 1st row
        addCheckBox(183, 143, 23, 23, txtTop);
        addCheckBox(255, 143, 23, 23, txtMid);
        addCheckBox(322, 143, 23, 23, txtJng);
        addCheckBox(393, 143, 23, 23, txtAdc);
        addCheckBox(457, 143, 23, 23, txtSup);
    }

    private void addCheckBox(final int x, final int y, final int width, final int height, final JTextArea txtTop)
    {
        final JCheckBox checkBox = new JCheckBox();
        checkBox.setBounds(x, y, width, height);
        checkBox.addItemListener(new BoldChanger(txtTop));
        this.contentPane.add(checkBox);
    }

    class BoldChanger implements ItemListener
    {
        private JTextArea   textArea;

        public BoldChanger(final JTextArea textArea)
        {
            this.textArea = textArea;
        }

        @Override
        public void itemStateChanged(final ItemEvent e)
        {
            if (e.getStateChange() == ItemEvent.SELECTED)
            {
                final Font boldFont = this.textArea.getFont().deriveFont(Font.BOLD);
                this.textArea.setForeground(Color.RED);
                this.textArea.setFont(boldFont);
            }
            else
            {
                final Font boldFont = this.textArea.getFont().deriveFont(Font.PLAIN);
                this.textArea.setForeground(Color.BLACK);
                this.textArea.setFont(boldFont);
            }
        }
    }
}

Following problems you had: 您遇到以下问题:

  • Innerclasses definitions are not allowed inside a method (you had to move it out) 方法内部不允许使用内部类定义(您必须将其移出)
  • The final think is true if you work with anynomous classes (those one you define like this ActionListener listner = new ActionListner{ ...} . If you have a class like this, you can access the member variable. 最后的想法是正确的,如果您使用的是任意类(像这样定义的类,就是ActionListener listner = new ActionListner{ ...} 。如果您拥有这样的类,则可以访问成员变量。

I renamed your ItemListner to BoldChanger . 我将您的ItemListner重命名为BoldChanger It gets the TextArea to change in the constructor. 它获取要在构造函数中更改的TextArea。

Btw: BTW:

  • CARRY 携带
  • JUNGLER ONLY 仅丛林
  • TANK
  • SUPPORT 支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM