Java Ribbon - Flamingo 不会 run() 可运行的 JPanel

标签 java multithreading ribbon runnable radiance-flamingo

在通过“循环”repaint() 方法从 JRibonFrame 调用 JInternalFrame 后,我试图使 JInternalFrame 中的 JPanel 刷新其图形。 无论何时何地我调用 JPanel run() 方法时都会出现该问题。以下是我尝试过的三种方法:

方法1:在将internalFrame添加到contentPane后立即调用run()方法

方法 2:创建另一个按钮[调用功能区中的 run()] 方法,并等待图像(在 Jpanel 中)加载,然后再单击“运行线程”按钮

方法 3:使 JRibonFrame 可运行。然后在JRibbonFrame run()方法中“循环”调用JPanel的run()方法,最后在main(String args[])方法中调用JRibbonFrame run()。

上述方法均无效(应用程序只是卡住!!!)

求求你帮忙!

DocumentClass.java

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.JPanel;

public class DocumentClass extends JPanel implements Runnable {


private BufferedImage image;
    public int imgx = 20, imgy= 20;

    public DocumentClass(File newImage){

        try {                
           image = ImageIO.read(newImage);
        } catch (IOException ex) {
             System.out.print("image not found");
        }
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, imgx, imgy, this);
    }

    public void run() {
        while(true){
            try {
                Thread.sleep(75);
            } catch (InterruptedException ex) {}
            repaint();
        }
    }


}

WindowClass.java

import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import org.jvnet.flamingo.common.*;
import org.jvnet.flamingo.ribbon.*;
import test.svg.transcoded.*;

public class WindowClass extends  JRibbonFrame {
    private final JDesktopPane desktopPane;

    public WindowClass(){
            super("Whatever");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);

            desktopPane = new JDesktopPane();
            add(desktopPane);
    }


    protected void configureApplicationMenu() {
        RibbonApplicationMenuEntryPrimary amEntryNew = new RibbonApplicationMenuEntryPrimary(
        new document_new(), "New", new ActionListener() {
        @Override
            public void actionPerformed(ActionEvent e) {

                JFileChooser fc = new JFileChooser();
                int returnVal = fc.showOpenDialog(null);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File newImage = fc.getSelectedFile();

                    JInternalFrame internalFrame = new JInternalFrame(
                    "New Project", true, true, true, true);


                    DocumentClass document  = new DocumentClass(newImage);
                    internalFrame.setContentPane(document);


                    // Add the internal frame to the desktopPane
                    desktopPane.add(internalFrame);
                    // Set the window size of the internal frame
                    internalFrame.setSize(400, 400);

                    // Display it.
                    internalFrame.setVisible(true);

                }

            ((DocumentClass)(desktopPane.getAllFrames()[0]).getContentPane()).run(); //this is Method 1. comment this and it will work but not refresh graphics
            }
        }, JCommandButton.CommandButtonKind.ACTION_ONLY);
        amEntryNew.setActionKeyTip("N");


        RibbonApplicationMenu applicationMenu = new RibbonApplicationMenu();
        applicationMenu.addMenuEntry(amEntryNew);

        this.getRibbon().setApplicationMenu(applicationMenu);

        RichTooltip appMenuRichTooltip = new RichTooltip();
        this.getRibbon().setApplicationMenuRichTooltip(appMenuRichTooltip);
        this.getRibbon().setApplicationMenuKeyTip("F");
    }

}

Main.java

import java.awt.*;
import javax.swing.*;

public class Main {
    public static void main(String[] args) {

            final WindowClass mainWindow = new WindowClass();
            mainWindow.configureApplicationMenu();
            mainWindow.setSize(500, 500);
            mainWindow.setVisible(true);
            mainWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}

提前致谢!

最佳答案

调用run不创建新线程,并且正在运行的线程actionPerformed还执行一些重要任务,例如在 repaint 之后刷新图形。您应该使用 Swing 计时器:

private Timer timer;

public DocumentClass(File newImage) {
    ...
    timer = new Timer(75, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            repaint();
        }
    });
}
public void start() {
    timer.start();
}

那就用我的方法4:调用start方法而不是 run方法。您不需要实现Runnable不再这样了,作为一个很好的加分。

关于Java Ribbon - Flamingo 不会 run() 可运行的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17846194/

相关文章:

excel - Xlam Addin如何创建更新进程

html - 使用CSS制作功能区中心

Java 执行器服务 : should I put a lock before to use execute?

java - 有什么方法可以将已经运行的浏览器附加到 java 中的 selenium webdriver?

java - 我正在尝试使用Hadoop MapReduce计算文本文件中 “pairs of word”的出现次数

java - 整数除法 : Why is the result of 1/3 == 0?

c - 使用 phtreads 从 C 中的多个线程读取和写入不同位置的数组是否安全?

java - 创建一个自中断的 ExecutorService

vba - 用于自定义 Office 2010 功能区的 XML 架构

java - 无法将消息写入控制台中的 Excel 文件,显示 java.lang.NullPointerException