开发者

JButton move text

开发者 https://www.devze.com 2023-02-22 02:26 出处:网络
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..

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

精彩评论

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

关注公众号