在我通过“同时循环”repaint() 方法从 JRibbonFrame 调用 JInternalFrame 之后,我试图让 JInternalFrame 中的 JPanel 刷新它的图形。无论何时何地我调用 JPanel run() 方法都会出现问题。以下是我尝试的三种方法:
方法 1:在我将 internalFrame 添加到 contentPane 之后立即调用 run() 方法
方法 2:在功能区中创建另一个按钮 [调用 run()] 方法并等待图像(在 Jpanel 中)被加载,然后再单击“运行线程”按钮
方法 3:使 JRibbonFrame 可运行。然后在 JRibbonFrame run() 方法中“循环”调用 JPanel 的 run() 方法,最后在 main(String args[]) 方法中调用 JRibbonFrame run()。
上述方法都不起作用(应用程序只是冻结!!!)
我求你帮忙!
文档类.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");
}
}
主.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);
}
}
提前致谢!