【问题标题】:Remove gap between buttons and rounded corners消除按钮和圆角之间的间隙
【发布时间】:2014-07-01 22:00:29
【问题描述】:

我有一个这个 jpanel,里面有 2 个按钮

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

现在我正在使用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);

【问题讨论】:

  • 请发布您的代码..

标签: java swing layout


【解决方案1】:

尝试使用setHgapsetVgap 归零。

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

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

【讨论】:

  • 我在构造函数 new GridLayout(1, 2, 0, 0) 中设置了它,但差距仍然存在。
  • 不要在构造函数中使用它——试试langLayout.setHgap(0);langLayout.setVgap(0);
  • 用 langLayout.setHgap(-5) 设置;并且 langLayout.setVgap(0) 适用于间隙
【解决方案2】:

取一个实现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(radius)。

希望这对您有所帮助。

【讨论】:

  • 请更正答案的格式、间距和缩进。
猜你喜欢
  • 2015-10-15
  • 2016-04-02
  • 2012-10-31
  • 2011-09-05
  • 1970-01-01
  • 2010-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多