1

I am trying to resize the JPanels but there is a space under it . Here is a link to show :

enter image description here

 And this is the code : import java.awt.*; import javax.swing.*; public class Ex1 extends JFrame{ private JTextArea textarea = new JTextArea (); private JTextField field = new JTextField ();`` private JButton buton = new JButton ("Trimite"); public Ex1(){ JPanel panel = new JPanel (new BorderLayout(2,2)); JPanel panel1 = new JPanel (new BorderLayout(2,2)); JPanel panel2 = new JPanel (new BorderLayout(2,2)); JLabel label1 = new JLabel ("Mesaje"); JLabel label2 = new JLabel ("Scrieti un mesaj"); panel1.setPreferredSize(new Dimension(350,100)); panel2.setPreferredSize(new Dimension(350,25)); panel1.add(label1, BorderLayout.NORTH); panel1.add(textarea, BorderLayout.CENTER); panel2.add(label2, BorderLayout.WEST); panel2.add(field, BorderLayout.CENTER); panel2.add(buton, BorderLayout.EAST); setLayout(new GridLayout(2,1,1,1)); panel.add(panel1, BorderLayout.NORTH); panel.add(panel2, BorderLayout.CENTER); add(panel); } public static void main(String[] args) { JFrame frame = new Ex1(); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } 

}

2
  • that is the frame not the panel. resize the frame Commented Dec 8, 2014 at 18:12
  • if you have enough time and have patience...the one approach is you use absolute layout(to very small project...) ...it has more time of you...but you can arrange every component at absolute position and size that you want..! :-) Commented Dec 8, 2014 at 19:00

2 Answers 2

4

You are setting a layout for a frame to GridLayout in which all components are given equal size. You have two rows, add(panel) adds the panel to the first row of the grid. The second row is left empty. See How to Use GridLayout.

Comment out setLayout(new GridLayout(2,1,1,1)); and the extra space should go away. When you comment this line the layout of frame's content pane will be BorderLayout. The default layout of the JFrame is BorderLayout. So add(panel); will add the panel to the center of the frame's content pane. As a result the panel should occupy all the available space.

As a side note, avoid setPreferredSize(), usually it is not necessary, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing for details.

You can specify the number of rows and columns for a text area and wrap it in the scroll pane, ie:

textArea = new JTextArea(5, 20); JScrollPane scrollPane = new JScrollPane(textArea); 

For more details see How to Use Text Areas

EDIT: example of getPreferredSize()

import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.*; public class Ex1 extends JPanel{ private JTextArea textarea = new JTextArea (); private JTextField field = new JTextField (); private JButton buton = new JButton ("Trimite"); public Ex1() { setLayout(new BorderLayout()); JPanel panel1 = new JPanel (new BorderLayout(2,2)); JPanel panel2 = new JPanel (new BorderLayout(2,2)); JLabel label1 = new JLabel ("Mesaje"); JLabel label2 = new JLabel ("Scrieti un mesaj"); panel1.add(label1, BorderLayout.NORTH); panel1.add(new JScrollPane(textarea), BorderLayout.CENTER); panel2.add(label2, BorderLayout.WEST); panel2.add(field, BorderLayout.CENTER); panel2.add(buton, BorderLayout.EAST); add(panel1, BorderLayout.CENTER); add(panel2, BorderLayout.SOUTH); } @Override public Dimension getPreferredSize() { return new Dimension(350, 300); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationByPlatform(true); Ex1 panel = new Ex1(); frame.add(panel); frame.pack(); frame.setVisible(true); } }); } } 
Sign up to request clarification or add additional context in comments.

10 Comments

One more question : if I want the frame's size to be 350 x 300 in only shows the first panel :-(
@Angelo how do you set 350x300 size?
Yes . In main , I want " frame.setSize (350,300)
@Angelo I cannot really reproduce the case when only one panel is shown. In any case, specify the desired number of rows and columns for text components. Also override getPreferredSize() for the panel (the one that is added to the frame's content) to return (350,300). Then pack() the frame. You may also want to put panel1 in the CENTER and panel2 in SOUTH so the text area occupies more space.
I dont really understand how is that done :-?? I just want the frame's size to be 350 x 300
|
0

You need to resize the JFrame not the JPanel. Try:

this.setPreferredSize(new Dimension(350, 25);// in Ex1 

Or in your main method:

frame.setPreferredSize(new Dimension(350, 25); 

1 Comment

Try using setbounds for resetting the window size

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.