JAVA初心者です。SWTでCanvasの上に長方形を描画しようとしています。
メソッドに引数を渡す前に初期設定をしようとして、
http://yan-note.blogspot.jp/2009/06/awt-2.html
を参考にして、
Graphics g = getGraphics();
としました。
ですが、eclipseでエラーが表示されています。
「メソッド getGraphics() は型 new SelectionAdapter(){} で未定義です」と出ます。
import java.awt.Graphics;
とクラスはインポートしているのですが、何故出来ないのかよく分かりません。
参考までにソースを添付します
import java.awt.Color;
import java.awt.Graphics;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class TestApp extends Shell {
private Text text;
/** * Launch the application. * @param args */ public static void main(String args[]) { try { Display display = Display.getDefault(); TestApp shell = new TestApp(display); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } catch (Exception e) { e.printStackTrace(); } } /** * Create the shell. * @param display */ public TestApp(Display display) { super(display, SWT.SHELL_TRIM); Label lblNewLabel = new Label(this, SWT.NONE); lblNewLabel.setBounds(10, 10, 86, 15); lblNewLabel.setText("グラフです"); Canvas canvas = new Canvas(this, SWT.NONE); canvas.setBounds(10, 31, 342, 182); text = new Text(this, SWT.BORDER); text.setBounds(10, 230, 73, 21); text.setText("0"); Button button = new Button(this, SWT.NONE); button.addSelectionListener(new SelectionAdapter() { /* * @Override(非 Javadoc) * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent) */ public void widgetSelected(SelectionEvent arg0) { /* ここにボタンを押したときの処理を書く * */ int data; Graphics g = getGraphics(); data = Integer.parseInt(text.getText()); if (data < 0 || data > 100){ Shell shell = new Shell(display); MessageBox msg = new MessageBox(shell); msg.setText("エラー"); msg.setMessage("0以上100以下を入れてください"); msg.open(); } else { lblNewLabel.setText(text.getText()); bar(g); } } }); button.setBounds(94, 226, 75, 25); button.setText("作成"); createContents(); } /** * Create contents of the shell. */ protected void createContents() { setText("SWT Application"); setSize(450, 300); } @Override protected void checkSubclass() { // Disable the check that prevents subclassing of SWT components } public void bar(Graphics g){ g.setColor(Color.darkGray); g.fillRect(10, 40, 100, 50); }
}
3日ほど悩んでますが、どうすれば良いのか皆目見当が付かないので分かる方お願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/30 08:55
2017/01/30 09:20 編集
2017/01/30 09:44