Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我怎样才能得到按钮的正确位置?(Java,JButton)_Java_Swing_Jbutton - Fatal编程技术网

我怎样才能得到按钮的正确位置?(Java,JButton)

我怎样才能得到按钮的正确位置?(Java,JButton),java,swing,jbutton,Java,Swing,Jbutton,我创建了一个带有面板的简单框架,该框架具有网格布局管理器。当我使用覆盖的paintComponent(g:Graphics)方法创建两个自定义按钮时,一个按钮的背景绘制错误 我想这是因为位置总是x=0,y=0。我怎样才能解决这个问题 按钮代码: public class JRoundedButton extends JButton { private int arcRadius; public JRoundedButton(String label, int arcRadius) {

我创建了一个带有面板的简单框架,该框架具有网格布局管理器。当我使用覆盖的paintComponent(g:Graphics)方法创建两个自定义按钮时,一个按钮的背景绘制错误

我想这是因为位置总是x=0,y=0。我怎样才能解决这个问题

按钮代码:

public class JRoundedButton extends JButton {

private int arcRadius;

public JRoundedButton(String label, int arcRadius) {
    super(label);
    this.setContentAreaFilled(false);
    this.arcRadius = arcRadius;
}

@Override
protected void paintComponent(Graphics g) {
    if (g instanceof Graphics2D) {
        //super.paintComponent(g);

        Graphics2D graphics = (Graphics2D) g;
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //Draw button background
        graphics.setColor(getBackground());
        graphics.fillRoundRect(getX(), getY(), getWidth() - 1, getHeight() - 1, arcRadius, arcRadius);

        //Draw font
        this.paintText(graphics);
    }
}


protected final void paintText(@NotNull Graphics2D g) {
    //Draw font
    g.setColor(getForeground());
    if (this.getFont() != null && this.getText() != null) {
        FontMetrics fm = getFontMetrics(getFont());
        g.setColor(this.getForeground());
        g.drawString(this.getText(), ((this.getWidth() / 2) - (fm.stringWidth(this.getText()) / 2)),
                ((this.getHeight() / 2) + fm.getMaxDescent()));
    }
}
下面是帧创建代码:

public static void main(String[] args) {
    JFrame frame = new JFrame("Buttontest");
    frame.setSize(new Dimension(500, 500));
    frame.setLayout(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel contentPanel = new JPanel();
    contentPanel.setSize(new Dimension(500, 500));
    contentPanel.setLayout(new GridLayout(2, 1, 0, 20));

    JRoundedButton button1 = new JRoundedButton("Rounded Button", 40);
    button1.setForeground(Color.YELLOW);
    button1.setBackground(Color.GREEN);
    JRoundedButton button2 = new JRoundedButton("Rounded Button 2", 40);
    button2.setForeground(Color.WHITE);
    button2.setBackground(Color.BLACK);

    contentPanel.add(button1);
    contentPanel.add(button2);

    frame.add(contentPanel, BorderLayout.CENTER);

    frame.setVisible(true);
}

如果我执行该按钮,上面的按钮将按预期显示,但下面的按钮不显示黑色背景。为什么会这样?

快速浏览,我注意到这是错误的:

graphics.fillRoundRect(getX(), getY(), getWidth() - 1, getHeight() - 1, arcRadius, arcRadius);
了解
getX()
getY()
都返回组件相对于其父组件的位置,通常是按钮所在的JPanel,但这不是您应该绘制的位置。相反,图形应该位于相对于按钮本身的位置

因此,这可能会更好:

graphics.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, arcRadius, arcRadius);

你太棒了!这花了我好几天的时间,我没有注意到。谢谢你的回答!祝你过得愉快;)