I'd like to know how i can change the position of a JTextField. If I do like this:
top.add(tdate);
it puts the JTextField on the same direction/line that my JComboBox. I'd like to change the Y and X position of my JTextField. Can you tell me how can I do this?
//Button CreateM
public class ActionMariage extends JFrame implements ActionListener
{
JComboBox combo1 = new JComboBox();
JComboBox combo2 = new JComboBox();
JComboBox combo3 = new JComboBox();
JComboBox combo4 = new JComboBox();
public void actionPerformed(ActionEvent evt)
{
JPanel container = new JPanel();
JButton bAjouterm = new JButton ("Ajouter");
JTextField tdate=new JTextField("Entrez la date du mariage");
JLabel labelm = new JLabel("Date du mariage :");
this.setTitle("Filliations");
this.setSize(1000, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
container.setBackground(Color.white);
container.setLayout(new BorderLayout());
combo1 = new JComboBox(tab3);
combo2 = new JComboBox(tab1);
combo3 = new JComboBox(tab3);
combo4 = new JComboBox(tab2);
combo1.setPreferredSize(new Dimension(100,20));
combo1.setForeground(Color.black);
combo2.setPreferredSize(new Dimension(300,20));
combo2.setForeground(Color.black);
combo3.setPreferredSize(new Dimension(100,20));
combo3.setForeground(Color开发者_C百科.black);
combo4.setPreferredSize(new Dimension(300,20));
combo4.setForeground(Color.black);
JPanel top = new JPanel();
top.add(combo1);
top.add(combo2);
top.add(combo3);
top.add(combo4);
top.add(bAjouterm);
top.add(bModifierm);
top.add(labelm);
top.add(tdate);
combo1.addActionListener(new cm1());
combo2.addActionListener(new cp1());
combo3.addActionListener(new cm2());
combo4.addActionListener(new cp2());
bAjouterm.addActionListener(new ajouterm());
container.add(top, BorderLayout.NORTH);
this.setContentPane(container);
this.setVisible(true);
}
}
Swing uses layout managers to layout components inside their container. Read the swing tutorial about layout managers.
First of all you are putting the gui construction method (new JTextField, new JComboBox, add..) inside the actionPerformed method. That's quite strange. To position correctly the component in swing you should use a proper LayoutManager (not a null layout). Have a look at this tutorial.
If you want to change x and y position of some controls,you have to set layout to null and use setBounds(int left,int top,int right ,int bottom) method after adding controls to set it's x and y position.
精彩评论