Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JApplet;
- import javax.swing.JButton;
- import javax.swing.JCheckBox;
- import javax.swing.JLabel;
- import javax.swing.JRadioButton;
- public class Applet2017a extends JApplet implements ActionListener {
- // поля = глобальные переменные
- JButton ok;
- JButton cancel;
- JCheckBox[] checkBox;
- JRadioButton radioButton;
- JLabel label;
- {
- // инициализация
- // ключевое слово this - наш апплет
- //this.setLayout(new FlowLayout()); // конкретный способ расположения
- this.setLayout(null); // мы сами будем всё располагать
- ok = new JButton("OK");
- ok.setBounds(10, 10, 90, 40);
- this.add(ok);
- cancel = new JButton("Cancel");
- this.add(cancel);
- checkBox = new JCheckBox[5];
- for (int i = 0; i < checkBox.length; i++) {
- checkBox[i] = new JCheckBox("" + i);
- this.add(checkBox[i]);
- checkBox[i].setBounds(30 * i, 60, 20, 20);
- }
- radioButton = new JRadioButton("radiobutton");
- label = new JLabel("Some text");
- this.add(radioButton);
- this.add(label);
- ok.addActionListener(this);
- cancel.addActionListener(this);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // работаем с e.getSource()
- if (e.getSource() == ok) {
- label.setText("OK was pressed");
- checkBox[0].setSelected(!checkBox[0].isSelected());
- }
- if (e.getSource() == cancel) {
- label.setText("Cancel was pressed");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement