1

我试图在JFrame窗口中用鼠标画线,当我尝试这样做时,它不起作用!请忽略菜单,我稍后会尝试使用它!我错过了什么!如果您能给我一些提示,我将不胜感激!

public class newGUI extends JFrame implements ActionListener, MouseMotionListener, MouseListener {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
Point point1;
Point point2;
Line2D line2d;

public static void main(String[] args){
    newGUI gui = new newGUI();
    gui.setVisible(true);
}

public newGUI()
{
    super("Menu Demonstration");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenu colorMenu = new JMenu("Choose Colors");
    JMenuItem greenChoice = new JMenuItem("GREEN");
    greenChoice.addActionListener(this);
    colorMenu.add(greenChoice);
    JMenuItem redChoice = new JMenuItem("RED");
    colorMenu.add(redChoice);
    JMenuBar bar = new JMenuBar();
    bar.add(colorMenu);
    setJMenuBar(bar);
    addMouseListener(this);
    addMouseMotionListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
    point1=arg0.getPoint();
}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseDragged(MouseEvent arg0) {
    // TODO Auto-generated method stub
    point2=arg0.getPoint();
    line2d=new Line2D.Double(point1, point2);
    repaint();

}

@Override
public void mouseMoved(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
public void paintComponent(Graphics g){
    Graphics2D g2d = (Graphics2D) g;
    if(point1!=null && point2!=null){
        g2d.setPaint(Color.RED);
        g2d.setStroke(new BasicStroke(1.5f));
        g2d.draw(line2d);
        repaint();
    }
}
}
4

1 回答 1

5
  • 始终以大写字母开头类名,即NewGui
  • 用于Event Dispatch Thread创建和更改 UI 组件
  • 不要不必要地扩展JFrame课程
  • 不要调用,setSize(..)而是调用实例JFrame#pack()JFrame
  • JFrame没有paintComponent(..),而是添加自定义JPanel并覆盖其paintComponent(..)andgetPreferredSize(..)
  • 不要忘记调用super.paintComponent(..)您的自定义JPanel
  • 不要repaint()从内部调用paintComponent(..)(这将导致重新绘制的无限循环超出需要)
  • 同样在可能的情况下(如果其他类不使用它)使用匿名MouseListeners/ MouseMotionListeners 而不是使用MouseAdapter/ MouseListenersMouseMotionListener等。

这是我制作的一个示例(基本上是您提到的带有修复的代码):

在此处输入图像描述

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class NewGui implements ActionListener {

    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                NewGui gui = new NewGui();
            }
        });
    }

    public NewGui() {
        initComponents();
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
    }

    private void initComponents() {
        JFrame frame = new JFrame("Menu Demonstration");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenu colorMenu = new JMenu("Choose Colors");
        JMenuItem greenChoice = new JMenuItem("GREEN");
        greenChoice.addActionListener(this);
        colorMenu.add(greenChoice);
        JMenuItem redChoice = new JMenuItem("RED");
        colorMenu.add(redChoice);
        JMenuBar bar = new JMenuBar();
        bar.add(colorMenu);
        frame.setJMenuBar(bar);

        frame.add(new MyPanel());

        frame.pack();
        frame.setVisible(true);
    }
}

class MyPanel extends JPanel {

    Point point1;
    Point point2;
    Line2D line2d;

    public MyPanel() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                super.mousePressed(me);
                point1 = me.getPoint();
            }
        });
        addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent me) {
                super.mouseDragged(me);
                // TODO Auto-generated method stub
                point2 = me.getPoint();
                line2d = new Line2D.Double(point1, point2);
                repaint();
            }
        });
    }

    //so our panel will be visible
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 200);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        //Set  anti-alias!
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON); 

        if (point1 != null && point2 != null) {
            g2d.setPaint(Color.RED);
            g2d.setStroke(new BasicStroke(1.5f));
            g2d.draw(line2d);
        }
    }
}
于 2012-11-02T18:33:45.820 回答