小应用程序启动后,我需要绘制公式,所以我尝试做下一个:
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准备绘制公式?