如何在 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
How to Draw a Horizontal Line in Java Swing
提问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:
根据您的用例,这些教程之一应该可以帮助您:
- The Java Graphics APITutorial => drawing in a swing component
- The Java SwingTutorial => using swing components (such as JSeparator)
- Java图形 API教程 => 在摆动组件中绘图
- Java Swing教程 => 使用 Swing 组件(例如JSeparator)
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 y
axis doesn't change.
就像绘制任何其他线一样,除了y
轴的值不会改变。