开发者

JButton move text

I'm searching for a solution/alternative for my JButton Problem. My buttontext isn't matching 开发者_如何转开发correctly with my "keyboard key picture". The text is a little too low..

JToggleButton tglbtn_newLine = new JToggleButton("0");
tglbtn_newLine.setBackground(new Color(240,240,240));
tglbtn_newLine.setBorderPainted(false);
tglbtn_newLine.setIcon(new ImageIcon(/*Picture of a key*/);
//should be 2..3 pix above the CENTER position:
tglbtn_newLine.setVerticalTextPosition(SwingConstants.CENTER);
tglbtn_newLine.setHorizontalTextPosition(SwingConstants.CENTER);
GridBagConstraints gbc_tglbtn_newLine = new GridBagConstraints();
gbc_tglbtn_newLine.insets = new Insets( 0, 0, 5, 5);
tglbtn_newLine.setMargin(new Insets(-2, -2, -2, -2));
gbc_tglbtn_newLine.gridx = 4;
gbc_tglbtn_newLine.gridy = 5;
controlPanel.add(tglbtn_newLine, gbc_tglbtn_newLine);

Is there a possibility to set the text to a specific y-position.

Example pic:

JButton move text


You might get some ideas from this example that uses setBorderPainted(false) and paintComponent() to precisely control placement and scale.


Ok I tried and tried... nothing seems to work.

I solved it now, that I resized the picture about 5 pixels. So the CENTER will be higher. Finally setMargin(new Insets(-8, -2, -2, -2)); an now it should work with dynamic text. It's not perfect but it works :/

Thx for your answers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜