泪求程序 在一个窗口里有两个按钮 矩形 和圆 点击一个 自动画图形

sk3212635 2009-12-07 09:16:05
哥哥姐姐帮忙拉
...全文
77 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
gentalguo 2009-12-07
  • 打赏
  • 举报
回复
这问题描述的。罢了,刚好闲着,也算练练敲键盘的速度。
你不是泪求吗?好,代码给你,希望你继续哭下去哈~~



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GraphicDemo extends JFrame {

private static final long serialVersionUID = 1L;
GraphicPanel gp;

public GraphicDemo() {
super();

Container c = getContentPane();
c.setLayout(new BorderLayout());
gp = new GraphicPanel();
gp.setBorder(BorderFactory.createLineBorder(Color.black));
c.add(gp);

JPanel p = new JPanel();
p.setLayout(new FlowLayout());
JButton rButton = new JButton("Rect");
JButton oButton = new JButton("Oval");
rButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
gp.setIsRect(true);
gp.repaint();
}
});

oButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gp.setIsRect(false);
gp.repaint();
}
});

p.add(rButton);
p.add(oButton);
c.add(p, BorderLayout.SOUTH);

setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}

private class GraphicPanel extends JPanel {

private static final long serialVersionUID = 1L;
boolean isRect = false;

public GraphicPanel() {
super();

}

public void setIsRect(boolean flag) {
isRect = flag;
}

@Override
public void paint(Graphics g) {
super.paint(g);
Rectangle r = getBounds();
Rectangle rect = new Rectangle();
rect.width = rect.height = (int) (Math.min(r.width, r.height) * 0.8);
rect.x = (r.width - rect.width) / 2;
rect.y = (r.height - rect.height) / 2;
if (isRect) {
g.drawRect(rect.x, rect.y, rect.width, rect.height);
} else {
g.drawOval(rect.x, rect.y, rect.width, rect.height);
}
}
}

public static void main(String[] args) {
new GraphicDemo();
}
}

boboo_2000_0 2009-12-07
  • 打赏
  • 举报
回复
楼主可以使用Java AWT里面Graghic类提供的画图方法画,很简单的。

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧