java – JButton中的组合与继承
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – JButton中的组合与继承,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2886字,纯文字阅读大概需要5分钟。
内容图文
![java – JButton中的组合与继承](/upload/InfoBanner/zyjiaocheng/733/3f8e79bb83c347b397588ab2f7b8aaa9.jpg)
我想用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 – JButton中的组合与继承全部内容,希望文章能够帮你解决java – JButton中的组合与继承所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。