CC1007NI: Further Programming Week 6-7 Dhruba Sen Module Leader (Islington College)
2.0
Overview Buttons Text fields Layout
Adding a button private JButton button; button = new JButton(“Press me!”); contentPane.add(button); button.addActionListener(this); String command = event.getActionCommand(); if (command.equals(“Press me!”)) {... }
Adding a text field private JTextField textField; textField = new JTextField(10); contentPane.add(textField); String inputText = textfield.getText(); textfield.setText(outputText);
Inputting numbers using a text field String inputText = textfield.getText(); int wholeNumber = Integer.parseInt(inputText); double decimal = Double.parseDouble(inputText);
Flow Layout Frame.setLayout(new FlowLayout()); Is a basic layout in which GUI components are placed in a left to right flow. For more precise arrangement: Frame.setLayout(null);.setBounds(x,y,width,height); E.g. btnsubmit.setBounds(5,5,100,25);
More Components JCheckBox JCheckBox(“ ”) Used for multiple selection at once.
More Components JRadioButton JRadioButton(“ ”) Used for single selection out of multiple options. For the same, button group must be created; ButtonGroup() Add radio buttons to the group. buttonGroup.add(RadioButton);
More Components JComboBox (Drop down menu) JComboBox() Array can also be added as menu items: JComboBox( ); To add (append) Items: comboBox.addItem(“ ”);
More Components JTextArea: Input box for more than one line of input. JTextArea(); Jtextarea.setLineWrap(true);
More Controls for frame FrameObject.setResizable(false); //true default FrameObject.setSize(,<height in pixels); FrameObject.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE);
THANK YOU.