Swing 101 question

Dear all,

I am playing with Swing. I plan to create a button and a textfield. I can create both of them on a Panel. The question is how to control the locations of both of them. I spend an hour and find out how to switch the locations between textarea and button, but the way is very stupid. It is because I change the order of the creation of textarea and button. Could you please tell me how should I control the location of those components? I have attached my code in the following! thanks

package im;

import javax.swing.AbstractButton;

import javax.swing.JButton;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.ImageIcon;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import javax.swing.JTextField;

publicclass Swingextends JPanel

implements ActionListener{

protected JButton SendButton;

public Swing(){

final JTextField tf =new JTextField("press <enter>", 30);

//tf.setHorizontalAlignment(JTextField.LEFT);

//tf.setBounds(100,300,35,30);

//tf.setSize(400,400);

add(tf);

SendButton =new JButton("Send");

//SendButton.setVerticalTextPosition(AbstractButton.CENTER);

//SendButton.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales

//SendButton.setMnemonic(KeyEvent.VK_D);

// SendButton.setActionCommand("disable");

//SendButton.setSize(100,100);

//SendButton.setHorizontalAlignment(JButton.RIGHT);

SendButton.addActionListener(this);

add(SendButton);

}

publicvoid actionPerformed(ActionEvent e){

}

privatestaticvoid createAndShowGUI(){

//Create and set up the window.

JFrame frame =new JFrame("IM");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

Swing newContentPane =new Swing();

newContentPane.setOpaque(true);//content panes must be opaque

frame.setContentPane(newContentPane);

//Display the window.

frame.pack();

frame.setVisible(true);

frame.setSize(550,400);

}

publicstaticvoid main(String[] args){

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

createAndShowGUI();

}

});

}

}

[4600 byte] By [marco_wua] at [2007-11-27 9:54:37]
# 1
You use Layout Managers. Read the Swing tutorial here: http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html
camickra at 2007-7-13 0:24:33 > top of Java-index,Desktop,Core GUI APIs...