微微微love笑 2017-05-19 01:44 采纳率: 0%
浏览 1451

Java GUI 一个按钮画直线,一个按钮画椭圆,怎么不行啊??

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class Board extends JFrame implements ActionListener
{
int x1,x2,y1,y2;
Container cp = getContentPane();

JPanel pa = new JPanel();
JButton but1 = new JButton("直线");
JButton but2 = new JButton("空心椭圆");

public Board()
{
    super("画板");
    setBounds(400, 20, 500, 500);
    cp.setLayout(new FlowLayout()); 
    pa.add(but1);
    pa.add(but2);
    cp.add(pa);

    but1.addActionListener(this);
//  but1.setActionCommand("直线");
    but2.addActionListener(this);
//  but2.setActionCommand("空心椭圆");

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == but1)
    {
        System.out.println("fj");

        cp.addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                x1=e.getX();
                y1=e.getY();
            }
            public void mouseReleased(MouseEvent e)
            {
                x2=e.getX();
                y2=e.getY();
                Graphics g = getGraphics();
                g.drawLine(x1, y1, x2, y2);
                //g.drawRect(x1, y1, Math.abs(x2-x1),Math.abs(y2-y1));
            }

        });

    } else
    {
        System.out.println("jfjf");
        cp.addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                x1=e.getX();
                y1=e.getY();
            }
            public void mouseReleased(MouseEvent e)
            {
                x2=e.getX();
                y2=e.getY();
                Graphics g = getGraphics();
                //g.drawLine(x1, y1, x2, y2);
                g.drawRect(x1, y1, Math.abs(x2-x1),Math.abs(y2-y1));
            }

        });
    //  g.drawRect(x1, y1, Math.abs(x2-x1),Math.abs(y2-y1));
    }
}

}

public class Text
{
public static void main(String[] args)
{
new Board();
}
}

  • 写回答

3条回答 默认 最新

  • 微微微love笑 2017-05-19 05:52
    关注

    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘