java - 我的代码在启动时不执行
问题描述
我需要在一个小程序启动后绘制公式,所以我尝试做下一个:
public class SearchIdeaApplet extends JApplet {
private JPanel mRootPanel;
private JButton mPrevButton;
private JButton mNextButton;
private JButton mUpdateButton;
private JPanel mPlotPanel;
private JPanel mFormulaPanel;
private void drawFormulas() {
TeXFormula formula = new TeXFormula("x^2 + 3*y^2");
// render the formula to an icon of the same size as the formula.
TeXIcon icon = formula
.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);
icon.setInsets(new Insets(5, 5, 5, 5));
// now create an actual image of the rendered equation
BufferedImage image = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
JLabel jl = new JLabel();
jl.setForeground(new Color(0, 0, 0));
icon.paintIcon(jl, g2, 0, 0);
// now draw it to the screen
Graphics g = mFormulaPanel.getGraphics();
g.drawImage(image,0,0,null);
}
@Override
public void init() {
try {
SwingUtilities.invokeAndWait(this::initSwing);
} catch (InterruptedException | InvocationTargetException e) {
e.printStackTrace();
}
}
private void onUpdateButtonClick(ActionEvent event) {
drawFormulas();
}
private void initSwing() {
setContentPane(mRootPanel);
drawFormulas(); //It doesn't work
mUpdateButton.addActionListener(this::onUpdateButtonClick);
}
}
但是drawFormulas()
调用initSwing()
没有任何作用。但是如果我按下mUpdateButton
我的公式就会显示出来。
如何等待 GUI 准备好绘制公式?
解决方案
推荐阅读
- android - 如何链接到原生 Android APP 的供应商库
- sass - Nativescript Angular DEPRECATION WARNING:不推荐使用 / 进行除法
- java - 使用 JPA CriteriaBuilder API 实现 NULLS LAST
- amazon-web-services - 无法检索使用 jenkins 构建创建的 Fargate 容器的公共 IP 地址
- settimeout - 如何使用 setTimeout for *ngIf in angular 10 for the ionic spinner
- c - 计算文件中的字符、单词和行
- excel - 如何在 Excel VBA 中测试构建的文件路径?
- google-cloud-firestore - Complex firestore rules
- php - 我正在尝试使用带有 htaccess 的参数来实现目录 URL
- php - 为什么不使用 php symfony 框架方法(控制器)?