/** 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
参数大概是个2维数组吧,按照数组的长度生成对应的table,并且把数据设置进去。
在tableCell里增加clicked事件,触发JOption.show*Dialog()
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);
}
}
如何去动态的定义矩形啊
如这样 我把这个作为一个class,
然后调用它, 我传递一个参数k,
然后画k个矩形,是平行的.问题:1,如何去画平行k个的矩形,