1

我正在开发一个允许用户输入消息的程序。当用户按下位于我的框架中的“消息”菜单项时,JOptionPane会弹出一个输入对话框,提示他们输入一个字符串。问题是我现在必须获取该字符串并将其粘贴到我的面板类中。此外,当用户按下同样位于我的框架中的另一个菜单项时,我还允许用户从不同的对话框中选择形状和颜色。字符串必须在我绘制的形状的顶部。我试过绘制字符串,但它不能正常工作。这是我的代码不起作用。当我在JOptionPane. 我该怎么做?

在我的框架里

private void messageItemActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

    String message = JOptionPane.showInputDialog("Enter your string");

    // my panel
    drawP.setMessage(message);

}

在我的面板中

public void setMessage(String s) {
    message = s;
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    // the user can select two different shapes this is
    // a boolean
    if (shape == true) {
        g.setColor(shapeColor);
        g.fillRect(x, y, 40, 40);
        g.drawString(message, x, y);

    } else {
        g.setColor(shapeColor);
        g.fillOval(x, y, 40, 40);
        g.drawString(message, x, y);
    }
}
4

1 回答 1

1

Have you call repaint() after you set the message? I tried before, repaint JFrame doesn't seems to works, but repaint JPanel works.

于 2012-07-19T01:37:12.993 回答