Dies ist eine alte Version des Dokuments!
Inhaltsverzeichnis
GUI-Usage
JLabel
A JLabel component can display just text, or an image, or both. To display an image, just use a JLabel component.
With the method setLocation(int x, int y) of a JLabel-component you can animate an image.
JTextField
With a JTextField component you can enter a line of text. Use the getText() method of the JTextField component to read the entered text.
String Name = tfName.getText();
JNumberField
With JNumbertField components you can easily display or enter numbers.
With getDouble(), getFloat(), getInt() and getLong() you get a number of a specific type:
int Age = nfAge.getInt();
You can display decimal numbers with maximum or a specific amount of decimal places
Example:
nfSum.setDouble(Sum); // maximum decimal places: 3657.42323426347834 nfSum.setDouble(Sum, 2); // two decimal places: 3657.42
JTextArea
A JTextArea component displays multiline text, a JTextField component a single line. With the object-inspector you can enter the text. During runtime you can add text with the append()-method respectively set text with setText() or read it with getText(). The lines are separated with the control character „\n“ (new line).
taAusgabe.setText("Output:\n\n"); taAusgabe.append("Number " + Number + " found! \n");
JButton
Every JButton-component gets an event-method for the actionPerformed-event, which is created if you push a button. In a GUI-form doubleclick a button to place the cursor at the beginning of the corrsponding event-method.
Example:
public void jButton1_ActionPerformed(ActionEvent evt) { // TODO add your code here } // end of jButton1_ActionPerformed
JCheckBox
A JCheckBox component is selected or not selected. You get the current state with isSelected()
.
if (myCheckBox.isSelected()) ...
JRadioButton & JButtonGroup
JRadiobuttons offer different options, from which you can choose just one. To group the JRadioButtons you have to place an invisible ButtonGroup component on the form. Write the name of this ButtonGroup for each JRadioButton as value of the attribute ButtonGroup in the object inspector.
The Java-Editor places this method into the java source code:
public String buttonGroup1_getSelectedRadioButtonLabel() { ... }
so it's easy to get the selected JRadioButton of a ButtonGroup. Example:
if (bgColor_getSelectedRadioButtonLabel().equals("green"))
JList
A JList component shows a list of objects. The user can select on or more objects. With the Items attribute in the object-inspector you can enter strings into a JList.
The data of a list are managed in a ListModel. During runtime you can edit the list objects with methods of the ListModel.
Examples
Access through ListModel:
myListModel.addElement("Vera"); myListModel.remove(0); String s = (String) myListModel.elementAt(3);
The ListModel knows all elements, but an element is selected in the List, not in the ListModel.
Access through List:
int i = myList.getSelectedIndex(); String s = (String) myList.getSelectedValue();
If the List contains numbers, the selected string has to be converted into a number:
String s = (String) myList.getSelectedValue(); int number = Integer.parseInt(s);
JSpinner
With a JSpinner component you can select numbers from a range between Minimum and Maximum. You set the values of Minimum, Maximum, StepSize and Value with the object-inspector.
The data of a JSpinner component are managed in the SpinnerModel. During runtime you can edit the data with the SpinnerModel-methods.
Examples:
Access through SpinnerModel
mySpinnerModel.setStepSize(2); int Value = mySpinnerModel.getNumber().intValue();
Access through Spinner
int Value = (int) mySpinner.getValue();
JTable
With a jTable-component you display a table.
Set the number of columns and rows with the attributes ColCount and RowCount in the object-inspector. Use the attribute Columns to set the column-names. Set AutoCreateRowSorter to true to enable sorting after column-names.
With the method setValueAt(Object aValue, int row, int column) you set a value at the position (row, column) and with getValueAt(int row, int column) you read it. getSelectedRow() gets the index of the first selected row.
Example: „Anna“ ist written in the first column of the selected row.
int i = jTabelle.getSelectedRow(); jTabelle.setValueAt("Anna", i, 0);