Resizing JPanel?
I am new to Java and learning the layouts...I am using a GridLayout and attempting to put JPanels thatc ontain other widgets inside the layout . My top panel is a graphic that has a height of 79....this is the ONLY panel that I want to have a height of 79....the others I would like to make 35, but the only way I have been able to make the header graphic fully display is by increasing the SetSize paramater of the whole window....but doing so stretches out the other panels and leaves too much white space. How can I individually size the JPanels? or can I?
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
publicclass GUIextends JFrame
implements ActionListener
{
private JButton button1, button2, button3;
private JLabel label1, label2, label3,label4, label5, headLabel;;
private JTextField text1, text2, text3, text4, text5;
// Constructor - GUI Window
public GUI()
{
// Basic Window Propertes
setTitle("Merchant");
setSize(750, 370);
setLocation(400,200);
// Grid Setup
GridLayout mainGrid =new GridLayout(4,1);
setLayout(mainGrid);
ImageIcon header =new ImageIcon("image.jpg");
headLabel =new JLabel(header);
// Create Input Labels
label1 =new JLabel("Name:", JLabel.CENTER);
label2 =new JLabel("QNumber:", JLabel.CENTER);
label3 =new JLabel("Cost:", JLabel.CENTER);
// Create Input Text Fields
text1 =new JTextField(15);
text1.setEnabled(true);
text2 =new JTextField(3);
text2.setEnabled(true);
text3 =new JTextField(7);
text3.setEnabled(true);
// Create Action Buttons
button1 =new JButton("Sale");
button2 =new JButton("Return");
button3 =new JButton("Invoice");
// Create Output Labels
label4 =new JLabel("Amount of Transaction:", JLabel.CENTER);
label5 =new JLabel("Current Balance:", JLabel.CENTER);
// Create Input Text Fields
text4 =new JTextField(8);
text4.setEnabled(true);
text5 =new JTextField(8);
text5.setEnabled(true);
// *************************** JPanel Layout ***************************
// Add Header Panel to Layout
JPanel headPanel =new JPanel();
headPanel.setBackground(Color.white);
headPanel.add(headLabel);
//headPanel.setPreferredSize(new Dimension(750, 79));
add(headPanel,"Header");
// Add Top Panel to Layout
JPanel topPanel =new JPanel();
topPanel.setBackground(Color.white);
topPanel.add(label1);
topPanel.add(text1);
topPanel.add(label2);
topPanel.add(text2);
topPanel.add(label3);
topPanel.add(text3);
add(topPanel,"Top");
// Add Middle Panel to Layout
JPanel midPanel =new JPanel();
midPanel.setBackground(Color.white);
midPanel.add(button1);
midPanel.add(button2);
midPanel.add(button3);
add(midPanel,"Middle");
//Add Bottom Panel to Layout
JPanel botPanel =new JPanel();
botPanel.setBackground(Color.white);
botPanel.add(label4);
botPanel.add(text4);
botPanel.add(label5);
botPanel.add(text5);
add(botPanel,"Bottom");
// *********************************************************************
// Listeners
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
// Make Visible
setVisible(true);
}
// Method - Checks to see if the user has performed an action
publicvoid actionPerformed(ActionEvent e){
// Check to see which button initiated the event
if (e.getSource() == button1)
{
System.err.println("Button 1 is HOT");
}
elseif (e.getSource() == button2)
{
System.err.println("Button 2 is HOT");
}
elseif (e.getSource() == button3)
{
System.err.println("Button 3 is HOT");
InvoiceFrame frame =new InvoiceFrame();
}
}
}

