java如何使得画的线上添加一个文本框(即在画布上添加一个文本框)
1个回答
展开全部
import javax.swing.*;
import java.awt.*;
/**
* @author Hardneedl
*/
final class BoxAndLineDemo extends JFrame {
public String getTitle() {return "BoxAndLineDemo";}
static private final Dimension size = new Dimension(600,400);
public Dimension getPreferredSize() {return size;}
public Dimension getMaximumSize() {return size;}
public Dimension getMinimumSize() {return size;}
public Dimension getSize(){return size;}
/*画笔*/
private interface Brush{
void doPaint(Graphics g);
}
/*抽象画布,依赖于画布配置的画笔执行绘制工作*/
abstract private class Canvas extends JComponent{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Brush b = getBrush();
if (b!=null){
b.doPaint(g);
}
}
abstract Brush getBrush();
}
private class _Canvas extends Canvas {
private Brush b;
private _Canvas() {
b = new Brush() {
public void doPaint(Graphics g) {
Graphics g2 = g.create();
g2.setColor(Color.BLACK);
g2.drawLine(100, 100, 30, 50);
g2.dispose();
}
};
}
Brush getBrush() {
return b;
}
}
private Canvas canvas = new _Canvas();
BoxAndLineDemo() throws HeadlessException {
attachListeners();
doLay();
}
private void attachListeners(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void doLay(){
Container container = getContentPane();
JTextField field=new JTextField("text field",50);
field.setBounds(20,20,100,25);
canvas.add(field);
container.add(canvas,BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String[] args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
new BoxAndLineDemo();
}
}
);
}
}
import java.awt.*;
/**
* @author Hardneedl
*/
final class BoxAndLineDemo extends JFrame {
public String getTitle() {return "BoxAndLineDemo";}
static private final Dimension size = new Dimension(600,400);
public Dimension getPreferredSize() {return size;}
public Dimension getMaximumSize() {return size;}
public Dimension getMinimumSize() {return size;}
public Dimension getSize(){return size;}
/*画笔*/
private interface Brush{
void doPaint(Graphics g);
}
/*抽象画布,依赖于画布配置的画笔执行绘制工作*/
abstract private class Canvas extends JComponent{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Brush b = getBrush();
if (b!=null){
b.doPaint(g);
}
}
abstract Brush getBrush();
}
private class _Canvas extends Canvas {
private Brush b;
private _Canvas() {
b = new Brush() {
public void doPaint(Graphics g) {
Graphics g2 = g.create();
g2.setColor(Color.BLACK);
g2.drawLine(100, 100, 30, 50);
g2.dispose();
}
};
}
Brush getBrush() {
return b;
}
}
private Canvas canvas = new _Canvas();
BoxAndLineDemo() throws HeadlessException {
attachListeners();
doLay();
}
private void attachListeners(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void doLay(){
Container container = getContentPane();
JTextField field=new JTextField("text field",50);
field.setBounds(20,20,100,25);
canvas.add(field);
container.add(canvas,BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String[] args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
new BoxAndLineDemo();
}
}
);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |