-1

I'm trying to create a GUI with java. My gui will be simple. You can see what I want from here : http://sketchtoy.com/64839370

In order to do that, I have decided to use BorderLayout as suggested on the web. I have two Jpanel object and I have put them into jFrame whose layout is borderlayout. You can see my simplified code below :

private Display display= new Display(); // Display extends JPanel 
public Simulation()
    {
        super();
        // frame settings 
        setTitle("Label of JFrame ");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBounds(100,100,1094,560);    
        contentPane=this.getContentPane();
        setResizable(false);

        contentPane.setLayout(new BorderLayout());

        try {
            LeftPanelLogo=ImageIO.read(new File("logo.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // generate left panel (information panel)
        leftPanel=new JPanel(){
            @Override
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                Graphics2D g2d=(Graphics2D)g;
                g2d.drawImage(LeftPanelLogo, 10, 250, null);
            }
        };
        //leftPanel.setLayout(null);

        // add panels to contentPane 


        leftPanel.setBackground(Color.WHITE);
        display.setBackground(Color.BLACK);

        contentPane.add(leftPanel,BorderLayout.WEST);
        contentPane.add(display,BorderLayout.CENTER);
}

In Display class constructor I have only the following code:

try 
        {
            bgPicture = ImageIO.read(new File("bg.jpg"));
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }

When I run the code, I saw that almost all the screen is fulfilled with the panel which is on the center, and I could not see the leftPanel, (in other words, all screen was black since I set the background of display panel to black)

So, how could I fix it ?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • `leftPanel.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). Using layouts is also likely the solution to this problem. – Andrew Thompson Apr 01 '15 at 13:40
  • @AndrewThompson thanks for the info but although I put it into comment, it did not change anything – bulak bulaki Apr 01 '15 at 13:41
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Apr 01 '15 at 13:42
  • I guess, I use nomenclature correctly, altough it is off topic could you show me some example where I used it incorrectly – bulak bulaki Apr 01 '15 at 13:44
  • BTW - I cannot see any reason why `leftPanel` is custom painted. Just add the image to an `ImageIcon`, add the image icon to a `JLabel`, and add the label to a (standard) `JPanel`. Than it will return a sensible preferred size to be used by `pack();` – Andrew Thompson Apr 01 '15 at 13:44
  • *"could you show me some example where I used it incorrectly"* `LeftPanelLogo`.. – Andrew Thompson Apr 01 '15 at 13:45
  • @AndrewThompson yea, you're right, I could not see it, I'll fix it – bulak bulaki Apr 01 '15 at 13:46
  • @AndrewThompson If I call pack(), does it changes the size of display jpanel? Moreover, putting it as you suggest will solve my problem ? – bulak bulaki Apr 01 '15 at 13:47
  • Why don't you ***try it***? Heck, I coded an entire example in the time it's taken to see that comment. – Andrew Thompson Apr 01 '15 at 13:55

1 Answers1

1

enter image description here

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class LogoLayout {

    private JComponent ui = null;

    LogoLayout() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        BufferedImage logo = new BufferedImage(
                276,560,BufferedImage.TYPE_INT_RGB);

        /* All that's needed */
        ui.add(new JLabel(new ImageIcon(logo)), BorderLayout.LINE_START);
        ui.add(new JTextArea("Display", 3, 44));
        /* All that's needed */
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                LogoLayout o = new LogoLayout();

                JFrame f = new JFrame("Logo Layout");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • @Andrew_Thompson sorry, it was my fault, system fault – bulak bulaki Apr 01 '15 at 13:58
  • @Andrew_Thompson I think you misunderstood me, In my question, I asked 2 panels, the first panel contains a logo and some text and the second panel (display panel) I'll put a background image, in your solution I cannot do it – bulak bulaki Apr 01 '15 at 14:11
  • *"the first panel contains a logo and some text"* You do realize that a `JLabel` can include with an image and text, right? Further, I advised you to post an MCVE/SSCCE (as oppose to uncompilable code snippets) more than half an hour ago. This is the best answer you'll get from me before I see your MCVE. – Andrew Thompson Apr 01 '15 at 14:16