11/08/2018, 19:45

Choice trong Java AWT

Previous Đối tượng của lớp Choice được sử dụng để hiển thị menu popup của các lựa chọn. Lựa chọn do người dùng lựa chọn được hiển thị ở đầu trình đơn. Nó kế thừa lớp Component. Khai báo lớp AWT Choice public class Choice extends Component implements ItemSelectable, ...

Previous

Đối tượng của lớp Choice được sử dụng để hiển thị menu popup của các lựa chọn. Lựa chọn do người dùng lựa chọn được hiển thị ở đầu trình đơn. Nó kế thừa lớp Component.

Khai báo lớp AWT Choice

public class Choice extends Component implements ItemSelectable, Accessible

Ví dụ Choice trong Java AWT

package vn.viettuts.awt;

import java.awt.Choice;
import java.awt.Frame;

public class ChoiceExample1 {
    public ChoiceExample1() {
        Frame frame = new Frame("Ví dụ Java AWT Choice");
        Choice choice = new Choice();
        choice.setBounds(100, 100, 150, 150);
        choice.add("C++");
        choice.add("Java");
        choice.add("PHP");
        choice.add("Python");
        choice.add("C#");
        frame.add(choice);
        frame.setSize(400, 250);
        frame.setLayout(null);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        new ChoiceExample1();
    }
}

Kết quả:

Ví dụ Choice trong Java AWT

Ví dụ Choice trong Java AWT với ActionListener

package vn.viettuts.awt;

import java.awt.Button;
import java.awt.Choice;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChoiceExample2 {
    public ChoiceExample2() {
        Frame frame = new Frame("Ví dụ Java AWT Choice");
        final Label label = new Label();
        label.setAlignment(Label.CENTER);
        label.setSize(400, 100);
        Button button = new Button("Show");
        button.setBounds(200, 100, 50, 20);
        final Choice choice = new Choice();
        choice.setBounds(100, 100, 75, 75);
        choice.add("C++");
        choice.add("Java");
        choice.add("PHP");
        choice.add("Python");
        choice.add("C#");
        frame.add(choice);
        frame.add(label);
        frame.add(button);
        frame.setSize(400, 250);
        frame.setLayout(null);
        frame.setVisible(true);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String data = "Ngôn ngữ lập trình được chọn: " +
                        choice.getItem(choice.getSelectedIndex());
                label.setText(data);
            }
        });
    }

    public static void main(String args[]) {
        new ChoiceExample2();
    }
}

Kết quả:

Ví dụ Choice trong Java AWT
Previous
0