我试图通过“while循环”repaint()方法从JRibbonFrame调用JInternalFrame后,在JInternalFrame中创建一个JPanel刷新它的图形。 无论何时何地我调用JPanel run()方法都会出现问题。以下是我尝试的三种方式:
方法1 :在将internalFrame添加到contentPane之后立即调用run()方法
方法2 :创建另一个按钮[调用功能区中的run()]方法并等到图像(在Jpanel中)加载后再单击“运行线程”按钮< / p>
方法3 :使JRibbonFrame可运行。然后在JRibbonFrame run()方法中“while循环”调用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);
}
}
提前感谢!
答案 0 :(得分:1)
调用run
不会创建新线程,并且运行actionPerformed
的线程也会执行几项重要任务,例如在repaint
之后刷新图形。你应该使用挥杆计时器:
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
,这是一个不错的优点。