private class showAction implements ActionListener
{
}
里面

解决方案 »

  1.   

    private class showAction implements ActionListener
    {
    }
    里面怎么写啊?
    是用paintComponent吗?
    哪位行行好,给个代码啊?我只知道下面的方法........
      public void paintComponent(Graphics g)
      {
        super.paintComponent(g);
         Graphics2D g2=(Graphics2D)g;
         double leftx=100;
         double topy=100;
         double width=200;
         double height=150;
          Rectangle2D rect=new Rectangle2D.Double(leftx,topy,width,height);
          g2.draw(rect);
         
      }
      

  2.   

    在MainPanel类中重载paintComponent(Graphics g)方法,在其内部写画距形的代码。
    监听器内部:
    {
         pack();//在这里调用窗体的重画代码
    }
      

  3.   

    to superberd(小熊维尼): 
    我在MainPanel类中重载paintComponent(Graphics g)方法,那么不用我点击按钮,frame上直接已经画出矩形了........我还是不明白阿
      

  4.   

    在MainPanel中声明两个静态变量初始化为0 ,点击batton 后再设为矩形的高和宽的尺寸
      

  5.   

    根据superberd(小熊维尼)给的思路
    兄弟现在是这样写的:
    public MainPanel()
      {
        setLayout(new BorderLayout());
        JButton showButton =new JButton("Show");
        add(showButton,BorderLayout.SOUTH);
        showAction creatTable =new showAction();
        showButton.addActionListener(creatTable);
      }
      double leftx = 0;
      double topy = 0;
      double width = 0;
      double height = 0;
      public void paintComponent(Graphics g)
      {
         super.paintComponent(g);
           Graphics2D g2=(Graphics2D)g;
          Rectangle2D rect=new Rectangle2D.Double(leftx,topy,width,height);
          g2.draw(rect);
      }
      private class  showAction implements ActionListener
        {
        public void actionPerformed(ActionEvent event)
          {
            leftx = 10;
            topy = 10;
            width = 30;
            height =100;
            repaint();
          }
        }
    }
    总觉得写的有些别扭,哪位能给改改?立刻结贴!
      

  6.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
    public class CreatLay {
      public static void main(String[] args)
      {
        MainFrame1 frame = new MainFrame1();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.show();
      }
    }
    class MainFrame1 extends JFrame
    {
      public  MainFrame1()
      {
        setTitle("bbb");
        setSize(500,500);
        MainPanel panel=new MainPanel();
        Container contentPane=getContentPane();
        contentPane.add(panel);
      }
    }
    class MainPanel extends JPanel
    {
      public static int h=0,g=0;
      public MainPanel()
      {
        setLayout(new BorderLayout());
        JButton showButton =new JButton("Show");
        add(showButton,BorderLayout.SOUTH);
        showAction creatTable =new showAction();
        showButton.addActionListener(creatTable);
      }
      private class  showAction implements ActionListener
        {
             h=150,g=200;
             pack();
        }
      public void paintComponent(Graphics g)
      {
        super.paintComponent(g);
         Graphics2D g2=(Graphics2D)g;
         double leftx=100;
         double topy=100;
         //double width=200;
         //double height=150;
          Rectangle2D rect=new Rectangle2D.Double(leftx,topy,g,h);
          g2.draw(rect);
         
      }}