Studyon Minte9.com
Java SE




Components




package testexample; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Vector; import javax.swing.*; public class Test extends JFrame { // --- Look Here --- // private JLabel jLabelName = new JLabel("Name"); private JTextField jTextName = new JTextField(); private JLabel jLabelPassword = new JLabel("Password"); private JTextField jTextPassword = new JPasswordField(); private JCheckBox jCheckBoxAgree = new JCheckBox(" agree"); private JLabel jLabelGender = new JLabel("Gender"); private JRadioButton jRadioButtonGender1 = new JRadioButton("male"); private JRadioButton jRadioButtonGender2 = new JRadioButton("female"); private ButtonGroup jButtonGroup1 = new ButtonGroup(); private JLabel jLabelCity = new JLabel("City"); private JComboBox jComboBoxCity = new JComboBox(); private JLabel jLabelLanguage = new JLabel("Language"); private JList jListLanguage = new JList(); private JScrollPane jScrollPaneLanguage = new JScrollPane(); private JLabel jLabelAmount = new JLabel("Amount"); private JFormattedTextField jFormattedTextFieldAmount = new JFormattedTextField(new Float(0)); private JLabel jLabelDay = new JLabel("Day"); private JSpinner jSpinnerDay = new JSpinner(); private JLabel jLabelDescription = new JLabel("Descrip."); private JTextPane jTextPaneDescription = new JTextPane(); private JScrollPane jScrollPaneDescription = new JScrollPane(); private JLabel jLabelSliderMonths1 = new JLabel("1 month"); private JLabel jLabelSliderMonths2 = new JLabel("24 months"); private JSlider jSliderMonths = new JSlider(1, 24); public static void main(String[] args) { setLookAndFill(); Test frame = new Test(); frame.setBounds(200, 200, 400, 600); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); } private static void setLookAndFill() { try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (Exception ex) {} } public Test() { initComponents(); } class Item { // Used for ComboBox private int key; private String value; public Item(int key, String value) { this.key = key; this.value = value; } public int getKey() { return this.key; } public String getValue() { return this.value; } @Override public String toString() { return this.value; } } public void initComponents() { // --- Look Here --- // JPanel panel = new JPanel(); GroupLayout layout = new GroupLayout(panel); panel.setLayout(layout); // Checkboxes jButtonGroup1.add(jRadioButtonGender1); jButtonGroup1.add(jRadioButtonGender2); // ComboBox (caution: Vector is syncronized) Vector model = new Vector(); model.addElement(new Item(1, "Bucharest")); model.addElement(new Item(2, "London")); model.addElement(new Item(3, "Paris")); jComboBoxCity.setModel(new DefaultComboBoxModel(model)); // List jListLanguage.setModel(new AbstractListModel() { String[] items = {"english", "romanian", "french", "german", "italian"}; @Override public int getSize() { return items.length; } @Override public Object getElementAt(int index) { return items[index]; } }); jScrollPaneLanguage.setViewportView(jListLanguage); // TextPane jScrollPaneDescription.setViewportView(jTextPaneDescription); // JLabel style jLabelSliderMonths1.setFont(new Font("Sans Serif", Font.BOLD, 11)); jLabelSliderMonths2.setFont(new Font("Sans Serif", Font.BOLD, 11)); // Send Button JButton sendButton = new JButton("Send"); sendButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { showValues(); } }); // --- Horizontal Layout --- // layout.setHorizontalGroup( layout.createSequentialGroup() .addGap(20) .addGroup(layout.createParallelGroup() .addGroup(layout.createSequentialGroup() .addComponent(jLabelName) .addGap(35) .addComponent(jTextName, 0, 200, 200) // width ) .addGroup(layout.createSequentialGroup() .addComponent(jLabelPassword) .addGap(13) .addComponent(jTextPassword, 0, 200, 200) ) .addGroup(layout.createSequentialGroup() .addGap(68) .addComponent(jCheckBoxAgree, 0, 200, 200) ) .addGroup(layout.createSequentialGroup() .addComponent(jLabelGender) .addGap(26) .addComponent(jRadioButtonGender1, 0, 60, 60) .addComponent(jRadioButtonGender2, 0, 70, 70) ) .addGroup(layout.createSequentialGroup() .addComponent(jLabelCity) .addGap(48) .addComponent(jComboBoxCity, 0, 200, 200) ) .addGroup(layout.createSequentialGroup() .addComponent(jLabelLanguage) .addGap(12) .addComponent(jScrollPaneLanguage, 0, 200, 200) ) .addGroup(layout.createSequentialGroup() .addComponent(jLabelAmount) .addGap(26) .addComponent(jFormattedTextFieldAmount, 0, 200, 200) ) .addGroup(layout.createSequentialGroup() .addComponent(jLabelDay) .addGap(46) .addComponent(jSpinnerDay, 0, 100, 100) ) .addGroup(layout.createSequentialGroup() .addComponent(jLabelDescription) .addGap(20) .addComponent(jScrollPaneDescription, 0, 200, 200) ) .addGroup(layout.createSequentialGroup() .addComponent(jLabelSliderMonths1) .addGap(22) .addComponent(jSliderMonths, 0, 200, 200) .addGap(22) .addComponent(jLabelSliderMonths2) ) .addGroup(layout.createSequentialGroup() .addGap(68) .addComponent(sendButton) ) ) ); // --- Vertical Layout --- // layout.setVerticalGroup( layout.createSequentialGroup() .addGap(50) .addGroup(layout.createParallelGroup() .addComponent(jLabelName) .addComponent(jTextName, 0, 25, 25) // height ) .addGap(10) .addGroup(layout.createParallelGroup() .addComponent(jLabelPassword) .addComponent(jTextPassword, 0, 25, 25) ) .addGap(10) .addGroup(layout.createParallelGroup() .addComponent(jCheckBoxAgree, 0, 20, 20) ) .addGap(10) .addGroup(layout.createParallelGroup() .addComponent(jLabelGender) .addComponent(jRadioButtonGender1, 0, 20, 20) .addComponent(jRadioButtonGender2, 0, 20, 20) ) .addGap(10) .addGroup(layout.createParallelGroup() .addComponent(jLabelCity) .addComponent(jComboBoxCity, 0, 25, 25) ) .addGap(10) .addGroup(layout.createParallelGroup() .addComponent(jLabelLanguage) .addComponent(jScrollPaneLanguage, 0, 75, 75) ) .addGap(10) .addGroup(layout.createParallelGroup() .addComponent(jLabelAmount) .addComponent(jFormattedTextFieldAmount, 0, 25, 25) ) .addGap(10) .addGroup(layout.createParallelGroup() .addComponent(jLabelDay) .addComponent(jSpinnerDay, 0, 25, 25) ) .addGap(10) .addGroup(layout.createParallelGroup() .addComponent(jLabelDescription) .addComponent(jScrollPaneDescription, 0, 75, 75) ) .addGap(10) .addGroup(layout.createParallelGroup() .addComponent(jLabelSliderMonths1) .addComponent(jSliderMonths, 0, 25, 25) .addComponent(jLabelSliderMonths2) ) .addGap(30) .addGroup(layout.createParallelGroup() .addComponent(sendButton) ) ); add(panel); } public void showValues() { String output = ""; output += "Name: " + jTextName.getText() + "n"; output += "Password: " + jTextPassword.getText() + "n"; output += "Agree: " + jCheckBoxAgree.isSelected() + "n"; String genderText = ""; genderText = jRadioButtonGender1.isSelected() ? "M" : ""; genderText = jRadioButtonGender2.isSelected() ? "F" : genderText; output += "Gender: " + genderText + "n"; Item item = (Item) jComboBoxCity.getSelectedItem(); output += "City: " + item.getKey() + " / " + item.getValue() + "n"; Object[] data = jListLanguage.getSelectedValues(); output += "Languages: "; for (Object x : data) { output += x + ", "; } output += "n"; output += "Amount: " + jFormattedTextFieldAmount.getText() + "n"; output += "Day: " + jSpinnerDay.getValue() + "n"; output += "Description: " + jTextPaneDescription.getText() + "n"; output += "Months: " + jSliderMonths.getValue() + "n"; System.out.println(output); } }
 


http://da2i.univ-lille1.fr/doc/tutorial-java/ui/features/components.html
http://www.java2s.com/Code/Java/Swing-JFC/AquickdemonstrationofJFormattedTextField.htm