算String对象的长度呢?单位要求用像素表示

double dStrLength = Math.Ceiling(this.CreateGraphics().MeasureString(textBox2.Text, textBox2.Font).Width);

在Java中,如果你想要在Swing或AWT库中绘制线,你可以创建一个`Graphics`对象并使用其提供的方法。这里是一个简单的示例,假设在一个`JPanel`上画线: ```java import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Graphics; import java.awt.Color; public class DrawLines { public static void main(String[] args) { JFrame frame = new JFrame("Drawing Lines"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); JPanel panel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); // 清除默认背景 // 绘制四条线 Graphics graphics = g.create(); // 创建图形上下文副本,防止修改原对象影响其他部分 try { graphics.setColor(Color.BLACK); // 设置颜色 // 长度为50像素的线 graphics.drawLine(50, 50, 50 + 50, 50 + 50); graphics.drawLine(frame.getWidth() - 50, 50, frame.getWidth(), 50 + 50); graphics.drawLine(50, frame.getHeight() - 50, 50 + 50, frame.getHeight()); graphics.drawLine(frame.getWidth() - 50, frame.getHeight() - 50, frame.getWidth(), frame.getHeight()); // 长度为20像素的线 graphics.drawLine(70, 70, 70 + 20, 70 + 20); graphics.drawLine(frame.getWidth() - 70, 70, frame.getWidth() - 70 - 20, frame.getHeight()); graphics.drawLine(70, frame.getHeight() - 70, 70 + 20, frame.getHeight() - 70 - 20); graphics.drawLine(frame.getWidth() - 70, frame.getHeight() - 70, frame.getWidth() - 70 - 20, frame.getHeight() - 70 + 20); } finally { graphics.dispose(); // 释放资源 } } }; frame.add(panel); frame.setVisible(true); } } ``` 这个程序会在窗口中显示四个长度为50和20像素的线段。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值