gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:18:45 25 4
gpt4 key购买 nike

在通过“循环”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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com