簡體   English   中英

Java-將JButton數組添加到JFrame

[英]Java - adding JButton array to JFrame

我試圖將2D JButton數組添加到JFrame,但沒有出現任何錯誤,只是JButton沒有顯示。

創建JButton:

public class TTTGrid {
private static JFrame frame;
private static int[][] coords;
private static int width, height;
public TTTGrid(JFrame frame,int[][] coords, int width, int height){
    this.frame = frame;
    this.coords = coords;
    this.width = width;
    this.height = height;
}
static JButton map[][] = new JButton[3][3];

public void Draw(){
    for(int i = 0; i<coords.length; i++){
        for(int j = 0; j<coords[i].length; j++){
            map[i][j] = new JButton();
            map[i][j].setBounds(i*100, j*100, width, height);
            frame.add(map[i][j]);

        }
    }
}

}

調用draw方法的位置:

public class TTTthread extends TTT implements Runnable {
int[][] map = new int[3][3];
TTTGrid grid = new TTTGrid(frame, map, 100, 100);

@Override
public void run() {
    try {
        while (true) {
            grid.Draw();

            Thread.sleep(20);

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

如果您的代碼像我認為的那樣在運行,則您似乎試圖每秒將9個JButton添加到您的GUI中50次! 那是很多按鈕的麻煩-您確定這是您要執行的操作嗎? 通過從Swing事件線程中進行Swing調用( 很多 Swing調用!),您的代碼還與Swing線程規則相違背。

您的主要解決方案可能

  • 將您的9個JButton添加到使用GridLayout(3, 3)的JPanel中
  • 僅執行一次而不是每秒執行50次
  • 然后將該JPanel添加到您的GUI BorderLayout.CENTER中,並確保不要使用null布局。
  • 不嘗試設置這些JButton的邊界,大小或位置,而是讓布局管理器開始工作
  • 擺脫while循環,而使用Swing的事件驅動模型將代碼更改為更多的事件驅動。
  • 努力使用大多數非靜態變量和方法,以使您的類成為真正的OOPS兼容類,從而使它們能夠利用OOPS編程的優點,包括降低程序復雜性和互連性(減少耦合)。

例如

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

public class MyTttFoo extends JPanel {
   // it's OK for constants to be static
   private static final long serialVersionUID = 1L;
   private static final int ROWS = 3;

   // use a larger Font to make buttons larger
   private static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 60);
   private static final String BLANK = "   ";
   private static final String X = "X";
   private static final String O = "O";

   // but not most variables
   private JButton[][] buttonGrid = new JButton[ROWS][ROWS];

   public MyTttFoo() {
      setBackground(Color.black);

      // use layout managers to help you create your GUI
      setLayout(new GridLayout(ROWS, ROWS, 1, 1));
      ActionListener btnListener = new ButtonListener();
      // create your buttons and add them only **once**
      for (int row = 0; row < buttonGrid.length; row++) {
         for (int col = 0; col < buttonGrid[row].length; col++) {
            JButton button = new JButton(BLANK);
            button.setFont(BTN_FONT);
            button.addActionListener(btnListener);
            add(button);  // add button to a gridlayout using component
            buttonGrid[row][col] = button; // and assign into the array
         }
      }
   }

   private class ButtonListener implements ActionListener {
      private boolean xTurn = true;

      @Override
      public void actionPerformed(ActionEvent e) {
         AbstractButton btn = (AbstractButton) e.getSource();
         String txt = btn.getText();
         if (txt.equals(BLANK)) {
            if (xTurn) {
               btn.setText(X);
            } else {
               btn.setText(O);               
            }
            xTurn = !xTurn;
         } 
      }
   }

   private static void createAndShowGui() {
      MyTttFoo mainPanel = new MyTttFoo();

      JFrame frame = new JFrame("MyTttFoo");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

暫無
暫無

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

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