java 按钮 点击按钮 出现一个图像 (直线或者矩形之类的最简单的2D)
我自己的程序实在编写不出来了图像只能编写出静态的图像动态的不是便不出来就是添加按钮的时候出不来因为我的整个页面弄得布局都是自定义的setlayout是null然后响应图像...
我自己的程序 实在编写不出来了 图像只能编写出静态的图像 动态的不是便不出来 就是添加按钮的时候出不来 因为我的整个页面弄得布局都是自定义的 setlayout是null 然后响应图像的时候响应不出来 我是实在没辙了 哪位高手能帮帮我 我的QQ 382274224 验证答案 齐向猛 多谢!!!!
展开
展开全部
写了一个简单的例子,你自己看看对你有没有帮助。我这个窗口刷新后图像就不见了,如果你想改变这个可以自己写个类继承Canvas然后再重写paint方法,道理都是一样的。
import java.awt.Canvas;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Draw extends JFrame {
private static final long serialVersionUID = 189860012786479470L;
private Canvas canvas;
public Draw(){
this.setTitle("Test Draw");
this.setSize(500, 300);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
canvas = new Canvas();
canvas.setSize(490, 200);
canvas.setBackground(Color.WHITE);
canvas.setVisible(true);
JButton btn1 = new JButton("Draw Line");
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
draw("line");
}});
JButton btn2 = new JButton("Draw Rect");
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
draw("rect");
}});
JButton btn3 = new JButton("Draw Round Rect");
btn3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
draw("rrect");
}});
this.add(btn1);
this.add(btn2);
this.add(btn3);
this.add(canvas);
}
private void draw(String type){
Graphics g = canvas.getGraphics();
g.setColor(Color.ORANGE);
if(type.equals("line")){
g.drawLine(10, 10, 100, 10);
}else if(type.equals("rect")){
g.drawRect(110, 10, 100, 50);
}else if(type.equals("rrect")){
g.fillRoundRect(220, 10, 100, 50, 15, 15);
}
}
public static void main(String[] args) {
Draw draw = new Draw();
draw.setVisible(true);
}
}
import java.awt.Canvas;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Draw extends JFrame {
private static final long serialVersionUID = 189860012786479470L;
private Canvas canvas;
public Draw(){
this.setTitle("Test Draw");
this.setSize(500, 300);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
canvas = new Canvas();
canvas.setSize(490, 200);
canvas.setBackground(Color.WHITE);
canvas.setVisible(true);
JButton btn1 = new JButton("Draw Line");
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
draw("line");
}});
JButton btn2 = new JButton("Draw Rect");
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
draw("rect");
}});
JButton btn3 = new JButton("Draw Round Rect");
btn3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
draw("rrect");
}});
this.add(btn1);
this.add(btn2);
this.add(btn3);
this.add(canvas);
}
private void draw(String type){
Graphics g = canvas.getGraphics();
g.setColor(Color.ORANGE);
if(type.equals("line")){
g.drawLine(10, 10, 100, 10);
}else if(type.equals("rect")){
g.drawRect(110, 10, 100, 50);
}else if(type.equals("rrect")){
g.fillRoundRect(220, 10, 100, 50, 15, 15);
}
}
public static void main(String[] args) {
Draw draw = new Draw();
draw.setVisible(true);
}
}
追问
真的谢谢你 我看看能不能行 方便给我qq吗
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询