当添加更多字符时,我需要更改 JTextField 对象的增长方向。目前,当我向其中添加更多内容时,它会从左到右增长,但我需要 JTextField 的边界从右到左增长。例如,当我向这个 JTextField 添加“StackOverflow”时,o/p 是,
<empty space>StackOverflow
但我想要,
StackOverflow<empty space>
你们能帮我解决这个问题吗?我试过 setHorizontalAlignment。但它不起作用。谢谢你的帮助。
编辑:添加 SSCCE 以获得更好的解释。
导入 java.awt.Container;导入 javax.swing.BoxLayout;导入 javax.swing.JButton;导入 javax.swing.JFrame;导入 javax.swing.JTextField;
公共类 JTextFieldExample { public static void addComponentsToPane(Container pane) { pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
JTextField transitionEditorJTextField = new JTextField("StackOverFlow");
pane.add(transitionEditorJTextField);
System.out.println("If I add text to JTextFiled notice that it grows towards Right - which is normal. "
+ "But I want it to grow towards left.");
JButton button = new JButton("Button.I.Am");
pane.add(button);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("BoxLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}