Я следовал javatutorial https://www.youtube.com/watch?v=3RQOikbGGUM&list=PLFE2CE09D83EE3E28&index=63 JCheckbox. Кроме имен переменных и классов, все то же самое, но, хотя у него нет ошибки, у меня есть это «Конструктор JCheckBox (String) не определен», когда я устанавливаю cb1 = new JCheckBox («полужирный»); и последующие все не правы из-за этого. Я использую 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*/
}
}
}