1,我想在swing的jpanel上面画矩形.但是具体画多少,需要看传递的参数,请问如何去画;
2,当我的鼠标进入到相应的矩形范围的时候,就能够点击,点击之后 再弹出 joption.

解决方案 »

  1.   

    可以直接使用JTable。
    参数大概是个2维数组吧,按照数组的长度生成对应的table,并且把数据设置进去。
    在tableCell里增加clicked事件,触发JOption.show*Dialog()
      

  2.   

    简单写了一个package mobi.chenwei.sample.shapesample;import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.Shape;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;/**
     * @author Chen Wei
     * @website www.chenwei.mobi
     */
    public class DrawPanel extends javax.swing.JPanel {
        
        private Shape shape = null;
        
        /** Creates new form DrawPanel */
        public DrawPanel() {
            initComponents();
        }
        
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {        addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    formMouseClicked(evt);
                }
            });
            setLayout(null);
        }// </editor-fold>    private void formMouseClicked(java.awt.event.MouseEvent evt) {                                  
            if(getShape().contains(evt.getPoint())){
                JOptionPane.showMessageDialog(this, ""); 
            }
        }                                     public Shape getShape() {
            return shape;
        }    public void setShape(Shape shape) {
            this.shape = shape;
        }
        
        
        // Variables declaration - do not modify
        // End of variables declaration
        
        public void paintComponent(Graphics g){
            if(getShape() != null){
                Graphics2D g2d = (Graphics2D)g;
                g2d.draw(getShape());
            }
        }
        
        public static void main(String[] args){
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            DrawPanel drawPanel = new DrawPanel();
            drawPanel.setShape(new Rectangle(20, 20, 100, 100));
            frame.add(drawPanel, BorderLayout.CENTER);
            
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
      

  3.   

    to chenweionline:
     如何去动态的定义矩形啊
    如这样 我把这个作为一个class,
    然后调用它, 我传递一个参数k,
    然后画k个矩形,是平行的.问题:1,如何去画平行k个的矩形,
      

  4.   

    用底层的graphics2D来画,提供最大的灵活性