如何更改 JTextField 的位置
我想知道如何更改 JTextField 的位置。如果我这样做:
top.add(tdate);
它将 JTextField 放在与我的 JComboBox 相同的方向/线上。我想更改 JTextField 的 Y 和 X 位置。你能告诉我该怎么做吗?
//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.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);
}
}
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.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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Swing 使用布局管理器在其容器内布局组件。阅读有关布局管理器的 Swing 教程。
Swing uses layout managers to layout components inside their container. Read the swing tutorial about layout managers.
首先,您将 gui 构造方法(new JTextField、new JComboBox、add..)放入 actionPerformed 方法中。这很奇怪。要正确定位 swing 中的组件,您应该使用正确的 LayoutManager (而不是空布局)。看看这个教程。
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.
如果要更改某些控件的 x 和 y 位置,则必须将布局设置为 null 并在添加控件后使用 setBounds(int left,int top,int right ,int Bottom) 方法来设置其 x 和 y 位置。
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.