How do I center a JTextfield

后端 未结 3 1301
伪装坚强ぢ
伪装坚强ぢ 2021-01-13 13:13

I\'m having trouble centering a JTexfield in my program.The Textfield does not appear aligned with the JButton. I\'ve tried using the x.setHoriontalAlignment(JTextfield.CENT

3条回答
  •  我在风中等你
    2021-01-13 13:34

     import javax.swing.*;
    import java.awt.*;
    
    public class JTableComponent{
      public static void main(String[] args) 
    {
      new JTableComponent();
      }
    
      public JTableComponent(){
      JFrame // Establish the frame.
      frame = new JFrame("title");
      frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setPreferredSize(new Dimension(700, 700));
    
      // Establish button dimensions.
      Dimension buttonDimension = new Dimension(120, 120);
    
      // Establish Textfield dimensions.
      Dimension textDimension = new Dimension(110, 110);
    
    
      // Create an input text field.
      JTextField textInput = new JTextField(20);
      textInput.setPreferredSize(textDimension);
      textInput.setMinimumSize(textDimension);
      textInput.setMaximumSize(textDimension);
      textInput.setHorizontalAlignment(JTextField.CENTER);
      JPanel  panel = new JPanel();
      panel.add(textInput);
      String string = textInput.getText();
    
    
      // Display the frame and text field.
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      frame.add(textInput);
      }
    }
    

    I have checked your code .its already in middle..

提交回复
热议问题