all types of buttons
Wed Feb 21 2024 08:48:31 GMT+0000 (Coordinated Universal Time)
Saved by @dsce
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LabelDemo { private JFrame frame; private JLabel label; public LabelDemo() { frame = new JFrame("A Simple Swing App"); Toolkit tk = frame.getToolkit(); Dimension dim = tk.getScreenSize(); int width = (int)dim.getWidth(); int height = (int)dim.getHeight(); frame.setSize(width, height); frame.setLayout(new FlowLayout()); ImageIcon ic = new ImageIcon("Animated_butterfly.gif"); label = new JLabel("A Butterfly", ic, JLabel.CENTER); label.setFont(new Font("Verdana", Font.BOLD, 18)); label.setBackground(Color.yellow); frame.add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new LabelDemo(); } } ButtonDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonDemo { private JFrame frame; private JLabel label; private JButton b1, b2; public ButtonDemo() { frame = new JFrame("A Simple Swing App"); Toolkit tk = frame.getToolkit(); Dimension dim = tk.getScreenSize(); int width = (int)dim.getWidth(); int height = (int)dim.getHeight(); frame.setSize(width, height); frame.setLayout(new FlowLayout()); label = new JLabel("I show the button text"); label.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(label); b1 = new JButton("The First Button"); b1.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(b1); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { label.setText(b1.getText()+" is pressed!"); } }); b2 = new JButton("The Second Button"); b2.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(b2); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { label.setText(b2.getText()+" is pressed!"); } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new ButtonDemo(); } } CheckBoxDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CheckBoxDemo implements ItemListener { private JFrame frame; private JCheckBox c1, c2, c3, c4; private JLabel label; private String message =" "; public CheckBoxDemo() { frame = new JFrame("A Simple Swing App"); //frame.setSize(600, 400); Toolkit tk = frame.getToolkit(); Dimension dim = tk.getScreenSize(); int width = (int)dim.getWidth(); int height = (int)dim.getHeight(); frame.setSize(width, height); frame.setLayout(new FlowLayout()); c1 = new JCheckBox("Pizza"); c1.addItemListener(this); c1.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(c1); c2 = new JCheckBox("Burger"); c2.addItemListener(this); c2.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(c2); c3 = new JCheckBox("Rolls"); c3.addItemListener(this); c3.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(c3); c4 = new JCheckBox("Beverage"); c4.addItemListener(this); c4.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(c4); label = new JLabel("I show the selected items"); label.setFont(new Font("Verdana", Font.BOLD, 18)); label.setForeground(Color.RED); frame.add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public void itemStateChanged(ItemEvent ie) { if(c1.isSelected()) message += c1.getText() +" "; if(c2.isSelected()) message += c2.getText() +" "; if(c3.isSelected()) message += c3.getText() +" "; if(c4.isSelected()) message += c4.getText() +" "; label.setText(message); message = " "; } public static void main(String[] args) { new CheckBoxDemo(); } } RadioButtonDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RadioButtonDemo implements ActionListener { private JFrame frame; private JRadioButton c1, c2, c3, c4; private JLabel label; public RadioButtonDemo() { frame = new JFrame("A Simple Swing App"); //frame.setSize(600, 400); Toolkit tk = frame.getToolkit(); Dimension dim = tk.getScreenSize(); int width = (int)dim.getWidth(); int height = (int)dim.getHeight(); frame.setSize(width, height); frame.setLayout(new FlowLayout()); c1 = new JRadioButton("Pizza"); c1.addActionListener(this); c1.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(c1); c2 = new JRadioButton("Burger"); c2.addActionListener(this); c2.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(c2); c3 = new JRadioButton("Rolls"); c3.addActionListener(this); c3.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(c3); c4 = new JRadioButton("Beverage"); c4.addActionListener(this); c4.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(c4); ButtonGroup bg = new ButtonGroup(); bg.add(c1); bg.add(c2); bg.add(c3); bg.add(c4); label = new JLabel("I show the selected items"); label.setFont(new Font("Verdana", Font.BOLD, 18)); label.setForeground(Color.RED); frame.add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public void actionPerformed(ActionEvent ae) { label.setText(ae.getActionCommand()); } public static void main(String[] args) { new RadioButtonDemo(); } } ComboBoxDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComboBoxDemo implements ActionListener { private JFrame frame; private JComboBox cb; private JLabel label; public ComboBoxDemo() { frame = new JFrame("A Simple Swing App"); //frame.setSize(600, 400); Toolkit tk = frame.getToolkit(); Dimension dim = tk.getScreenSize(); int width = (int)dim.getWidth(); int height = (int)dim.getHeight(); frame.setSize(width, height); frame.setLayout(new FlowLayout()); cb = new JComboBox(); cb.addItem("Banana"); cb.addItem("Apple"); cb.addItem("Orange"); cb.addItem("Grape"); cb.addItem("Mango"); cb.addItem("Pineapple"); cb.addActionListener(this); frame.add(cb); label = new JLabel("I show the selected item"); label.setFont(new Font("Verdana", Font.BOLD, 18)); label.setForeground(Color.RED); frame.add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public void actionPerformed(ActionEvent ae) { label.setText((String)cb.getSelectedItem()); } public static void main(String[] args) { new ComboBoxDemo(); } } ComboBoxDemo2.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComboBoxDemo2 implements ActionListener { private JFrame frame; private JComboBox cb1, cb2, cb3; private JLabel label; public ComboBoxDemo2() { frame = new JFrame("A Simple Swing App"); //frame.setSize(600, 400); Toolkit tk = frame.getToolkit(); Dimension dim = tk.getScreenSize(); int width = (int)dim.getWidth(); int height = (int)dim.getHeight(); frame.setSize(width, height); frame.setLayout(new FlowLayout()); String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; cb1 = new JComboBox(); cb2 = new JComboBox(months); cb3 = new JComboBox(); for(int i = 1; i<=31; i++){ cb1.addItem(i); } for(int i = 1970; i<2048; i++){ cb3.addItem(i); } cb1.addActionListener(this); cb2.addActionListener(this); cb3.addActionListener(this); frame.add(cb1); frame.add(cb2); frame.add(cb3); label = new JLabel("I show the selected Date"); label.setFont(new Font("Verdana", Font.BOLD, 18)); label.setForeground(Color.RED); frame.add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public void actionPerformed(ActionEvent ae) { String message = ""; message += (Integer)cb1.getSelectedItem()+", "; message += (String)cb2.getSelectedItem()+", "; message += (Integer)cb3.getSelectedItem(); label.setText(message); } public static void main(String[] args) { new ComboBoxDemo2(); } } ListDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class ListDemo implements ListSelectionListener { private JFrame frame; private JList<String> list; private JLabel label; private JToolTip tip; public ListDemo() { frame = new JFrame("A Simple Swing App"); //frame.setSize(600, 400); Toolkit tk = frame.getToolkit(); Dimension dim = tk.getScreenSize(); int width = (int)dim.getWidth(); int height = (int)dim.getHeight(); frame.setSize(width, height); frame.setLayout(new FlowLayout()); String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; list = new JList<String>(months); list.addListSelectionListener(this); frame.add(list); //JScrollPane sp = new JScrollPane(list); //frame.add(sp); label = new JLabel("I show the selected Date"); label.setFont(new Font("Verdana", Font.BOLD, 18)); label.setForeground(Color.RED); frame.add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public void valueChanged(ListSelectionEvent ae) { String message = ""; for(String each: list.getSelectedValuesList()) message += each +" "; label.setText(message); } public static void main(String[] args) { new ListDemo(); } } TextDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class TextDemo implements ActionListener { private JFrame frame; private JTextField tf1, tf2, tf3; private JTextArea ta; private JLabel label, label2; private JButton b; public TextDemo() { frame = new JFrame("A Simple Swing App"); //frame.setSize(600, 400); Toolkit tk = frame.getToolkit(); Dimension dim = tk.getScreenSize(); int width = (int)dim.getWidth(); int height = (int)dim.getHeight(); frame.setSize(width, height); frame.setLayout(new FlowLayout()); tf1 = new JTextField("Enter the name", 25); tf2 = new JTextField(20); tf3 = new JTextField("Enter a value"); tf1.setFont(new Font("Verdana", Font.BOLD, 18)); tf2.setFont(new Font("Verdana", Font.BOLD, 18)); tf3.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(tf1); frame.add(tf2); frame.add(tf3); //tf1.addActionListener(this); tf2.addActionListener(this); tf3.addActionListener(this); ta = new JTextArea(20, 15); ta.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(ta); label = new JLabel(); label.setFont(new Font("Verdana", Font.BOLD, 18)); label.setForeground(Color.RED); frame.add(label); label2 = new JLabel(); label2.setFont(new Font("Verdana", Font.BOLD, 18)); label2.setForeground(Color.green); frame.add(label2); b = new JButton("Display"); b.addActionListener(this); frame.add(b); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public void actionPerformed(ActionEvent ae) { String message =""; message += tf1.getText()+": "; message += tf2.getText()+": "; message += tf3.getText()+": "; label.setText(message); label2.setText(ta.getText()); } public static void main(String[] args) { new TextDemo(); } }
Comments