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)"
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?
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?