私はJLabelを持っています(実際には、JXLabelです)。
アイコンとテキストを配置しました。
<icon><text>
次に、コンポーネントの左側に次のように間隔を追加します。
<space><icon><text>
JLabelを移動したり、画像を変更して間隔を追加したりする提案は受け入れません。
プレーンなJavaコードでそれを行う方法を知りたいだけです。
私は解決策を見つけました!
setBorder(new EmptyBorder(0,10,0,0));
みんな、ありがとう!
このように:あまり明確ではありませんが、ラベルに特定の幅の透明な境界線を追加することで間隔を追加できます
ラベルをコンテナの片側に押し付けようとしている場合は、接着剤を追加できます。このようなもの:
JPanel panel = new JPanel();
panel.setLayoutManager(new BoxLayout(panel, BoxLayout.LINE_AXIS);
panel.add(new JLabel("this is your label with it's image and text"));
panel.add(Box.createHorizontalGlue());
あなたの質問はあまり明確ではありませんが。
JLabel の preferredSize を変更する必要はありません。GridBagLayout Manager を使用してコンポーネント間の分離を指定できます。コンテナで GridBagLayout を使用し、左側にインセットを指定する GridBagConstraints オブジェクトを使用して JXLabel を追加するだけです。
JPanel panel=new JPanel(new GridBagLayout());
JLabel label=new JLabel("xxxxx");
GridBagConstraints constraints=new GridBagConstraints();
constraints.insest.left=X; // X= number of pixels of separation from the left component
panel.add(label,constraints);
制約の設定で多くの構成プロパティを省略したことに注意してください。GridBagLayoutのドキュメントをよく読んでください。