我正在研究一個允許用戶輸入消息的程序。當用戶按下位於我的框架中的「消息」菜單項時,會彈出一個JOptionPane
輸入對話框,提示他們輸入一個字符串。問題是我現在必須獲取該字符串並將其粘貼到我的面板類中。此外,我還允許用戶在另一個菜單項上按下另一個對話框時從另一個菜單項中選擇一個形狀和顏色。該字符串必須位於我繪製的形狀的頂部。我試過 繪製字符串,但它不能正常工作。這是我的代碼不工作。 我只想在我的面板上繪製字符串,當我在JOptionPane
上點擊OK時。我會怎麼做?從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);
}
}
*「從JOPTIONPANE向面板添加字符串」*不需要SHOUT給我們,請使用正確的大寫名稱。 – 2012-07-19 02:26:56
爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-07-19 02:32:55