0

I am trying to create a frame, and when I am adding some components they don't listen to the sizes I give them, or locations - whenever I resize the frame, the components stick together, one aside another. Also, I have a scrollable text area, which takes the length and width of the text written in it. Plus, if I don't resize the frame the components don't show.

My code:

public static void main(String[] args){
    new Main();
}
private void loadLabel(){
    label.setBounds(0,0,269,20);
    //Setting the icon, not relevant to the code.
    panel.add(label);
}
private void loadInput(){
    input.setBounds(0,20,300,60);
    JScrollPane scroll = new JScrollPane (input);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setVisible(true);
    scroll.setBounds(50,20,300,60);
    panel.add(scroll);
}

private JPanel panel = new JPanel();
private JLabel label = new JLabel();
private JTextArea input = new JTextArea("Enter message                                                         ");
public Main() {
    super("Frame");
    setLocationRelativeTo(null);
    setSize(300, 400);
    setContentPane(panel);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    loadLabel();
    loadInput();

}

Thanks in advance!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
NonameSL
  • 37
  • 4
  • 2
    If you use layouts, your problems will resolve themselves –  Aug 23 '14 at 21:50
  • 1
    Your main and only issue is that you use `setBounds`. Instead learn how to use LayoutManager's and all your issues will disappears at once. For once and for all, learn that you should never use `setBounds/setSize/setLocation`. If you ever want to do that, just use an appropriate `LayoutManager`. – Guillaume Polet Aug 23 '14 at 21:57
  • Some info to [LayoutManager](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) – Andie2302 Aug 23 '14 at 22:19
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). 2) Provide ASCII art or a simple drawing of how the GUI should appear at default size and (if resizable) with extra width/height. .. – Andrew Thompson Aug 24 '14 at 03:30
  • .. 3) Swing GUIs might have to work on different platforms, using different PLAFs, on different screen sizes and resolutions with different default settings for font size. As such, they are not conducive to exact placement of components. Instead use layout managers, or [combinations of layout managers](http://stackoverflow.com/a/5630271/418556) as well as [layout padding and borders](http://stackoverflow.com/q/17874717/418556) for white space. – Andrew Thompson Aug 24 '14 at 03:30

2 Answers2

0

You shouldn't arrange your components using .setBounds(,,,) but instead arrange your components using layout ( http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html ). Plus you haven't set your label a text or icon so it's hard to see those component correctly. Here i'm using BoxLayout to manage your components vertically and put them on the EAST side of your frame by replacing setContentPane(panel); to getContentPane().add(panel,BorderLayout.EAST); to help us see your components correctly.

import java.awt.*;
import javax.swing.*; 

public class Main extends JFrame {
   public static void main(String[] args){
    new Main();
}
private void loadLabel(){
    label.setBounds(0,0,269,20);
    //Setting the icon, not relevant to the code.
    panel.add(label);
}
private void loadInput(){
    input.setBounds(0,20,300,60);
    JScrollPane scroll = new JScrollPane (input);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setVisible(true);
    scroll.setBounds(50,20,300,60);
    panel.add(scroll);
}

private JPanel panel = new JPanel();
private JLabel label = new JLabel("Your Label");
private JTextArea input = new JTextArea("Enter message                                                         ");
public Main() {
    super("Frame");
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    setLocationRelativeTo(null);
    setSize(300, 400);
    getContentPane().add(panel,BorderLayout.EAST);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    loadLabel();
    loadInput();

}
 }

enter image description here

Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19
-1

Write like this

loadLabel();
loadInput();

setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Load the content then make it visible true

Gurkan İlleez
  • 1,503
  • 1
  • 10
  • 12