簡體   English   中英

每次單擊JButton都會打開新的JFrame

[英]New JFrame opens every time a JButton is clicked

因此,我試圖創建國際象棋並開始構建將用作董事會的東西。 在此代碼中,我能夠“切換”兩個按鈕的位置,但是,每次單擊任何一個按鈕(出於測試目的,調用Rook類)時,都會導致為未知的打開新的JFrame原因。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ChessTesting1 extends JFrame implements ActionListener
{
    JButton tiles[] [] = new JButton [8] [8];
    public int x, y, turn;
    String buttonSwitch, buttonText = "";
    JFrame frame;

    public static void main (String args[])
    {
        new ChessTesting1 ();
    }


    public ChessTesting1 ()
    {
        frame = new JFrame ();
        frame.setSize (800, 800);
        frame.setLocation (150, 50);
        frame.setResizable (false);
        frame.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
        JPanel contentPane = (JPanel) frame.getContentPane ();
        contentPane.setLayout (new GridLayout (8, 8));

        for (int x = 0 ; x < 8 ; x++)
        {
            for (int y = 0 ; y < 8 ; y++)
            {
                tiles [x] [y] = new JButton ("" + (y + 1));
            }
        }

        for (int x = 0 ; x < 8 ; x++)
        {
            for (int y = 0 ; y < 8 ; y++)
            {
                contentPane.add ((tiles [x] [y]));
                tiles [i] [j].addActionListener (this);
            }
        }
        frame.show ();
    }




    public void actionPerformed (ActionEvent e)
    {
        Object command = e.getSource ();
        for (int i = 0 ; i < 8 ; i++)
        {
            for (int j = 0 ; j < 8 ; j++)
            {
                if (command == tiles [i] [j])
                {
                    if (turn == 0)
                    {
                        buttonText = tiles [i] [j].getText ();
                        x = i;
                        y = j;
                        new Bishop (x, y);
                        frame.dispose ();

                        buttonSwitch = buttonText;
                        turn++;
                    }
                    else
                    {
                        tiles [x] [y].setText (tiles [i] [j].getText ());
                        tiles [i] [j].getText ();
                        tiles [i] [j].setText ("" + buttonSwitch);
                        turn = 0;
                    }
                buttonText = tiles [x] [y].getText ();
                }
            }
        }
    }
}

主教班:

public class Bishop extends ChessTesting1
{
    public Bishop (int x, int y)
    {
        for (int i = 0 ; i < 8 ; i++)
        {
            for (int j = 0 ; j < 8 ; j++)
            {
                this.tiles [i] [j].setEnabled (false);
            }
        }
        for (int i = 0 ; i < 8 ; i++)
        {
            this.tiles [x + 1] [y + 1].setEnabled (true);
            if ((x + 1) >= 8 || (y + 1) >= 8)
            {
                break;
            }
        }
        for (int i = 0 ; i < 8 ; i++)
        {
            this.tiles [x + 1] [y - 1].setEnabled (true);
            if ((x + 1) >= 8 || (y + 1) >= 8)
            {
                break;
            }
        }
        for (int i = 0 ; i < 8 ; i++)
        {
            this.tiles [x - 1] [y + 1].setEnabled (true);
            if ((x + 1) >= 8 || (y + 1) >= 8)
            {
                break;
            }
        }
        for (int i = 0 ; i < 8 ; i++)
        {
            this.tiles [x - 1] [y - 1].setEnabled (true);
            if ((x + 1) >= 8 || (y + 1) >= 8)
            {
                break;
            }
        }
    }
}

我知道當前的代碼執行不了什么,但是我想知道如何才能阻止每次單擊都打開新的JFrame。

任何幫助,將不勝感激! 謝謝!

實例化new Bishop (x, y);類的東西時,您正在擴展ChessTesting1 new Bishop (x, y); 您還調用了超級構造函數,因此每次都創建一個新的JFrame 從OOP來看,象棋件擴展了ChessTesting1是沒有意義的,因此,您應該仔細地重新考慮您的設計。 我建議閱讀有關模型視圖控制器(MVC)的設計模式。

考慮以下:

import java.awt.EventQueue;
import javax.swing.JFrame;

public class SuperClass {
    public SuperClass() {
        JFrame frame = new JFrame();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new SuperClass();
                    new ChildClass();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

public class ChildClass extends SuperClass {
    public ChildClass() {
        // implicit super() call
    }
}

將出現兩個JFrame元素,因為ChildClass通過ChildClass()的構造函數中的隱式super()調用擴展了SuperClass的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM