java button和jbutton_java – JButton中的组合与继承

我想用Swing创建一个简单的桌面游戏.我有一个JFrame和一个JPanel变量.

我想将JButtons添加到这个JPanel,但我想创建一个自己的类.

我创建了一个扩展JButton(继承)的类:

public class GameField extends JButton {...}

所以我可以将GameField添加到JPanel.

但我想通过组合创建GameFields:

public class GameField{

private JButton button;

}

但在这篇文章中我如何将GameField添加到JPanel?

我可以通过compisition解决这个问题吗?

解决方法:

But in this clase how I can add GameField to JPanel? Can I solve this

problem by compisition?

你这样做是通过添加一个这样的简单getter:

public class GameField{

private JButton button;

public GameField(String text) {

button = new JButton(text);

// do your stuff here

}

public JButton getButton() {

return button;

}

}

然后在你的GUI中:

public void createAndShowGUI() {

JPanel panel = new JPanel(new GridLayout(5,5));

panel.add(new GameField("Button # 1").getButton());

panel.add(new GameField("Button # 2").getButton());

...

JFrame frame = new JFrame("Game");

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.getContentPane().add(panel);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

编辑

你在评论中说过:

Thanks, but in this case if I’d like to access this field (i.e.

panel.getComponent(i)), I can get only a JButton, and not a GameField.

您可以使用GameField对象保留列表,也可以使用putClientProperty()方法保留对GameField对象的引用,如下例所示:

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

public class Demo {

private void createAndShowGUI() {

ActionListener actionListener = new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

JButton button = (JButton)e.getSource();

GameField gameField = (GameField)button.getClientProperty("GameField");

if(gameField != null) {

System.out.println(gameField.getText());

}

}

};

GameField gameField1 = new GameField("Button # 1");

gameField1.getButton().addActionListener(actionListener);

GameField gameField2 = new GameField("Button # 2");

gameField2.getButton().addActionListener(actionListener);

JPanel content = new JPanel(new GridLayout());

content.add(gameField1.getButton());

content.add(gameField2.getButton());

JFrame frame = new JFrame("Demo");

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.add(content);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

class GameField {

private String text;

private JButton button;

public GameField(String text) {

this.text = text;

button = new JButton(text);

button.putClientProperty("GameField", GameField.this);

}

public JButton getButton() {

return button;

}

public String getText() {

return text;

}

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

new Demo().createAndShowGUI();

}

});

}

}

标签:java,inheritance,swing

来源: https://codeday.me/bug/20190825/1716885.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值