2013-03-08 104 views
1

我正在使用帶有GridBagLayout的JFrame,我希望能夠從特定的x,y座標獲取組件(在我的情況下是JButton)。不應該這樣做,因爲我給了它x,y座標GridBagConstraints開始?從GridBagLayout中的特定座標獲取組件

有沒有簡單的方法來做到這一點?

+0

您應該註冊'mouseClicked'事件給每個組件,而不是..這將幫助你找回所有的組件點擊了的JFrame .. – 2013-03-08 18:44:17

+0

我」什麼m試圖做的是採取一定的JButton並在它周圍的方形模式上執行JButtons上的一組操作。 即,如果我的按鈕在1,1我希望能夠得到按鈕0,0,1,0,2,0,0,1,2,1等等。 – Bamapag 2013-03-08 18:52:32

回答

0

enter image description here
這裏是實現你所尋找的一個小荒謬的例子。我希望這將是對你有幫助...

import javax.swing.JFrame; 
import javax.swing.JButton; 
import javax.swing.SwingUtilities; 
import java.awt.GridBagLayout; 
import java.awt.GridBagConstraints; 
import java.awt.Color; 
import java.awt.Point; 
import java.awt.Container; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.util.Map; 
import java.util.Set; 
import java.util.LinkedHashMap; 
class GridBagFrame extends JFrame implements ActionListener 
{ 
    GridBagLayout gb ; 
    GridBagConstraints gc; 
    Map <Point,JButton> map ; 
    final int SIZE = 20; 
    public void createAndShowGUI() 
    { 
     gb = new GridBagLayout(); 
     gc = new GridBagConstraints(); 
     gc.fill = GridBagConstraints.BOTH; 
     map = new LinkedHashMap<Point,JButton>(); 
     Container container = getContentPane(); 
     container.setLayout(gb); 
     int x =0 , y = -1 ; 
     JButton[] button = new JButton[SIZE]; 
     for (int i = 0 ; i < SIZE ; i++) 
     { 
      button[i] = new JButton(String.valueOf(i + 1)); 
      if (i % 4 == 0) 
      { 
       x = 0 ; 
       y = y +1; 
      } 
      gc.gridx = x++; 
      gc.gridy = y; 
      gb.setConstraints(button[i],gc); 
      container.add(button[i]); 
      map.put(new Point(x,y),button[i]); 
      button[i].setActionCommand(x+","+y); 
      button[i].addActionListener(this); 
     } 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     pack(); 
     setResizable(false); 
     setVisible(true); 
    } 
    @Override 
    public void actionPerformed(ActionEvent evt) 
    { 
     resetAll(); 
     JButton source = (JButton)evt.getSource(); 
     String command = source.getActionCommand(); 
     String[] arr = command.split(","); 
     int x = Integer.parseInt(arr[0]); 
     int y = Integer.parseInt(arr[1]); 
     for (int iy = y - 1; iy <= y + 1; iy++) 
     { 
      for (int ix = x -1 ; ix <= x + 1 ; ix++) 
      { 
       JButton button = map.get(new Point(ix,iy)); 
       if (button != null) 
       { 
        button.setForeground(Color.red); 
       } 
      } 
     } 
     source.setForeground(Color.blue); 
    } 
    private void resetAll() 
    { 
     Set<Point> pointSet = map.keySet(); 
     for (Point point : pointSet) 
     { 
      map.get(point).setForeground(Color.black); 
     } 
    } 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       GridBagFrame frame = new GridBagFrame(); 
       frame.createAndShowGUI(); 
      } 
     }); 
    } 
} 
1

如果您知道所需的JFrame上的x,y座標,則可以使用GridBagLayout的location()方法找到什麼是單元格(GridBagConstraints中的x,y)。知道GridBagLayout上的x,y,您需要遍歷JFrame的組件,並找到GridBagConstraints中具有相同的x,y的元素,並將其與在getConstraints(Component comp)方法上給出的約束進行比較。

GridBagLayout API

2

看看我希望能夠得到的組件(對我來說一個JButton)從特定的x,y座標。

  1. 將ActionListener添加到每個按鈕。
  2. 在ActionListener代碼中,您可以使用ActionEvent的event.getSource()方法獲取您單擊的按鈕。
  3. 一旦知道源代碼,您就可以使用組件的getParent()方法獲得父容器。
  4. 然後,您可以使用容器的getLayout()方法獲取佈局管理器。
  5. 一旦你有了GridBagLayout,你可以使用getContraints()方法下注組件的約束條件。
  6. 然後你可以得到x/y座標。
+0

@Bamapag可能是另一個相當直接的工作方式,可以使用[putClientProperty](http://stackoverflow.com/a/13531549/714968),你可以將多個這種方法與任何額外的細節結合起來,在飛行中獲得這個座標, – mKorbel 2013-03-08 19:21:55