ユーザーがデータを追加し、ボタンを押すとウィンドウに図形が描画される単純なGUIを描画しようとしています。私はより大きなプログラムに失敗したので、正方形に戻ります。メニューバーに[描画]ボタンがある単純なウィンドウを作成しようとしています。ボタンを押すと、グラフィッククラスを実装するJPanelから派生したクラスにハードコーディングした図形が同じウィンドウに表示されます。
これが私が持っているものですが、機能していません。誰かが私が間違っていることを見てください。イライラするのは、シェイプを作成してコンストラクターのレイアウトに追加するだけでは正常に機能するのに、ボタンを使用して操作を実行しようとすると失敗することです。
ありがとう。
パブリッククラスShapeGUITestはJFrameを拡張します{
public static final int WINDOW_WIDTH = 500;
public static final int WINDOW_HEIGHT = 500;
public static final int X = 100;
public static final int Y = 75;
public static final int SHAPE_BASE = 200;
public static final int SHAPE_HEIGHT = 250;
JPanel buttonPanel;
JComponent shapeArea;
public ShapeGUITest() {
super("Draw Shapes Dammit!");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
shapeArea = new Jpanel();
add(shapeArea, BorderLayout.CENTER);
JButton drawButton = new JButton("Draw");
drawButton.addActionListener(new DrawButtonListener());
JMenuBar bar = new JMenuBar();
bar.add(drawButton);
setJMenuBar(bar);
}
/*public void drawShapePanel() {
shapeArea = new ShapePanel();
add(shapeArea, BorderLayout.CENTER);
}*/
private class DrawButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//drawShapePanel();
//shapeArea = new ShapePanel();
//add(shapeArea, BorderLayout.CENTER);
ShapePanel s = new ShapePanel();
//s.setVisible(true);
//shapeArea.removeAll();
shapeArea.add(s);
shapeArea.revalidate();
validate();
//ShapeFrame s = new ShapeFrame();
}
}
private class ShapeFrame extends JFrame {
public ShapeFrame() {
super("the shape is here");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
setBackground(Color.WHITE);
g.setColor(Color.YELLOW);
g.fillRect(X, Y, SHAPE_BASE, SHAPE_HEIGHT);
g.setColor(Color.GREEN);
g.drawRect(X, Y, SHAPE_BASE, SHAPE_HEIGHT);
}
}
private class ShapePanel extends JPanel {
public ShapePanel() {
super();
setVisible(true);
setLayout(new FlowLayout());
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
g.setColor(Color.YELLOW);
g.fillRect(X, Y, SHAPE_BASE, SHAPE_HEIGHT);
g.setColor(Color.GREEN);
g.drawRect(X, Y, SHAPE_BASE, SHAPE_HEIGHT);
}
}
public static void main(String[] args) {
ShapeGUITest gui = new ShapeGUITest();
gui.setVisible(true);
}
}