【发布时间】:2010-12-16 00:04:08
【问题描述】:
当添加更多字符时,我需要更改 JTextField 对象的增长方向。目前,当我向其中添加更多内容时,它会从左到右增长,但我需要 JTextField 的边界从右到左增长。 例如当我将“StackOverflow”添加到这个 JTextField 时,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 { 公共静态无效 addComponentsToPane(容器窗格){ 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();
}
});
}
}
【问题讨论】:
标签: java swing jtextfield direction