消除按钮和圆角之间的间隙

时间:2014-07-01 13:23:15

标签: java swing layout

我有一个带有2个按钮的jpanel

enter image description here

我想删除按钮之间的空格,并删除圆角,使其看起来像这样

enter image description here

现在我正在使用GridLayout(1,2,0,0)

修改

    JButton englishBtn = new JButton();
    JButton polishBtn = new JButton();

    Font verdana = new Font("Verdana", Font.PLAIN, 11);
    englishBtn.setText("EN");
    englishBtn.setFont(verdana);
    englishBtn.setFocusable(false);
    englishBtn.setBackground(new Color(5, 74, 127));
    englishBtn.setForeground(Color.WHITE);

    polishBtn.setText("PL");
    polishBtn.setFont(verdana);
    polishBtn.setFocusable(false);
    polishBtn.setBackground(new Color(153, 0, 0));
    polishBtn.setForeground(Color.WHITE);

    JPanel langPanel = new JPanel();
    GridLayout langLayout = new GridLayout(1, 2, 0, 0);
    langPanel.setLayout(langLayout);
    langPanel.add(englishBtn);
    langPanel.add(polishBtn);

2 个答案:

答案 0 :(得分:2)

尝试使用setHgapsetVgap归零。

本文档可能对您有所帮助 -

http://download.java.net/jdk7/archive/b123/docs/api/java/awt/GridLayout.html#setHgap(int)

答案 1 :(得分:1)

按照以下方式获取一个包含Border接口的类

  

class RoundedBorder implements Border {
int radius;
RoundedBorder(int radius) {
this.radius = radius;
}
public Insets getBorderInsets(Component c) {
return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);
}
public boolean isBorderOpaque() {
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
g.drawRoundRect(x,y,width-3,height-3,radius,radius);
g.setColor(Color.red);
c.setForeground(Color.red);
}
}

现在在按钮的属性上,即button.set“b1.setBorder(new RoundedBorder(50)); “将按钮中所需的半径作为参数传递给RoundedBorder(半径)。

希望这有助于你。