JAVA-GUI占位符
我希望为JAVA开发一个猜字游戏,但在开发GUI时遇到了一些困难。我不想开发一个简单的GUI,只使用一个文本字段,我想开发一个更具移动性的GUI。因此,我希望白框显示为单词中字符的占位符。它会像下面的图片一样 从本质上讲,玩家可以将角色从下方拖放到占位符框中(我可能会让它们成为按钮或其他东西),以便与该位置的单词的角色进行比较。我曾尝试创建一个文本字段数组列表,但失败得很惨。有人对如何进行有什么建议吗JAVA-GUI占位符,java,Java,我希望为JAVA开发一个猜字游戏,但在开发GUI时遇到了一些困难。我不想开发一个简单的GUI,只使用一个文本字段,我想开发一个更具移动性的GUI。因此,我希望白框显示为单词中字符的占位符。它会像下面的图片一样 从本质上讲,玩家可以将角色从下方拖放到占位符框中(我可能会让它们成为按钮或其他东西),以便与该位置的单词的角色进行比较。我曾尝试创建一个文本字段数组列表,但失败得很惨。有人对如何进行有什么建议吗 根据程序员提供的答案进行更新 在使用了MadProgrammer的一些代码之后,我为我的vi
根据程序员提供的答案进行更新 在使用了MadProgrammer的一些代码之后,我为我的views包设计了以下包结构
> Views
>> GameBoard.java
>> HighScorePanel.java
>> MainPanel.java
>> ScorePanel.java
>> StatisticPanel.java
>> TimerPanel.java
>> ViewConfig.java
>> WordPanel.java
本质上只是一堆放在GameBoard.java类中的面板。我遇到的主要问题是MainPanel.java类,以及保持纵横比。下面是到目前为止视图的图像,它看起来有点像样,但是单词hint&category正好对着文本字段,并且文本字段不够宽
如果我放大这个框架,组件之间的间距将保持不变,并且不会放大。所以本质上我在寻找一种方法来动态设置组件之间的间距。我已将GameBoard.java、MainPanel.java和StatisticPanel.java的代码放在下面。游戏板由主面板和统计面板组成。统计面板由ScorePanel、TimerPanel和HighScorePanel组成。最后,主面板由程序员先前建议的位组成
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);
}
}
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);
}
}
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);
}
}
统计面板.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);
}
}
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);
}
}
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);
}
}
MainPanel.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);
}
}
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);
}
}
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);
}
}
一个想法可能是使用一系列复合布局,您可以构建这些布局来满足您的需求 核心是一个
JPanel
,它将呈现精心制作的JLabel
s,能够显示单词的字符,例如
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GuessingGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GuessingGame extends JPanel {
public GuessingGame() {
setLayout(new GridBagLayout());
JLabel label = new JLabel("<html>A few sentences giving either a definition or a<br> few hints about what the word could be</html>");
WordPane wordPane = new WordPane("stackoverflow");
JLabel wordCategory = new JLabel("Word Category");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(4, 4, 4, 4);
add(label, gbc);
add(wordPane, gbc);
JPanel pnlWordCategory = new JPanel(new GridLayout(1, 6, 4, 4));
pnlWordCategory.add(new LetterLabel("A"));
pnlWordCategory.add(new LetterLabel("B"));
pnlWordCategory.add(new LetterLabel("C"));
pnlWordCategory.add(new LetterLabel("D"));
pnlWordCategory.add(new LetterLabel("W"));
pnlWordCategory.add(new LetterLabel("G"));
add(wordCategory, gbc);
add(pnlWordCategory, gbc);
}
}
public class WordPane extends JPanel {
private String word;
private List<JLabel> labels;
public WordPane(String text) {
setLayout(new GridLayout(1, text.length(), 4, 4));
labels = new ArrayList<>(text.length());
for (int index = 0; index < text.length(); index++) {
LetterLabel label = new LetterLabel();
labels.add(label);
add(label);
}
}
}
public class LetterLabel extends JLabel {
public LetterLabel(String text) {
this();
setText(text);
}
public LetterLabel() {
setBorder(new CompoundBorder(new LineBorder(Color.GRAY), new EmptyBorder(4, 4, 4, 4)));
}
@Override
public Dimension getPreferredSize() {
Insets insets = getInsets();
Dimension size = new Dimension();
FontMetrics fm = getFontMetrics(getFont());
size.width = (insets.left + insets.right) + fm.stringWidth("M");
size.height = (insets.top + insets.bottom) + fm.getHeight();
return size;
}
}
}
现在,控制。为了管理流程,您需要某种模型或控制器,能够从WordPane
获取输入并进行验证,然后通知WordPane
猜测是否有效,以便更新UI,但这超出了问题的范围
已更新
有许多方法可以实现将字符从一个部件拖动到另一个部件,但都不简单
可能最简单的方法是将控件的
字符串
值包装在一个可转移的
中,并在该字段被删除后进行更新,类似于您可以使用网格布局在JPanel上使用JLabels place…这不是一个选项。太难看了。目前的问题是,您不能将JTextFields的数组列表大量添加到jpanethank中。是的,我已经对控制器类有了想法,对模型也有了想法。非常感谢!要明白,D'n'D并不是世界上最简单的事情,看,啊,你是对的!但我想学点新东西。我只是不擅长图形部分,我肯定会努力去理解它,但这将是一次很好的学习经历