ok I have a little problem. Im writing an app for a project at school. When i run the project the panel2 and panel3 over supersized, and i cant figure out why. i tried playing around with it, but cant get the second panel to be the width of field1 or the panel3 to be the width of label1. I set the background color of the two panels to show what i mean. If anyone can give a reason and possibly an answer to fix this, that would be great.
Sincerely,
Chris Dancy
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class WriterTest
{
public static void main(
String [] args)
{
JFrame f = new JFrame();
f.add(new FrameSetUp());
f.setLocation(300,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.pack();
}
}
class FrameSetUp extends JPanel
{
private JPanel panel1,panel2,panel3;
private JTextArea area1,area2;
private JLabel label1;
private JTextField field1;
private JScrollPane s1,s2;
public FrameSetUp()
{
super.setLayout(new GridLayout(3,1));
panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel1.setBorder(BorderFactory.createEtchedBorder());
area1 = new JTextArea(18,30);
area1.setLineWrap(true);
area1.setEditable(false);
s1 = new JScrollPane(area1,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
area2 = new JTextArea(18,10);
area2.setEditable(false);
area2.setLineWrap(true);
s2 = new JScrollPane(area2);
panel1.add(s1);
panel1.add(s2);
super.add(panel1);
panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel2.setBackground(Color.red);
field1 = new JTextField(30);
field1.addActionListener(new field1Listener());
panel2.add(field1);
super.add(panel2);
panel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel3.setBackground(Color.black);
label1 = new JLabel("over here");
label1.setBackground(Color.red);
label1.setForeground(Color.green);
panel3.add(label1);
super.add(panel3);
}
class field1Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
area1.append(String.format("%s\n",field1.getText()));
field1.setText("");
}
}
}