したがって、関数setPaintFlag()
のスコープ内に外部のボタンリスナーがあり、呼び出す必要がありますが、Javaではランタイムエラーが発生しています。さらに、私の描画パネルが表示されません。誰も私の間違いを見つけられますか?プログラムの目的は、ランダムな大きさと色でランダムな位置に円または正方形のいずれかを描くことです。プログラムがコンパイルされ、ウィンドウが表示されますが、ボタンだけが表示されますが、起動しません。目に見えるエラー私はsetPaintFlag()
上にカーソルを移動The method setPaintFlag(int) is undefined for the type PaintLab.buttonListener
ボタンリスナーを使用してJava内部クラス関数を呼び出す
ボタンを押した後に、より詳細なエラーメッセージが
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PaintLab extends JPanel {
private JButton circle, square;
private JPanel drawingPanel;
public PaintLab() {
circle = new JButton("Draw Circle");
circle.addActionListener(new ButtonListener());
square = new JButton("Draw Square");
square.addActionListener(new ButtonListener());
JPanel buttonPanel = new JPanel();
JPanel drawingPanel = new DrawingPanel(400, 400);
add(drawingPanel);
BoxLayout bX = new BoxLayout(buttonPanel, BoxLayout.X_AXIS);
buttonPanel.setLayout(bX);
buttonPanel.add(circle);
buttonPanel.add(square);
add(buttonPanel);
}
private class DrawingPanel extends JPanel { // outer
private boolean paintFlag = false;
private int theMode = -1;
private int width, height;
private DrawingPanel(int width, int height) { // inner
this.width = width;
this.height = height;
}
public void setPaintFlag(int current) { // inner
paintFlag = true;
theMode = current;
}
public void paintComponent(Graphics g) { // inner
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int r = (int) (Math.random() * 255) + 1;
int gn = (int) (Math.random() * 255) + 1;
int b = (int) (Math.random() * 255) + 1;
g2.setColor(new Color(r, gn, b));
int w = (int) (Math.random() * width) + 1;
int h = (int) (Math.random() * height) + 1;
int x = (int) (Math.random() * width) + 1;
int y = (int) (Math.random() * height) + 1;
if (theMode == 0) {
g2.drawOval(x, y, w, h);
g2.fillOval(x, y, w, h);
} else if (theMode == 1) {
g2.drawRect(x, y, w, h);
g2.fillRect(x, y, w, h);
}
g2.dispose();
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == circle) {
setPaintFlag(0);
drawingPanel.repaint();
} else if (event.getSource() == square) {
setPaintFlag(1);
drawingPanel.repaint();
}
}
}
}
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: The method setPaintFlag(int) is undefined for the type PaintLab.ButtonListener The method setPaintFlag(int) is undefined for the type PaintLab.ButtonListener
であり、これがメインです。あなたはsetPaintFlag(int current)
フォームButtonListener
を呼ぶが、それはDrawingPanel
に定義されているため
Circle = new JButton("Draw Circle")
Circle.addActionListener(new buttonListener())
エラーメッセージは次のとおりです。彼らは何のアクションリスナーを持っていないので
public class paintLabMain {
public static void main(String[] args) {
JFrame frame=new JFrame("Paint Lab");
frame.setPreferredSize(new Dimension (300,600));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new PaintLab());
frame.pack();
frame.setVisible(true);
}
}
1. [MCVE]を投稿してください。 2.あなたは 'buttonListener'を呼び出しますか? 3.どのようなエラーが出ますか? – c0der
申し訳ありません私は私のメインを追加することを忘れていました。プログラムがコンパイルされ、ウィンドウが表示されますが、ボタンだけが表示されますが、起動しません。 –
アクションリスナーがないため、ボタンは起動しません – c0der