JLabelで水平方向のギャップを追加する方法
質問
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 マネージャーは、コンポーネント間の分離を指定します。コンテナでGridBagLayoutを使用し、 GridBagConstraints 左のインセットを指定するオブジェクト:
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
所属していません StackOverflow