构造函数JCheckBox(String)未定义

时间:2018-09-04 15:22:01

标签: java constructor jcheckbox

我遵循了JCheckbox的https://www.youtube.com/watch?v=3RQOikbGGUM&list=PLFE2CE09D83EE3E28&index=63的javatutorial。除了变量和类的名称以外,其他所有内容都是相同的,但是尽管他没有错误,但是当我设置cb1 = new JCheckBox(“ bold”)时,我的这个“构造函数JCheckBox(String)未定义”;因此,所有这些都是错误的。我正在使用Eclipse IDE

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

public class subjcb extends JFrame{
    private JTextField tf;
    private JCheckBox cb1;
    private JCheckBox cb2;

public subjcb(){
    super("title");
    setLayout(new FlowLayout());

    /*the textbox*/
    tf=new JTextField("This is a sentence", 20);
    tf.setFont(new Font("Serif", Font.PLAIN, 14));
    add(tf);

    cb1 = new JCheckBox("bold");
    cb2 = new JCheckBox("italic");
    add(cb1);
    add(cb2);

    HandlerClass handler = new HandlerClass();
    cb1.addItemListener(handler);
    cb2.addItemListener(handler);

}

private class HandlerClass implements ItemListener{ /*a class inside another class could see the all variables inside the class it is in*/
    public void itemStateChanged(ItemEvent event){ /* a check box is an item, and when we click it, an event will occur and we could define something will happen due to this event*/
        Font font = null;
        if(cb1.isSelected() && cb2.isSelected())
            font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
        else if(cb1.isSelected())
            font = new Font("Serif", Font.BOLD, 14);
        else if(cb2.isSelected())
            font = new Font("Serif", Font.ITALIC, 14);
        else
            font = new Font("Serif", Font.PLAIN, 14);

        tf.setFont(font);/*after checking through the options and decided what should the output be, this will make the words on the window look like that out put*/
    }
}

}

0 个答案:

没有答案