如何将我的 JTextField 居中对齐
How to align my JTextField to center
所以我有两个面板,第一个包含文本字段和标签:
public class Panel1 extends JFrame implements ActionListener {
//Common variables
JTextField val;
public Component panel1() {
// Panel 1 to add labels and textfields
final JPanel order = new JPanel(new GridLayout(1, 5));{
order.add(new JLabel("Current Value:", JLabel.CENTER));
order.add(val = new JTextField(3));
另一个包含按钮的面板:
JPanel buttons = new JPanel();
JButton add;
buttons.add(add=new JButton("+"));
add.setToolTipText("Pressing the button will find total cost");
add.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
}
});
JButton sub;
buttons.add(sub = new JButton("-"));
sub.setToolTipText("Pressing the button will save the record to .csv file");
sub.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
}
});
JButton reset;
buttons.add(reset=new JButton("Clear"));
reset.setToolTipText("Pressing the button will clear data entered into the screen");
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
JButton quit;
buttons.add(quit= new JButton("Quit"));
quit.setToolTipText("Pressing the button will close the window");
quit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
System.exit(0);
}
});
// Panel to combine rest of the panels to one
JPanel main = new JPanel();
main.setLayout(new GridBagLayout());
main.add(order);
main.add(buttons);
return main;}
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
问题是我的文本域没有对齐。 IT SIS 排成一排,按钮如下:
_______ ________ ________ _______
Current Value:|_______| |____+___| |___-____| |__reset|
我希望 Jtextfield(当前值)位于中间,而不是按钮。
使用另一个布局管理器。
尝试替换
JPanel order = new JPanel(new GridLayout(1, 5));
和
JPanel order = new JPanel(new FlowLayout());
所以我有两个面板,第一个包含文本字段和标签:
public class Panel1 extends JFrame implements ActionListener {
//Common variables
JTextField val;
public Component panel1() {
// Panel 1 to add labels and textfields
final JPanel order = new JPanel(new GridLayout(1, 5));{
order.add(new JLabel("Current Value:", JLabel.CENTER));
order.add(val = new JTextField(3));
另一个包含按钮的面板:
JPanel buttons = new JPanel();
JButton add;
buttons.add(add=new JButton("+"));
add.setToolTipText("Pressing the button will find total cost");
add.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
}
});
JButton sub;
buttons.add(sub = new JButton("-"));
sub.setToolTipText("Pressing the button will save the record to .csv file");
sub.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
}
});
JButton reset;
buttons.add(reset=new JButton("Clear"));
reset.setToolTipText("Pressing the button will clear data entered into the screen");
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
JButton quit;
buttons.add(quit= new JButton("Quit"));
quit.setToolTipText("Pressing the button will close the window");
quit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
System.exit(0);
}
});
// Panel to combine rest of the panels to one
JPanel main = new JPanel();
main.setLayout(new GridBagLayout());
main.add(order);
main.add(buttons);
return main;}
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
问题是我的文本域没有对齐。 IT SIS 排成一排,按钮如下:
_______ ________ ________ _______
Current Value:|_______| |____+___| |___-____| |__reset|
我希望 Jtextfield(当前值)位于中间,而不是按钮。
使用另一个布局管理器。
尝试替换
JPanel order = new JPanel(new GridLayout(1, 5));
和
JPanel order = new JPanel(new FlowLayout());