当添加更多字符时,我需要更改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(容器窗格){pane.setLayout(新窗格(BoxLayout,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();
}
});
}}
发布于 2009-11-13 05:31:25
textField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);发布于 2009-11-13 02:13:54
我不太明白你的问题。通常使用如下代码创建一个JTextField:
JTextField textField = new JTextField(10);这为文本字段提供了固定的首选大小,该大小取决于所使用的布局管理器。
听起来你好像是在做这样的事情:
JTextField textField = new JTextField();在这种情况下,我认为文本字段没有大小。你甚至可以给它添加字符吗?好吧,也许这种情况下的解决方案是向文本字段添加一个ComponentListener,并跟踪原始大小。每次更改大小时,您都会根据大小的不同来更改文本字段的位置。同样,这可能会工作,也可能不会工作,这取决于布局管理器。
如果您需要更多帮助,请发布您的SSCCE以显示问题。
https://stackoverflow.com/questions/1724204
复制相似问题