简体   繁体   中英

ActionListener doesn't work properly after drawing line in java

I have a problem with an action listener after I draw a line, basicly it works just one time, for example, my application is downloading an image, than u pick two points, first with left button mouse, second with right button, than u click "connect points" button, and it is drawing a line. And this works, I can do it with many lines etc. BUT when I close an window with image and reload it the "connect points" button stops working. Dk what to do with it. Here is the code:

the painting line part:

public void paint(Graphics g) {

 super.paint(g);
    myPaint(g);  
 }

 private void myPaint(Graphics g) {
    g.drawLine(lx1, ly1, px2, py2);
 }
}

ActionListener part:

public void actionPerformed(ActionEvent e) {

    if(e.getSource()==painterka){

       Graphics g = imadzysko.getGraphics();
          paint(g);
            lx1=0;
            ly1=0;
            px2=0;
            py2=0;    
       }
 }

Panel with graphic part:

void diagramKY (JFrame windower, String tyt, String content) {
    Listener listener = new Listener(); 

    panelik.setLayout(null);
    painterka =  new JButton("Connect Points");
    windower = new JFrame("");
    windower.setTitle(tyt+" - diagram");
    windower.setSize(800, 600);
    windower.setVisible(true);
    windower.setLocationRelativeTo(null);
    URLdownloader.fileUrl("http://stooq.pl/c/?s="+content+"&c=1d&t=l&a=lg",
             content+".png","");
    imadzysko = new ImagePanel(new ImageIcon(content+".png").getImage());
    panelik.add(imadzysko);
    panelik.add(painterka);
    imadzysko.addMouseListener(new MyMouseListener());
    painterka.addActionListener(listener);
    Insets insets = panelik.getInsets();
    Dimension size = imadzysko.getPreferredSize();
    imadzysko.setBounds(20 + insets.left, 20 + insets.top,
        size.width, size.height);
    size = painterka.getPreferredSize();
    painterka.setBounds(630 + insets.left, 20 + insets.top,
        size.width, size.height);
    panelik.repaint();
    imadzysko.repaint();
    windower.add(panelik);

well, any suggestions? :)

1) create panel, put that to the GUI and last code lines would be

windower.setLocationRelativeTo(null);
windower.setVisible(true);

otherwiese your panel never will be visible on the screen

2) don't use setBounds() etc.., for that is there exists LayoutManagers

windower.add(panel);

then your panel fill whole JFrame area

3) never use paint(Graphics g) in the Swing Code, use only paintComponent(Graphics g) to avoids un-expected output to the GUI

4) don't create new Top-level Containers on Runtime, for popup window create only one JDialog or JWindow and re-use that for another Action

5) you have problems with Concurency in Swing , your GUI freeze, because waiting for hard and longtime code, implements SwingWorker , there is similair example about that

6) if you want to display some pictures or images look for Icon placed in the JLabel

7) really required to read 2D Graphics tutorial before posting question here

Graphics g = imadzysko.getGraphics();

Never do that. A Java GUI should paint when told to do so. When that time comes, the paint(Graphics) or paintComponent(Graphics) will be called. Do the painting then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM