Jump to content

making gui

- - - - -

  • Please log in to reply
3 replies to this topic

#1
MeesterLintwurm

MeesterLintwurm

    Newbie

  • Members
  • Pip
  • 3 posts
Hello,

I am trying to make a gui program(so far so good), but when trying to put an image in a panel, it starts getting wonkey as I want to put the image at a random place.

(for instance I need 5 nodes at different places.)


package test;


import java.awt.*;

import java.awt.event.*;

import java.util.Random;

import javax.swing.*;

import javax.swing.border.LineBorder;


public class test2 extends JFrame implements ActionListener

{

	Random generator = new Random();

	private JTextField textfield;

	private JPanel northPanel = new JPanel();

	private JPanel southPanel = new JPanel();

	private JPanel mainPanel = new JPanel();

	public test2()

	{

		LineBorder lb = new LineBorder(Color.black, 1);

		mainPanel.setBorder(lb);

		setSize(800, 600);

		BorderLayout layout = new BorderLayout();

		setLayout(layout);

		JLabel label2 = new JLabel("Vertex: ");

		southPanel.add(label2);

		textfield = new JTextField(8);

		southPanel.add(textfield);

		JButton button = new JButton("Add Vertex");

		button.addActionListener(this);

		southPanel.add(button);

		add(northPanel, BorderLayout.NORTH);

		add(southPanel, BorderLayout.SOUTH);

		add(mainPanel, BorderLayout.CENTER);

	}

	@Override

	public void actionPerformed(ActionEvent e)

	{

		String text = textfield.getText();

		if (text.equals(""))

		{

			JOptionPane.showMessageDialog(null, "Please enter a vertex name.");

		}

		else

		{

			final JLabel label = new JLabel(new ImageIcon("circle.jpg"));

			label.setSize(label.getIcon().getIconWidth(), label.getIcon().getIconHeight());

			label.setText(textfield.getText());

			mainPanel.add(label);

			final Random random = new Random();

			label.setLocation( random.nextInt(800-150), random.nextInt(600-150));

		}

		validate();

	}

	public static void main(String[] args)

	{

		test2 app = new test2();

		app.setVisible(true);

		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}



Everytime I add a node it goes right next to the previous one... The setLocation(random number, random number) does nothing!

Someone please help .

Thanks

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
You are using a BorderLayout. By definition, these do not allow exact positioning by pixel. The Layout Managers for Swing are very powerful, and you should learn to use them in order to produce clean, well-spaced GUI's.

Nevertheless, if you want to do exact positioning by pixel, here is a link which will teach you how to accomplish this:

Doing Without a Layout Manager (Absolute Positioning) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
MeesterLintwurm

MeesterLintwurm

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks dude! That tut did the job! It is working now!

gregwarner FLAWLESS VICTORY!!

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Lol. You're welcome. :-)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users