Chapter 13 Graphical User Interface Components: (My space web page) Part 2

Chapter 13 Graphical User Interface Components: Part 2 781 25 // instantiate gridbag constraints 26 constraints = new GridBagConstraints(); 27 28 // create GUI components 29 String metals[] = { “Copper”, “Aluminum”, “Silver” }; 30 JComboBox comboBox = new JComboBox( metals ); 31 32 JTextField textField = new JTextField( “TextField” ); 33 34 String fonts[] = { “Serif”, “Monospaced” }; 35 JList list = new JList( fonts ); 36 37 String names[] = 38 { “zero”, “one”, “two”, “three”, “four” }; 39 JButton buttons[] = new JButton[ names.length ]; 40 41 for ( int count = 0; count < buttons.length; count++ ) 42 buttons[ count ] = new JButton( names[ count ] ); 43 44 // define GUI component constraints 45 // textField 46 constraints.weightx = 1; 47 constraints.weighty = 1; 48 constraints.fill = GridBagConstraints.BOTH; 49 constraints.gridwidth = GridBagConstraints.REMAINDER; 50 addComponent( textField ); 51 52 // buttons[0] -- weightx and weighty are 1: fill is BOTH 53 constraints.gridwidth = 1; 54 addComponent( buttons[ 0 ] ); 55 56 // buttons[1] -- weightx and weighty are 1: fill is BOTH 57 constraints.gridwidth = GridBagConstraints.RELATIVE; 58 addComponent( buttons[ 1 ] ); 59 60 // buttons[2] -- weightx and weighty are 1: fill is BOTH 61 constraints.gridwidth = GridBagConstraints.REMAINDER; 62 addComponent( buttons[ 2 ] ); 63 64 // comboBox --weightx is 1: fill is BOTH 65 constraints.weighty = 0; 66 constraints.gridwidth = GridBagConstraints.REMAINDER; 67 addComponent( comboBox ); 68 69 // buttons[3] -- weightx is 1: fill is BOTH 70 constraints.weighty = 1; 71 constraints.gridwidth = GridBagConstraints.REMAINDER; 72 addComponent( buttons[ 3 ] ); 73 74 // buttons[4] -- weightx and weighty are 1: fill is BOTH 75 constraints.gridwidth = GridBagConstraints.RELATIVE; 76 addComponent( buttons[ 4 ] ); Fig. 13.21 Demonstrating the GridBagConstraintsconstants RELATIVEand REMAINDER(part 2 of 3).
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Leave a Reply