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;
public class JTextFieldExample {public static void addComponentsToPane(Container panel){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();
}
});
}
}