简体   繁体   English

在java中绘制线后,ActionListener无法正常工作

[英]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. Dk该怎么做。 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: ActionListener部分:

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 1)创建面板,将其放到GUI,最后的代码行

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 2)不要使用setBounds()等..,因为那里存在LayoutManagers

windower.add(panel);

then your panel fill whole JFrame area 然后你的面板填满整个JFrame区域

3) never use paint(Graphics g) in the Swing Code, use only paintComponent(Graphics g) to avoids un-expected output to the GUI 3)永远不要在Swing Code中使用paint(Graphics g) ,只使用paintComponent(Graphics g)来避免对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 4)不要在运行时创建新的顶级容器 ,因为弹出窗口只创建一个JDialog或JWindow并将其重新用于另一个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 5)你在Swing中的Concurency ,你的GUI冻结有问题,因为等待硬和长时间的代码,实现SwingWorker ,有类似的例子

6) if you want to display some pictures or images look for Icon placed in the JLabel 6)如果要显示一些图片或图像,请查找放置在JLabel中的 图标

7) really required to read 2D Graphics tutorial before posting question here 7)在发布问题之前,确实需要阅读2D图形教程

Graphics g = imadzysko.getGraphics();

Never do that. 永远不要那样做。 A Java GUI should paint when told to do so. Java GUI应该在被告知时进行绘制。 When that time comes, the paint(Graphics) or paintComponent(Graphics) will be called. 到那时,将调用paint(Graphics)paintComponent(Graphics) Do the painting then. 然后做画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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