java jtextfield 居中_java – 如何使JTextfield居中

第一个:x.setHorizo​​ntalAlignment(JTextField.CENTER);将文本设置在中心而不是JTextField

如果要将JTextField放在中心,只需创建panel = new JPanel();并在其上添加您的按钮像这样:panel.add(button1);

panel.add(BUTTON2);

panel.add(textInput,BorderLayout.CENTER); —>在这里我将textInput设置在jpanel的中心

然后:将’Jpanel’添加到Jframe:frame.add(面板);

试试这个 :

package javaapplication1;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.JPanel;

class WidgetProject implements ActionListener {

//class constants

private static final Color BUTTON_COLOUR1 = Color.WHITE;

private static final int BUTTON_HEIGHT = 75;

private static final int BUTTON_WIDTH = 400;

private static final int TEXTFIELD_HEIGHT = 400;

private static final int TEXTFIELD_WIDTH = 50;

private static final String SECONDS_PER_MINUTE = "Seconds to Minutes or Minutes to Seconds";

private static final String BUTTON2_MODIFIED_LABEL = "yes";

private static final String POUNDS_PER_KILOGRAM = "Pounds to Kilograms or Kilograms to Pounds";

private static final String CHANGE_MY_LABEL = "1";

private static final int HEIGHT = 400;

private static final int WIDTH = 400;

// instance fields

private JButton button1;

private JButton button2;

private JButton button3;

private JButton button4;

private JFrame frame;

private JTextField textInput;

private JPanel panel;

/**

* A free-standing frame with two buttons.

*

* @param title the title of this frame

*/

public WidgetProject(String title) {

// Establish the frame.

frame = new JFrame(title);

panel = new JPanel();

frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));

// Establish button dimensions.

Dimension buttonDimension = new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT);

// Establish Textfield dimensions.

Dimension textDimension = new Dimension(TEXTFIELD_HEIGHT, TEXTFIELD_WIDTH);

// Create and add the first button.

button1 = new JButton(SECONDS_PER_MINUTE);

button1.setActionCommand(CHANGE_MY_LABEL);

button1.addActionListener(this);

button1.setPreferredSize(buttonDimension);

button1.setMinimumSize(buttonDimension);

button1.setMaximumSize(buttonDimension);

button1.setBackground(BUTTON_COLOUR1);

panel.add(button1);

// Create and add the second button.

button2 = new JButton(POUNDS_PER_KILOGRAM);

button2.setActionCommand(CHANGE_MY_LABEL);

button2.addActionListener(this);

button2.setPreferredSize(buttonDimension);

button2.setMinimumSize(buttonDimension);

button2.setMaximumSize(buttonDimension);

button2.setBackground(BUTTON_COLOUR1);

panel.add(button2);

// Create an input text field.

textInput = new JTextField(20);

textInput.setPreferredSize(textDimension);

textInput.setMinimumSize(textDimension);

textInput.setMaximumSize(textDimension);

textInput.setHorizontalAlignment(JTextField.CENTER);

panel.add(textInput, BorderLayout.CENTER);

String string = textInput.getText();

frame.add(panel);

// Display the frame and text field.

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

} // end of constructor ButtonDuo

@Override

public void actionPerformed(ActionEvent e) {

throw new UnsupportedOperationException("Not supported yet.");

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值