如何在 Java Swing 中绘制水平线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3513656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:25:47  来源:igfitidea点击:

How to Draw a Horizontal Line in Java Swing

javauser-interfaceswing

提问by Lalchand

How does one draw a horizontal line using the Java Swing library? I know that I need to override paint(Graphics g)but I am not sure what to put in the method.

如何使用 Java Swing 库绘制一条水平线?我知道我需要覆盖,paint(Graphics g)但我不确定在方法中放什么。

@Override
public void paint(Graphics g)
{
   // What goes here?
}

采纳答案by josefx

Depending on your use case, one of these tutorials should help you:

根据您的用例,这些教程之一应该可以帮助您:

Here an example class which draws a Black line

这是一个绘制黑线的示例类

public class MyLine extends JPanel
{

     @Override public void paint(Graphics g)
     {
         //Get the current size of this component
         Dimension d = this.getSize();

         //draw in black
         g.setColor(Color.BLACK);
         //draw a centered horizontal line
         g.drawLine(0,d.height/2,d.width,d.height/2);
     }
}

回答by Noel M

Just like drawing any other line, except the value for the yaxis doesn't change.

就像绘制任何其他线一样,除了y轴的值不会改变。