Java Reference
In-Depth Information
30
31 frame.setVisible( true );
32 }
33 }
The preceding program uses a for loop to set the text of the first nine buttons to
the integers 1 through 9, but it runs into the problem that the constructor for a
JButton accepts a String as its parameter. To get around this issue, the for loop
uses a trick to convert the integer value stored in i into a String by concatenating it
with the empty string, "" . The program's graphical output is the following:
It can be tricky to get a composite layout to look just right. You may want to prac-
tice with lots of examples and carefully consider the stretching behaviors of the vari-
ous layouts. For example, how might you create a window that looks like this?
BorderLayout is the right choice for the frame, but how would you lay out the
individual components? There are three rows of components at the top of the win-
dow, so your first thought might be to place those components in a panel with a 3
3
grid layout. You'd then add this layout to the north region of the frame, along with the
central text area and a Send button in the south region:
×
// this code does not produce the right layout
frame.setLayout(new BorderLayout());
 
Search WWH ::




Custom Search