0

我刚开始学习 Java 并且有一些菜鸟问题。下面几乎是我的程序的完整列表,因为现在我想听听我能听到的所有批评。

我要解决的“问题”是如何在它们JLabel之间共享一个对象,JPanels以便我可以处理类似的事件MousePressed?我设法处理了来自 PlotSurface 的 MousePressed 事件:在我以某种方式移动平面上的光标后,JLabel坐标会刷新。但是代码对我来说看起来很难看。任何建议将不胜感激!

目标是处理来自ControlsPanel(X、Y、Z 和 R 可以更改)的事件。我想PlotSurface在这四个更改后画一个圆圈。

class PlotSurface extends JPanel {
    private ColoredCircle redCircle = new ColoredCircle();
    private Point start = new Point(0,0);
    private JLabel _label;
    public PlotSurface(JLabel label) 
    {
        _label = label;
        setBackground(Color.WHITE);
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                String pos = redCircle.getX() + "," + redCircle.getY();
                _label.setText(pos);
                moveCircle(e.getX(),e.getY());
            }
        });

        addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
               String pos = redCircle.getX() + "," + redCircle.getY();
                _label.setText(pos);
                moveCircle(e.getX(),e.getY());
            }
        });

    }

    private void moveCircle(int x, int y) {

        final int CURR_X = redCircle.getX();
        final int CURR_Y = redCircle.getY();
        final int CURR_W = redCircle.getWidth();
        final int CURR_H = redCircle.getHeight();
        final int OFFSET = 1;

        if ((CURR_X!=x) || (CURR_Y!=y)) {

        // The circle is moving, repaint background over the old square location. 
        repaint(CURR_X,CURR_Y,CURR_W+OFFSET,CURR_H+OFFSET);

        // Update coordinates.
        redCircle.setX(x);
        redCircle.setY(y);

        // Repaint the circle at the new location.
        repaint(redCircle.getX(), redCircle.getY(), 
        redCircle.getWidth()+OFFSET, 
        redCircle.getHeight()+OFFSET);
        }

    }

 @Override 
  public Dimension getPreferredSize() {
       return new Dimension(150,150);
   } 

 @Override 
  public void paintComponent(Graphics g) {
       super.paintComponent(g);       
       String pos = redCircle.getX() + "," + redCircle.getY();
       Graphics2D g2 = (Graphics2D) g;
       g2.drawString(pos,10,10); 
       g2.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);
       g2.drawLine(getWidth()/2,0,getWidth()/2,getHeight());
       g2.fillOval(100,100,50,50);
       redCircle.paintCircle(g2);
  }     

}

class ColoredCircle{ //
 it know how to draw itself   }

class ControlsHeader extends JPanel 
{
    public ControlsHeader() {
        super();          
        JLabel lblR = new JLabel();
        lblR.setText("R: ");
        //...
        JSpinner x = new JSpinner(model);
        //...
        JComboBox y = new JComboBox();
        //..
        ButtonGroup z = new ButtonGroup();

         }
  }

class ControlsFooter extends JPanel 
{
    public ControlsFooter()
    {
        super();
        //a couple of elements
    }
}


class ControlsFrame extends JFrame 
{
    public ControlsFrame(String title) 
    {
        super(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(1000, 1000));

        JLabel coords = new JLabel();
        coords.setText("COORDS");
        coords.setForeground(Color.WHITE);
        JPanel footer = new ControlsFooter();
        footer.add(coords); 

        add(new PlotSurface(coords), BorderLayout.CENTER);        
        add(new ControlsHeader(), BorderLayout.NORTH);
        add(footer, BorderLayout.SOUTH);
    }
}
public class Lab4UI {

    private static void startGUI() {  
        JFrame frame = new ControlsFrame("Lab 4. №36");
        frame.pack();
        frame.setVisible(true);
    }

   public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {  
               startGUI(); 
        }});
}

}
4

1 回答 1

1

标签是个体,可能有其他文本、图标、位置、大小。你可以让你的标签类扩展 JLabel。但最好似乎保留一个单独的类,可以为 JLabel 添加/提供 MouseAdapter。

共享 JLabel 是不可行的,因为已实现的组件具有固定的父容器。

我无法判断整体上下文,但对于我自己,我会首先设计一个抽象系统:绘图表面,由鼠标控制,也许是一些常见的管理类共享逻辑。

也许让两个尽可能独立的 JPanel 组件,然后看看可以共享什么。


无需在外部提供坐标,因为无法共享 JLabel。

public class PlotSurface extends JPanel {
    private ColoredCircle redCircle = new ColoredCircle();
    private Point start = new Point(0,0);
    private JLabel coords = new JLabel();
    private final MouseAdapter mouseAdapter = new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            String pos = redCircle.getX() + "," + redCircle.getY();
            coords.setText(pos);
            moveCircle(e.getX(),e.getY()); // Or one param: e.getPoint().
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            String pos = redCircle.getX() + "," + redCircle.getY();
            coords.setText(pos);
            moveCircle(e.getX(),e.getY());
        }
    };

    public PlotSurface() 
    {
        coords.setText("COORDS");
        coords.setForeground(Color.WHITE);
        setBackground(Color.WHITE);
        addMouseListener(mouseAdapter);
        addMouseMotionListener(mouseAdapter);
    }
于 2013-11-10T21:11:56.333 回答