Java Reference
In-Depth Information
We will create a JPanel that contains the search panel and the booking panel. Since these
two panels do not need to change size when the user resizes the window, we will place the
search panel in the north, and the booking panel in the south, as shown in Figure 8-26.
Figure 8-26. The new panel for searching and for booking
We can then take the panel shown in Figure 8-26 and add it to the south of the panel
shown in Figure 8-25, which results in a GUI that will resize the table pane where needed,
while leaving the search and booking panes the desired size. The code shown in Listing 8-20
demonstrates this techinique. Note, however, that labels are used in place of the real compo-
nents—this is done so that the code may be easier to understand.
Listing 8-20. Combining Several Panels to Form One Overall GUI
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class MyFrame {
public static void main(String[] args) throws Exception {
Border border = BorderFactory.createLineBorder(Color.BLACK);
JFrame theFrame = new JFrame();
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// This is the panel for the table - that is all it contains
JPanel tablePanel = new JPanel();
tablePanel.setBorder(border);
JLabel table = new JLabel("Table display area",SwingConstants.CENTER);
table.setPreferredSize(new Dimension(650,225));
tablePanel.add(table);
// The search options panel
JPanel searchPanel = new JPanel();
searchPanel.setBorder(border);
JLabel search = new JLabel("Search area",SwingConstants.CENTER);
search.setPreferredSize(new Dimension(650,15));
searchPanel.add(search);
// the booking options panel
JPanel bookPanel = new JPanel();
bookPanel.setBorder(border);
JLabel book = new JLabel("Booking area",SwingConstants.CENTER);
book.setPreferredSize(new Dimension(650,15));
bookPanel.add(book);
Search WWH ::




Custom Search