第一个是x.sethorizontalalignment (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 Java APP 1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import Java.awt.event.actionevent;
import Java.awt.event.action listener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPanel;
class坚强的草丛implements ActionListener {
//class constants
privatestaticfinalcolorbutton _ colour1=color.white;
privatestaticfinalintbutton _ height=75;
privatestaticfinalintbutton _ width=400;
privatestaticfinalinttextfield _ height=400;
privatestaticfinalinttextfield _ width=50;
privatestaticfinalstringseconds _ per _ minute=' secondstominutesorminutestoseconds ';
privatestaticfinalstringbutton2_ modified _ label=' yes ';
privatestaticfinalstringpounds _ per _ kilogram=' poundstokilogramsorkilogramstopounds ';
privatestaticfinalstringchange _ my _ label='1';
privatestaticfinalintheight=400
private static final int WIDTH=400;
//实例字段
私有jbutton button 1;
私有jbutton button 2;
私有jbutton button 3;
私有jbutton button 4;
私有jframe frame;
private JTextField textInput;
私有jpanel panel;
//*
大草丛(String title )。
//Establish the frame。
frame=newjframe(title );
panel=new JPanel (;
frame.set layout (newboxlayout (frame.get content pane ),BoxLayout.Y_AXIS );
frame.setdefaultcloseoperation (jframe.exit _ on _ close );
frame.setpreferredsize (new dimension ) width,HEIGHT );
//Establish button dimensions。
dimensionbuttondimension=new dimension (button _ width,BUTTON_HEIGHT );
//establish文本字段限制。
dimensiontextdimension=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 (button dimension;
button1. setminimumsize (button dimension );
button1. setmaximumsize (button dimension );
button1. set background (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 (button dimension;
button2. setminimumsize (button dimension );
button2. setmaximumsize (button dimension;
button2. set background (button _ colour1);
Panel.add(Button2;
//Create an input text field。
textinput=newjtextfield(20;
textinput.setpreferredsize (text dimension );
textinput.setminimumsize (text dimension;
textinput.setmaximumsize (text dimension;
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体贴的柚子/p @Override
公共语音格式(actionperformed ) {
thrownewunsupportedoperationexception (' notsupportedyet.' );
}
}