java code: JFrame filled up with all existing gui swing components, each with its action…
- User
java code: JFrame filled up with all existing gui swing components, each with its action listener
- Assistant
Here is an example code that creates a
JFramefilled with various Swing components, each with its own ActionListener:import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class SwingComponentsExample extends JFrame { public SwingComponentsExample() { setTitle("Swing Components Example"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 400); setLayout(new FlowLayout()); // Create JButton JButton button = new JButton("Click me"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(SwingComponentsExample.this, "Button clicked"); } }); add(button); // Create JCheckBox JCheckBox checkBox = new JCheckBox("Check me"); checkBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { boolean selected = checkBox.isSelected(); JOptionPane.showMessageDialog(SwingComponentsExample.this, "Checkbox selected: " + selected); } }); add(checkBox); // Create JTextField JTextField textField = new JTextField(10); textField.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String text = textField.getText(); JOptionPane.showMessageDialog(SwingComponentsExample.this, "Text entered: " + text); } }); add(textField); // Create JRadioButton JRadioButton radioButton1 = new JRadioButton("Option 1"); radioButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(SwingComponentsExample.this, "Option 1 selected"); } }); add(radioButton1); JRadioButton radioButton2 = new JRadioButton("Option 2"); radioButton2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(SwingComponentsExample.this, "Option 2 selected"); } }); add(radioButton2); // Create JComboBox String[] options = {"Option 1", "Option 2", "Option 3"}; JComboBox<String> comboBox = new JComboBox<>(options); comboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String selectedOption = (String) comboBox.getSelectedItem(); JOptionPane.showMessageDialog(SwingComponentsExample.this, "Selected option: " + selectedOption); } }); add(comboBox); } public static void main(String[] args) { SwingComponentsExample example = new SwingComponentsExample(); example.setVisible(true); } }This code creates a
JFramewindow and adds various Swing components, includingJButton,JCheckBox,JTextField,JRadioButton, andJComboBox. Each component has its ownActionListenerthat will perform a specific action when triggered. - User
complete list of all swing components like JButton, JCheckBox, JTextField, JRadioButton, and JComboBox
- Assistant
Here is a complete list of all the commonly used Swing components:
- JButton: A clickable button that performs an action when clicked.
- JCheckBox: A box that can be selected or deselected.
- JRadioButton: A button that represents a choice between two or more mutually exclusive options.
- JTextField: A text input field where the user can enter and edit text.
- JTextArea: A multi-line text area where the user can enter and edit longer text.
- JPasswordField: A field for entering password text.
- JLabel: A non-interactive component that displays a single line of read-only text or an image.
- JList: A component that displays a list of selectable items.
- JTable: A component for displaying and editing tabular data.
- JComboBox: A drop-down list that allows the user to choose an option from a predefined set of choices.
- JSlider: A visual control that allows the user to select a value from a range by dragging a knob along a track.
- JProgressBar: A visual component that represents the progress of a long-running task.
- JMenu: A component that represents a menu, which can contain menu items and submenus.
- JMenuItem: A component that represents an item in a menu.
- JMenuBar: A container component that holds a menu bar, which can contain menus.
- JPopupMenu: A component that represents a popup menu, which can be shown at a specific position in a window.
- JToolBar: A container component that holds buttons and other components for quick access to actions.
These are some of the most commonly used Swing components. There are also other advanced components available, such as JTree, JFileChooser, JColorChooser, and more. You can find a complete list of Swing components and their documentation in the official Java documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/package-summary.html