1

次のコードがあります(スイングとJavaを学ぼうとしています)。クラスを使用して長方形のコンポーネントを使用してはしごを作成し、メイン フレームに配置しました。すべて問題なく動作しますが、サイズを少しでも変更すると、ShapeManager オブジェクト (つまり、はしご) が消えます。何が起こっているのかわかりません。助けてください。

GUIMain クラス:

package mainProg;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.*;


public class GUIMain {

    static JPanel mainPanel;
    static JButton[] newButtons;
    static ShapeManager newShape;

    private static class BtnEvtHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            //System.exit(0);
            JOptionPane.showMessageDialog( null, "WELCOME" );
        }

    }

    private static JButton[] createButtons() {
        JButton[] buttonArray= new JButton[2];
        buttonArray[0]=new JButton("OK");
        buttonArray[1]=new JButton("MOVE");
        BtnEvtHandler okButtonHandler= new BtnEvtHandler();
        ( buttonArray[0]).addActionListener(okButtonHandler);

        return buttonArray;

    }


    private static ShapeManager createShape(int x) {
        ShapeManager newContent=new ShapeManager(x);
        return newContent;      
    }

    private static JPanel mainContainer() {
        JPanel mainPanel= new JPanel();
        mainPanel.setSize(400, 400);
        return mainPanel;

    }


    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(false);
        JFrame frame = new JFrame(" DB ");

        mainPanel= mainContainer();
        mainPanel.setLayout(new BorderLayout(10, 10));
        newButtons= createButtons();
        newShape= createShape(20);

        newButtons[0].setHorizontalAlignment(0);
        mainPanel.add(newButtons[0],BorderLayout.PAGE_START); 
        newButtons[1].setHorizontalAlignment(0);
        mainPanel.add(newButtons[1],BorderLayout.PAGE_END);
        newShape.setPreferredSize(new Dimension(400, 400));
        mainPanel.add(newShape, BorderLayout.LINE_END);




        frame.setContentPane(mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setLocation(500,200);
        frame.setVisible(true);
    }


    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

ShapeManager クラス:

package mainProg;

import javax.swing.JPanel;

import java.awt.*;


@SuppressWarnings("serial")
class ShapeManager extends JPanel {

    int rectPos;

    ShapeManager(int rectPos) {

        setPreferredSize(new Dimension(400,400));
        this.rectPos=rectPos;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        while (rectPos<150) {
            g.setColor(Color.BLUE);
            g.drawRect(rectPos+10, rectPos+10, 100, 10);
            g.fillRect(rectPos+10, rectPos+10, 100, 10);
            rectPos=rectPos+10;

        }
    }
}
4

1 に答える 1

3

長方形の位置をリセットすることはないため、最初の描画後は 150 を超えたままになります。while ループを終了した後、リセットする必要があります。

これを試して:

g.setColor(Color.BLUE);
int position = rectPos;
while (position<150) {
   position += 10;
   g.drawRect(position, position, 100, 10);
   g.fillRect(position, position, 100, 10);
}
于 2013-09-20T15:18:25.453 に答える