import javax swing import java awt event public
import javax. swing. *; import java. awt. event. *; public class Fruits. And. Vegetables extends JFrame implements Item. Listener{ private JPanel panel; private JCheck. Box fruits; private JCheck. Box vegetables; private JText. Area area; private JScroll. Pane scroll; private String[] fruit. Names = {"Портокали", "Банани", "Ябълки"}; private String[] vegetable. Names = {"Домати", "Чушки", "Зеле"}; public Fruits. And. Vegetables() { super("Плодове и зеленчуци"); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); Container container = get. Content. Pane(); panel = new JPanel(); fruits = new JCheck. Box("Плодове"); vegetables = new JCheck. Box("Зеленчуци"); area = new JText. Area(5, 10); scroll = new JScroll. Pane(area, JScroll. Pane. VERTICAL_SCROLLBAR_ALWAYS, JScroll. Pane. HORIZONTAL_SCROLLBAR_ALWAYS);
panel. add(fruits); panel. add(vegetables); container. add(panel, Border. Layout. PAGE_START); container. add(scroll, Border. Layout. CENTER); fruits. add. Item. Listener(this); vegetables. add. Item. Listener(this); set. Size(250, 200); set. Visible(true); } // Извиква се автоматично при вдигане на събитието „избор на елемент“ // за бутоните за избор fruits и vegetables public void item. State. Changed(Item. Event e) { area. set. Text(""); if (fruits. is. Selected()) { for (int i = 0; i < fruit. Names. length; i++) area. append(fruit. Names[i] + "n"); } if (vegetables. is. Selected()) { for (int i = 0; i < vegetable. Names. length; i++) area. append(vegetable. Names[i] + "n"); } }
public static void main(String[] args) { Event. Queue. invoke. Later(new Runnable() { public void run() { new Fruits. And. Vegetables(); } }
Пример: Приложение, в което чрез радио бутони се изобразяват плодове или зеленчуци. Използва събитието Action. Event за JRadio. Button. JPanel panel JText. Area area JRadio. Button fruits JRadio. Button vegetables Button. Group group
import javax. swing. *; import java. awt. event. *; import javax. swing. border. *; public class Fruits. And. Vegetables. With. Radio. Buttons extends JFrame implements Action. Listener { private JPanel panel; private Button. Group group; private JRadio. Button fruits; private JRadio. Button vegetables; private JText. Area area; private JScroll. Pane scroll; private String[] fruit. Names = {"Портокали", "Банани", "Ябълки"}; private String[] vegetable. Names = {"Домати", "Чушки", "Зеле"}; public Fruits. And. Vegetables. With. Radio. Buttons() { super("Плодове и зеленчуци"); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); Container container = get. Content. Pane(); group = new Button. Group(); fruits = new JRadio. Button("Плодове");
panel = new JPanel(new Grid. Layout(0, 1)); panel. set. Border(new Titled. Border(new Etched. Border(), "Избери")); area = new JText. Area(5, 10); JScroll. Pane scroll = new JScroll. Pane(area, JScroll. Pane. VERTICAL_SCROLLBAR_ALWAYS, JScroll. Pane. HORIZONTAL_SCROLLBAR_ALWAYS); group. add(fruits); group. add(vegetables); panel. add(fruits); panel. add(vegetables); container. add(panel, Border. Layout. LINE_START); container. add(scroll, Border. Layout. CENTER); fruits. add. Action. Listener(this); vegetables. add. Action. Listener(this); set. Size(250, 200); set. Visible(true); }
// Извиква се автоматично при вдигане на събитието „натискане на // бутон“ за радио бутоните fruits и vegetables public void action. Performed(Action. Event e) { area. set. Text(""); if (fruits. is. Selected()) for (int i = 0; i < fruit. Names. length; i++) area. append(fruit. Names[i] + "n"); else if (vegetables. is. Selected()) for (int i = 0; i < vegetable. Names. length; i++) area. append(vegetable. Names[i] + "n"); } public static void main(String[] args) { Event. Queue. invoke. Later(new Runnable() { public void run() { new Fruits. And. Vegetables. With. Radio. Buttons(); } }
import javax. swing. *; import java. awt. event. *; public class Total. Sum extends JFrame implements Action. Listener, Item. Listener{ private JPanel panel 1; private JCombo. Box item; private String[] names = {"Портокали", "Банани", "Ябълки", "Домати", "Чушки", "Зеле"}; private float[] prices = {1. 40 f, 1. 10 f, 2. 50 f, 2. 80 f, 3. 50 f, 0. 69 f}; private JLabel l 1, l 2, l 3, l 4, l 5; private JText. Field f. Price, f. Quantity, f. Result, f. Total; private JPanel panel 2; private JButton button; private int i; // Индекс на избрания продукт private float price; // Цена на избрания продукт private float total; // Обща сума public Total. Sum() { super("Плод-зеленчук"); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); Container container = get. Content. Pane();
panel 1 = new JPanel(); panel 1. set. Layout(new Grid. Layout(5, 2)); item = new JCombo. Box(); for (int i = 0; i < names. length; i++) item. add. Item(names[i]); i = 0; price = prices[i]; total = 0. 0 f; l 1 = new JLabel("Избери: "); l 2 = new JLabel("Цена: "); l 3 = new JLabel("Количество: "); l 4 = new JLabel("Общо: "); l 5 = new JLabel("Обща сума: "); f. Price = new JText. Field(10); f. Price. set. Text(""+price); f. Quantity = new JText. Field(10); f. Result = new JText. Field(10); f. Total = new JText. Field(10); panel 2 = new JPanel(); button = new JButton("Изчисли");
panel 1. add(l 1); panel 1. add(item); panel 1. add(l 2); panel 1. add(f. Price); panel 1. add(l 3); panel 1. add(f. Quantity); panel 1. add(l 4); panel 1. add(f. Result); panel 1. add(l 5); panel 1. add(f. Total); panel 2. add(button); container. add(panel 1, Border. Layout. CENTER); container. add(panel 2, Border. Layout. PAGE_END); item. add. Item. Listener(this); button. add. Action. Listener(this); set. Size(300, 200); set. Visible(true); }
public void action. Performed(Action. Event e) { float k = Float. parse. Float(f. Quantity. get. Text()); total += price*k; f. Result. set. Text(""+price*k); f. Total. set. Text(""+total); } public void item. State. Changed(Item. Event e) { i = item. get. Selected. Index(); price = prices[i]; f. Price. set. Text(""+price); f. Quantity. set. Text(""); f. Result. set. Text(""); f. Total. set. Text(""); } public static void main(String[] args) { Event. Queue. invoke. Later(new Runnable() { public void run() { new Total. Sum(); } } // Количество // Обща сума
- Slides: 28