2016-11-09 43 views
0

我要设计,所有的手为德州扑克的可能组合显示在屏幕上使用Java的用户界面,所以: Something Like this设计一个扑克UI与矩阵和Jbutton将在Java中

我一个Java初学者如此裸露在我身边......我知道我基本上需要169个方块(Jbuttons)或一个13x13“桌子”。所以我遇到了两个问题,我可以设计一个界面,我有这169个单独的按钮,但我如何分配它们?我知道我需要两个循环(一个在另一个内)来生成列表,但我无法掌握如何。 我的一个朋友建议我使用矩阵,而不是使用for循环创建按钮...

这是我的基本想法,但我想改变按钮的颜色,当我按下它们并发送任何写在他们的JText ...

任何想法表示赞赏,并感谢您抽出时间来阅读这篇

package modelo; 
import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class RangoImpreso implements ActionListener{ 

    JButton []ArrayButton = new JButton[169];//Buttons 
    JPanel jp1; 
    JLabel jl1, jl2; 

    private RangoImpreso() 
    { 

     JFrame frMain = new JFrame("Rango Pre-Flop"); 
     frMain.setLayout(new BorderLayout(10, 20)); 

     jl1 = new JLabel(); //jlabel that outputs number pressed  
     jl1.setText("Aquí ira el numero que se pulse"); 

     mostrarBot(); 

     frMain.add(jl1, BorderLayout.NORTH); 
     frMain.add(jp1, BorderLayout.SOUTH); 

     frMain.setSize(600, 600); 
     frMain.setLocation(700, 300); 
     frMain.setVisible(true); 
     frMain.setResizable(false); 
     frMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    } 

    public void mostrarBot() //method where the Jpanel is 
    { 
     jp1 = new JPanel(new GridLayout(13, 13, 0, 0)); 

     for(int i=ArrayButton.length-1; i>=0; i--) //create buttons, add properties 
     { 
      ArrayButton[i] = new JButton(""+(i+1)); 
      jp1.add(ArrayButton[i]); 
      ArrayButton[i].setMargin(new Insets(0, 0, 0, 0)); 
      ArrayButton[i].addActionListener(this); 
     } 
    } 

    public static void main(String[] args) 
    {   
     RangoImpreso trin = new RangoImpreso();  
    } 

    @Override 
    public void actionPerformed(ActionEvent e) //Use jlabel to show button pressed 
    {   
     jl1.setText(e.getActionCommand()); 
    } 
} 

P

回答

0

这是两个for循环您需要创建按钮

  • ö外环=指数
  • I =内环

两者都从0到12计数的索引,以便将创建13×13个按钮

for(int o = 0; o < 13; o++) { 
    for(int i = 0; i < 13; i++) { 
     //Create you buttons 
     JButton btn = new JButton(); 
     //Add Handlers to change color and send text to textfield here... 
    } 
} 
+0

由于一个很多!我结束了使用Jlabels而不是单独的类来将它们稍后合并到一个框架中,与boxlayouts等有一些乐趣! – pablo