Java 使用唯一的图像图标动态初始化jbutton
这是我第一次尝试在Java中使用GUI布局。我正在尝试创建一个简单的记忆卡游戏,在这个游戏中,用户翻转两张卡,并尝试获得一个匹配,如果没有匹配,他们会翻转过来,如果有匹配,他们会一直翻转,直到你获得所有匹配。我可能会让自己很难受,因为我让整个游戏对定义卡片列数和行数的构造函数变量是动态的。我认为这比硬编码某个值要好,但现在我很难将img文件夹中的图像放到我的卡片上 我知道Java中不允许使用变量的变量&作为一名ColdFusion开发人员,这对我来说真的很难适应。你能帮我想出用Java实现这一点的方法吗 这是我的代码的简化版本Java 使用唯一的图像图标动态初始化jbutton,java,dynamic,jframe,jpanel,jbutton,Java,Dynamic,Jframe,Jpanel,Jbutton,这是我第一次尝试在Java中使用GUI布局。我正在尝试创建一个简单的记忆卡游戏,在这个游戏中,用户翻转两张卡,并尝试获得一个匹配,如果没有匹配,他们会翻转过来,如果有匹配,他们会一直翻转,直到你获得所有匹配。我可能会让自己很难受,因为我让整个游戏对定义卡片列数和行数的构造函数变量是动态的。我认为这比硬编码某个值要好,但现在我很难将img文件夹中的图像放到我的卡片上 我知道Java中不允许使用变量的变量&作为一名ColdFusion开发人员,这对我来说真的很难适应。你能帮我想出用Java实现这一点
import javax.swing.JFrame;
public class MemoryApp
{
public static void main(String args[])
{
//Creates a new game with 3 columns and 4 rows
final CardGame myGame = new CardGame(3, 4);
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI(myGame.getCols(), myGame.getRows());
}
});
}
private static void createAndShowGUI(int c, int r) {
//Create and set up the window.
Window frame = new Window("GridLayoutDemo", c, r);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
frame.addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
纸牌游戏类别:
public class CardGame
{
private int cols, rows;
public CardGame(int c, int r)
{
cols = c;
rows = r;
}
public int getCols(){
return cols;
}
public int getRows(){
return rows;
}
}
具有网格布局的窗口:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
public class Window extends JFrame
{
private int cols, rows;
GridLayout windowGrid = new GridLayout(1,1);
public Window(String t, int c, int r)
{
super(t);
cols = c;
rows = r;
windowGrid.setColumns(c);
windowGrid.setRows(r);
}
public void addComponentsToPane(final Container pane)
{
final JPanel compsToExperiment = new JPanel();
compsToExperiment.setLayout(windowGrid);
JPanel controls = new JPanel();
controls.setLayout(new GridLayout(cols,rows));
int countCard = (cols * rows) / 2;
/**
* Add buttons to experiment with Grid Layout.
* This is where I'd like to be able to loop through
* countCard and create the required number of buttons
* as well as put images on the buttons like so:
*
* ImageIcon image1 = new ImageIcon(getClass().getResource("card1.png"));
* through
* ImageIcon image1 = new ImageIcon(getClass().getResource("card" & cardCount & ".png"));
*
* Below is how I would attempt this in ColdFusion- I know
* the variable of variables syntax is invalid, it is just
* to show you what I mean.
*/
// for(int i = 0; i < countCard; i++;)
// {
// compsToExperiment.add(new JButton("../img/card" & i & ".png"));
// ImageIcon variables["image" & i] = new ImageIcon(getClass().getResource("card" & i & ".png"));
// imageButton.setIcon(variables["image" & i]);
// etc. with ButtonClickEventHandler, haven't gotten that far yet
// }
pane.add(compsToExperiment, BorderLayout.NORTH);
pane.add(new JSeparator(), BorderLayout.CENTER);
}
}
导入java.awt.BorderLayout;
导入java.awt.Container;
导入java.awt.GridLayout;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.jsepator;
公共类窗口扩展了JFrame
{
私有整数列、行;
GridLayout windowGrid=新的GridLayout(1,1);
公共窗口(字符串t、整数c、整数r)
{
超级(t);
cols=c;
行=r;
windowGrid.setColumns(c);
windowGrid.setRows(r);
}
公共void addComponentsToPane(最终容器窗格)
{
最终JPanel CompstoExperience=新JPanel();
compsToExperiment.setLayout(windowGrid);
JPanel controls=新的JPanel();
setLayout(新的GridLayout(cols,rows));
int countCard=(列*行)/2;
/**
*添加按钮以试验网格布局。
*这是我希望能够循环通过的地方
*计数卡并创建所需数量的按钮
*以及在按钮上放置图像,如下所示:
*
*ImageIcon image1=新的ImageIcon(getClass().getResource(“card1.png”);
*通过
*ImageIcon image1=新的ImageIcon(getClass().getResource(“card”&cardCount&.png”);
*
*下面是我在ColdFusion中如何尝试的-我知道
*变量的变量语法无效,它只是
*让你明白我的意思。
*/
//对于(int i=0;i
根据到目前为止您发布的代码注释代码,一种方法可能是这样的:
public class Window extends JFrame
{
...
// A java.util.List that stores all the buttons, so
// that their icons may be changed later
private List<JButton> buttons = new ArrayList<JButton>();
// A java.util.List that stores all the ImageIcons that
// may be placed on the buttons
private List<ImageIcon> imageIcons = new ArrayList<ImageIcon>();
public void addComponentsToPane(final Container pane)
{
...
for(int i = 0; i < countCard; i++;)
{
// String concatenation is done with "+" in Java, not with "&"
String fileName = "card" + i + ".png";
// Create the icon and the button containing the icon
ImageIcon imageIcon = new ImageIcon(getClass().getResource(fileName));
JButton button = new JButton(imageIcon);
// Add the button to the main panel
compsToExperiment.add(button);
// Store the button and the icon in the lists
// for later retrieval
imageIcons.add(imageIcon);
buttons.add(button);
// Attach an ActionListener to the button that will
// be informed when the button was clicked.
button.addActionListener(createActionListener(i));
}
}
private ActionListener createActionListener(final int cardIndex)
{
return new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
clickedCardButton(cardIndex);
}
};
}
private void clickedCardButton(int cardIndex)
{
System.out.println("Pressed button for card "+cardIndex);
// Here you can change the icons of the buttons or so...
JButton button = buttons.get(cardIndex);
ImageIcon imageIcon = imageIcons.get(cardIndex);
....
}
公共类窗口扩展JFrame
{
...
//存储所有按钮的java.util.List,因此
//他们的图标可能会在以后更改
私有列表按钮=新建ArrayList();
//一个java.util.List,其中存储了
//可以放在按钮上
private List imageIcons=new ArrayList();
公共void addComponentsToPane(最终容器窗格)
{
...
对于(int i=0;i
你提到这是你第一次用java构建GUI。所以我假设这只是为了“练习”。如果你打算建立一个“实际应用”,你应该考虑一些模型视图控制器(MVC)。这方面的方法。
非常酷-这篇文章编译得很漂亮,我可以很容易地理解。没错,这或多或少是一种实践。我对MVC概念的理解还不足以实现它。我想我需要以某种方式使用列表&这真的帮助了它。