Please help with GUI problem, can't set size

As it is right now all the Textfields are gigantic and not a small rectangle as I want to...

This doesn't do what Iit's told:

JTextField consumer1 = new JTextField(2);

consumer1.setBounds(new Rectangle(0, 10, 10, 10));

Could someone please give me a hint?

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.WindowAdapter;

import java.util.Scanner;

import javax.swing.*;

import javax.swing.Timer;

import java.util.*;

import java.awt.event.*;

import javax.swing.border.*;

/**

* @author

*

*/

publicclass Guiextends JFrame//implements ActionListener

{

public Consumer c1;

public Consumer c2;

public Consumer c3;

public Producer p1;

public Producer p2;

public Producer p3;

public Buffer buffer;

private JButton startButton;

private JPanel buttonPanel, producerPanel, consumerPanel, bufferPanel,

consumerButtonpanel, producerButtonpanel, bufferButtonpanel;

private Container container;

private JLabel producerLabel =new JLabel("Producer is not producing.");

private JLabel consumerLabel =new JLabel("Consumer is not consuming.");

private JLabel bufferLabel =new JLabel("Buffer is not working.");

public Gui(Buffer buffer, Consumer c1, Consumer c2, Consumer c3, Producer p1, Producer p2, Producer p3)

{

this.buffer = buffer;

this.c1 = c1;

this.c2 = c2;

this.c3 = c3;

this.p1 = p1;

this.p2 = p2;

this.p3 = p3;

setLayout(new FlowLayout());

getContentPane().setBackground(Color.lightGray);

container = getContentPane();

container.setLayout(new GridLayout(1,3,1,2));

JTextField consumer1 =new JTextField(2);

consumer1.setBounds(new Rectangle(0, 10, 10, 10));

JTextField consumer2 =new JTextField(2);

consumer2.setBounds(new Rectangle(0, 10, 10, 10));

JTextField consumer3 =new JTextField(2);

consumer3.setBounds(new Rectangle(0, 10, 10, 10));

JTextField bufferTextfield =new JTextField(2);

JTextField producer1 =new JTextField(2);

JTextField producer2 =new JTextField(2);

JTextField producer3 =new JTextField(2);

JTextField getBufferTime =new JTextField(2);

JTextField getConsumerTime =new JTextField(2);

JTextField getProducerTime =new JTextField(2);

JButton consumeButton =new JButton("Set consume-time");

JButton produceButton =new JButton("Set produce-time");

JButton bufferButton =new JButton("Set buffer-time");

producerPanel=new JPanel(new GridLayout(3,0));

consumerPanel=new JPanel(new GridLayout(3,0));

bufferPanel=new JPanel(new GridLayout(2,0));

consumerButtonpanel =new JPanel(new GridLayout());

producerButtonpanel =new JPanel(new GridLayout());

bufferButtonpanel =new JPanel(new GridLayout());

producerPanel.setPreferredSize(new Dimension(300,700));

consumerPanel.setPreferredSize(new Dimension(300,700));

bufferPanel.setPreferredSize(new Dimension(300,700));

consumerButtonpanel.setPreferredSize(new Dimension(5,5));

producerButtonpanel.setPreferredSize(new Dimension(5,5));

bufferButtonpanel.setPreferredSize(new Dimension(5,5));

container.add(producerPanel,BorderLayout.WEST);

container.add(bufferPanel,BorderLayout.CENTER);

container.add(consumerPanel,BorderLayout.EAST);

consumerPanel.add(consumerButtonpanel);

producerPanel.add(producerButtonpanel);

bufferPanel.add(bufferButtonpanel);

consumerPanel.add(consumerLabel);

consumerPanel.add(consumer1);

consumerPanel.add(consumer2);

consumerPanel.add(consumer3);

consumerPanel.setBackground(Color.LIGHT_GRAY);

consumerPanel.add(consumeButton);

bufferPanel.add(bufferLabel);

bufferPanel.add(bufferTextfield);

bufferPanel.setBackground(Color.LIGHT_GRAY);

bufferPanel.add(bufferButton);

producerPanel.add(producerLabel);

producerPanel.add(producer1);

producerPanel.add(producer2);

producerPanel.add(producer3);

producerPanel.add(produceButton);

producerPanel.setBackground(Color.LIGHT_GRAY);

consumerButtonpanel.add(consumeButton);

consumerButtonpanel.add(getConsumerTime);

bufferButtonpanel.add(bufferButton);

bufferButtonpanel.add(getBufferTime);

producerButtonpanel.add(produceButton);

producerButtonpanel.add(getProducerTime);

pack();

//setResizable(false);

setVisible(true);

setSize(900, 500);

setLocation(100, 100);

setTitle("Producer-Consumer");

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}

[7352 byte] By [_Robb_] at [2007-11-26 12:16:50]
# 1
Switch from GridLayout to GridBagLayoutKaj
kajbj at 2007-7-7 14:53:29 > top of Java-index,Archived Forums,Socket Programming...
# 2
Thanks, I had GridBagLayout at first, but I didn't understand it very well. I'll make another try.
_Robb_ at 2007-7-7 14:53:29 > top of Java-index,Archived Forums,Socket Programming...
# 3

> Thanks, I had GridBagLayout at first, but I didn't

> understand it very well. I'll make another try.

You can download JBuilder foundation. It's free and has an UI builder which is pretty easy to understand. You can use it to layout components in GrigbagLayout. It makes it a bit easier to understand how GridbagLayout works.

Kaj

kajbj at 2007-7-7 14:53:29 > top of Java-index,Archived Forums,Socket Programming...
# 4
That would be very useful! Is it possible to get it as a plugin to Eclipse somehow?
_Robb_ at 2007-7-7 14:53:29 > top of Java-index,Archived Forums,Socket Programming...
# 5
First you have to have a main method (I think you don't have it in other classes)create an object in the main of type GUIor if you have the main methodtry to put the setSize and setVisible at the end of the code%%%
G_Abubakr at 2007-7-7 14:53:29 > top of Java-index,Archived Forums,Socket Programming...
# 6
> That would be very useful! Is it possible to get it> as a plugin to Eclipse somehow?What, JBuilder? It's an IDE by itself. Eclipse will have GUI bilder plugins available, too, but I think Kaj endorsed JBuilder for the quality of the generated code.
CeciNEstPasUnProgrammeur at 2007-7-7 14:53:29 > top of Java-index,Archived Forums,Socket Programming...
# 7

Call me antiquated whoever wants, but i prefer (if I have time, obviously) to set the layout as null. If you do this, you can dimension and place the components as you really want. The disvantage is that you must set the dimensions and location (setBound method) of all gui components inserted into the container which had its layout setted to null.

Yes, yes... It's a time consuming way, i admit... but i like to have full control of the size and location of my gui components...

Container c = myJFrameOrSomethingLikeThat.getContentPane();

c.setLayout(null);

myJTextField.setBounds(xPosition, yPosition, width, height);

...

Have fun!

Lumantu at 2007-7-7 14:53:29 > top of Java-index,Archived Forums,Socket Programming...
# 8

> Yes, yes... It's a time consuming way, i admit... but

> i like to have full control of the size and location

> of my gui components...

As soon as I go and resize the window or change the system font to use a different size (in fact, I did), you're hosed. You don't have full control.

CeciNEstPasUnProgrammeur at 2007-7-7 14:53:29 > top of Java-index,Archived Forums,Socket Programming...
# 9

> As soon as I go and resize the window or change the

> system font to use a different size (in fact, I did),

> you're hosed. You don't have full control.

As I said before, I know that way can be cumbersome... I work aroud those problems you mentioned using something like a repository of constants (An abstract class, an interface, an properties file, whatever...) to dimension my components, and those dimentions have their calculation tied to each other. It give me back some control, but, as you said, not full control...

I guess it's a matter of taste. I preffer that way - maybe because I am a old man full of crazes... lol... Here, in Brazil we have a saying: "Taste is like nose: Each one have its own" lol...

Lumantu at 2007-7-7 14:53:29 > top of Java-index,Archived Forums,Socket Programming...
# 10
In the US that saying is somewhat more vulgar:Opinions are like assholes, everybody has one.~Tim :)
SomeoneElse at 2007-7-7 14:53:29 > top of Java-index,Archived Forums,Socket Programming...
# 11

> In the US that saying is somewhat more vulgar:

>

> Opinions are like assholes, everybody has one.

>

> ~Tim :)

Here too!!! lol... :o)

"Tasteislikeas*h*le:Each onehaveits own"

"Gosto?que nemcu:Cada umtemo seu"

null

Lumantu at 2007-7-7 14:53:29 > top of Java-index,Archived Forums,Socket Programming...