【发布时间】:2015-03-01 14:23:07
【问题描述】:
我希望为 JAVA 开发一个猜词游戏,但我在开发 GUI 时遇到了一些困难。我不是想开发一个只使用一个文本字段的简单 GUI,我想开发一个更像移动设备的 GUI。因此,我希望白框显示为单词中字符的占位符。如下图所示。
基本上,玩家将能够从下方将字符(我可能会让它们是按钮或其他东西)拖放到占位符框中,以便与该位置的单词字符进行比较。我试过制作一个文本字段的数组列表,但是失败了。有人对如何进行有任何建议吗?
根据 MadProgrammer 提供的答案更新
在使用了一些 MadProgrammer 的代码后,我为我的视图包提出了以下包结构。
> Views
>> GameBoard.java
>> HighScorePanel.java
>> MainPanel.java
>> ScorePanel.java
>> StatisticPanel.java
>> TimerPanel.java
>> ViewConfig.java
>> WordPanel.java
本质上只是一堆放在 GameBoard.java 类中的面板。我遇到的主要问题是 MainPanel.java 类,并保持纵横比。所以下面是到目前为止的视图图像,它看起来还不错,但是单词提示和类别正好与文本字段相对,并且文本字段不够宽。
如果我放大这个框架,组件之间的间距将保持完全相同,并且不会按比例放大。所以基本上我在寻找一种方法来动态设置组件之间的间距。我已将 GameBoard.java、MainPanel.java 和 StatisticPanel.java 的代码放在下面。 GameBoard 由 MainPanel 和 StatisticsPanel 组成。 StatisticsPanel 由 ScorePanel、TimerPanel 和 HighScorePanel 组成。最后 MainPanel 由 MadProgrammer 之前建议的位组成。
GameBoard.java
package views;
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class GameBoard extends JFrame{
private StatisticsPanel sp = new StatisticsPanel();
private MainPanel mp = new MainPanel();
public GameBoard() {
// set the title for the game board
setTitle(ViewConfig.DEFAULT_GAME_TITLE);
// add panel components to board
add(sp, BorderLayout.PAGE_START);
add(mp, BorderLayout.CENTER);
// set the board size
setSize(ViewConfig.DEFAULT_FRAME_WIDTH, ViewConfig.DEFAULT_FRAME_HEIGHT);
// set the default close operation
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
GameBoard gb = new GameBoard();
// set visible
gb.setVisible(true);
}
}
StatisticsPanel.java
package views;
import java.awt.FlowLayout;
import javax.swing.JPanel;
public class StatisticsPanel extends JPanel {
private ScorePanel sp = new ScorePanel();
private TimerPanel tp = new TimerPanel();
private HighScorePanel hsp = new HighScorePanel();
protected StatisticsPanel() {
// Create a layout for the panel, flow layout in this case as each component should line up
// horizontally
FlowLayout panelLayout = new FlowLayout(FlowLayout.CENTER, (ViewConfig.DEFAULT_FRAME_WIDTH/6), 4);
// Set the panel's layout to the newly created layout
setLayout(panelLayout);
// add components
add(sp);
add(tp);
add(hsp);
}
}
MainPanel.java
package views;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainPanel extends JPanel {
private JButton submitAns, clearAns;
private JLabel wordHint, wordCategory;
private WordPanel wp = new WordPanel(ViewConfig.DEFAULT_GUESS_WORD.length());
protected MainPanel() {
// create the buttons to submit and clear answer
submitAns = new JButton(ViewConfig.DEFAULT_SUBMIT);
clearAns = new JButton(ViewConfig.DEFAULT_CLEAR);
// Create new default labels for the word hint and category
wordHint = new JLabel(ViewConfig.DEFAULT_WORD_HINT);
wordCategory = new JLabel(ViewConfig.DEFAULT_WORD_CATEGORY);
// Set layout manger to GridBagLayout
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(4, 4, 4, 4);
// add all components to panel with following format:
// Word Hint > Input Fields > Word Category > Buttons
add(wordHint, gbc);
add(wp, gbc);
add(wordCategory, gbc);
// Create secondary JPanel to group buttons together, give it flow layout and add button panel to parent
JPanel buttonPanel = new JPanel();
FlowLayout buttonPanelLayout = new FlowLayout(FlowLayout.CENTER, 10, 4);
buttonPanel.add(submitAns, buttonPanelLayout);
buttonPanel.add(clearAns, buttonPanelLayout);
// add button panel to parent
add(buttonPanel);
}
protected JLabel getWordHint() {
return wordHint;
}
protected void setWordHint(String wordHint) {
this.wordHint.setText(wordHint);
}
protected JLabel getWordCategory() {
return wordCategory;
}
protected void setWordCategory(String wordCategory) {
this.wordCategory.setText(wordCategory);
}
}
【问题讨论】:
-
您可以使用 GridLayout 将 JLabels 放置在 JPanel 上...
-
不是一个选项。太丑了。目前的问题是您无法将 JTextFields 的数组列表添加到 JPanel
标签: java