Results 1 to 2 of 2
  1. #1
    arcelivez is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default JAVA GUI Layout problems...

    Hello,

    so I have a problem with my Swing GUI, I'm trying to create a whiteboard application with a label, then lower the white drawing area (400x250 pixels) and at the bottom 3 buttons, to choose the pen color, well, it doesn't work, what happens is you can just see the three buttons in the gui, so can anyone tell me what I'm doing wrong here?

    The main action happens in the method "public void addComponents(Container pane)"
    Java Code:
    package whiteboard;
    
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import javax.swing.*;
    
    /**
     *
     * @author arturas
     */
    public class MainWindow {
        JPanel layout;
    
        private static void showGUI() {
            //Create and set up the window.
            JFrame window = new JFrame("Shared Whiteboard Client");
            //window.setMinimumSize(new Dimension(600,400));
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            MainWindow crazy = new MainWindow();
            crazy.addComponents(window.getContentPane());
    
            //Display the window.        
            window.pack();
            window.setVisible(true);
            window.setLocationRelativeTo(null);
        }
        
        public void addComponents(Container pane) {
            // It's supposed to be a Whiteboard with a drawing area
    
            // The Title over the drawing area
            JPanel layoutHeader = new JPanel();
            JLabel label1 = new JLabel("Draw in the white area");
            layoutHeader.add(label1);
            layoutHeader.setBackground(Color.yellow);
    
            // The JPanel for the Drawing Area in future to be
            JPanel layoutDrawingArea = new JPanel();
            layoutDrawingArea.setBackground(Color.white);
            layoutDrawingArea.setMinimumSize(new Dimension(400, 250));
    
            // The buttons for choosing pen color
            JButton buttonYellow = new JButton("Yellow");
            JButton buttonGreen = new JButton("Green");
            JButton buttonRed = new JButton("Red");
    
            // The building of the layout, which doesn't work the way I expect it to...
            JPanel layoutButtons = new JPanel();
            layoutButtons.add(buttonYellow);
            layoutButtons.add(buttonGreen);
            layoutButtons.add(buttonRed);
    
            layout = new JPanel(new CardLayout());
            layout.add(layoutHeader, "Please draw in the white area");
            layout.add(layoutDrawingArea, "DrawingArea");
            layout.add(layoutButtons, "Buttons");
            pane.add(layoutHeader, BorderLayout.PAGE_START);
            pane.add(layoutDrawingArea, BorderLayout.CENTER);
            pane.add(layoutButtons, BorderLayout.PAGE_END);
    
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    showGUI();
                }
            });
        }
    }
    Any ideas?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JAVA GUI Layout problems...

    I don't understand your use of a CardLayout-using JPanel and adding components to it. It seems like that it serves no purpose, and worse that it is messing things up so that your other components won't show; so you'll have to explain your rationale behind this. Also, most layout managers respect a component's preferedSize, so this property will likely have to be set on your drawing area. I assume that you're going to use MouseMotionListener and MouseListener (combined as a MouseAdapter) eventually, correct?

Similar Threads

  1. Replies: 13
    Last Post: 12-06-2010, 06:57 AM
  2. Keyboard Layout Problems
    By FrederikNS in forum Eclipse
    Replies: 0
    Last Post: 10-28-2008, 05:03 PM
  3. JPanel / layout problems
    By Warhorsei in forum AWT / Swing
    Replies: 4
    Last Post: 06-04-2008, 06:26 AM
  4. Java GUI layout
    By nick2price in forum New To Java
    Replies: 2
    Last Post: 05-27-2008, 10:42 PM
  5. Use the Layout in java
    By lenny in forum AWT / Swing
    Replies: 2
    Last Post: 07-26-2007, 08:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •