// Project: Swing // Module: swingcontrols // Source code file: MyFrame.java // Show how to create a simple Swing // frame with some controls. // Look up these controls in the // Java Class Library import javax.swing.*; public class MyFrame extends JFrame { // Declare and instantiate controls. private JTextField textField = new JTextField( ); private JButton button = new JButton("Push Me"); private JRadioButton radioButton = new JRadioButton("Radio Button Text" ); private JCheckBox checkBox = new JCheckBox("Check Box Text" ); private JComboBox comboBox = new JComboBox( ); private JPanel contentPanel = new JPanel( ); // The main method creates the frame. public static void main(String[] args) { new MyFrame( ); } public MyFrame( ) { // Set text in title bar. super("SwingControls Example"); // Don't use layout manager contentPanel.setLayout(null); // Add textfield to panel textField.setLocation(25, 25); textField.setSize(150, 25); contentPanel.add(textField); // Add button to panel button.setLocation(25, 75); button.setSize(150, 25); contentPanel.add(button); // Add radiobutton to panel radioButton.setLocation(25, 125); radioButton.setSize(150, 25); contentPanel.add(radioButton); // Add checkbox to panel checkBox.setLocation(25, 175); checkBox.setSize(150, 25); contentPanel.add(checkBox); // Add combobox (dropdown menu) to panel. comboBox.setLocation(25, 225); comboBox.setSize(150, 25); contentPanel.add(comboBox); // Add some items to the combobox. comboBox.addItem("Chicago"); comboBox.addItem("Joliet"); comboBox.addItem("Springfield"); // Set the panel to be used to display // controls on the frame. this.setContentPane(contentPanel); // Set width of frame to 400 pixels and // height to 500 pixels. this.setSize(400, 500); // Make frame visible. this.setVisible(true); } }