通过java中的按钮获取输入

发布于 2024-09-04 05:05:16 字数 874 浏览 3 评论 0原文

我知道标题可能描述性不够,但我正在用 Java 制作一个魔方游戏,基本上,我正在尝试复制数独游戏中的用户输入:http://www.websudoku.com/

我所拥有的是一个按钮(不是 JButton)网格作为棋盘,我希望用户能够做的是,当用户单击其中一个按钮时,类似于上面的游戏,它允许用户输入他猜测按钮本身,而不是弹出一个带有某种输入字段的对话框。

我不知道从哪里开始,我是 Java 初学者(不是很初学者,但我对各种 Java API 的了解非常有限),所以我试图找出这是否可行以及是否可行,我该怎么做呢?感谢您的任何帮助。


我不知道这是否属于评论或答案,但我将其发布为上一个问题的扩展。谢谢你们两位的帮助。

我基本上已经实现了我对该计划的预期目标。但现在,我想通过让它以多线程方式运行来使其更好。对于可以将这种幻方程序的哪些方面委托给单独的线程,你们有什么建议吗?

截至目前,程序所做的事情是:提示用户输入正方形大小> >。构建 nxn 网格,当用户单击开始按钮时,它开始为用户计时。当用户输入新条目时,棋盘会不断更新每行、每列和对角线上的总计,并更新相应的框。最后,当对成功的魔方板的检查结果为真时,它会禁用所有文本字段并让用户开始新游戏或退出。

再说一次,虽然我已经用 Java 编程了一段时间,但我的知识非常有限,现在,我想了解如何以多线程方式构建像这样的简单程序,尽管它们在单线程上运行良好。

最后,我检查了运行程序时使用的内存(基本上是在 Windows 中的 javaw.exe 下的任务管理器中查看),对于 19 x 19 的网格,它显示大约 19000K。这是太多还是正确?如果太多,我该如何减少?

非常感谢您的帮助。

I understand that the title might not be descriptive enough, but I'm making a magic square game in Java and basically, I'm trying to replicate user input as found in the sudoku game here: http://www.websudoku.com/.

What I have is a n x n grid of Buttons (not JButton) as the board and what I want the user to be able to do is when the user clicks on one of the buttons, similar to the game above, it allows the user to type in his guess in the button itself instead of popping up a dialog box with an input field of some sort.

I don't know where to start, I am a beginner in Java (not very beginner, but my knowledge with the various Java APIs is very limited), so I'm trying to find out if this would be possible and if it is, how would I go about doing it? Thanks for any help.


I didn't know if this belonged in comments or answers, but I am posting this as an extension to the previous question. Thank you, both of you, for your help.

I have basically achieved what I set out to do with the program. But now, I want to make it better by having it operate in a multithreaded way. Do any of you have any suggestions as to what aspect of such a magic square program could be delegated to separate threads.

As of now, what the program does is this: prompt the user for the square size > build n x n grid and when user clicks a start button, it starts timing the user. while the user inputs a new entry, the board constantly updates the totals on each row, column and diagonal and updates the appropriate boxes. Finally, when the check for a successful magic square board turns out true, it disables all the textfields and lets the user start a new game or exit.

Once again, while I have programmed in Java for a while, my knowledge is very limited and right now, I want to understand how simple programs like these, while they work well on a single thread, can be built in a multithreaded way.

Lastly, I was checking the Memory used when I run my program (basically looking through task manager in windows, under javaw.exe) and for a grid of 19 x 19, it says about 19000K. Is this too much or about right and if its too much, how can I reduce it?

Thank you greatly for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽心 2024-09-11 05:05:16

我会避免这种 GUI 设计,因为它不会像用户期望的那样工作。但是,如果您确实想要这种行为,我建议您从 JTextField 开始,并给它一个 BevelBorder。像这样的东西:

JTextField tf = new JTextField();
tf.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
yourPanel.add(tf);

一个完整​​的存根程序可能看起来像这样:

import java.awt.*;

import javax.swing.*;
import javax.swing.border.*;

public class FrameTest {
    public static void main(String[] args) throws InterruptedException {

        JFrame jf = new JFrame("Demo");
        jf.getContentPane().setLayout(new GridLayout(9, 9, 3, 3));    

        for (int i = 0; i < 9*9; i++) {
            JTextField tf = new JTextField(1);
            tf.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            tf.setBackground(new Color(230, 230, 230));
            tf.setHorizontalAlignment(JTextField.CENTER);
            tf.setFont(tf.getFont().deriveFont(20f));
            jf.add(tf);
        }

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);

    }
}

在此处输入图像描述

I would avoid this gui design, since it will not work as the user expects it to. However, if you really want the behavior, I suggest you start with a JTextField and give it a BevelBorder. Something like this:

JTextField tf = new JTextField();
tf.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
yourPanel.add(tf);

A complete stub program could look something like this:

import java.awt.*;

import javax.swing.*;
import javax.swing.border.*;

public class FrameTest {
    public static void main(String[] args) throws InterruptedException {

        JFrame jf = new JFrame("Demo");
        jf.getContentPane().setLayout(new GridLayout(9, 9, 3, 3));    

        for (int i = 0; i < 9*9; i++) {
            JTextField tf = new JTextField(1);
            tf.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            tf.setBackground(new Color(230, 230, 230));
            tf.setHorizontalAlignment(JTextField.CENTER);
            tf.setFont(tf.getFont().deriveFont(20f));
            jf.add(tf);
        }

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);

    }
}

enter image description here

我们只是彼此的过ke 2024-09-11 05:05:16

听起来您真正想要的是 可编辑表格。实际上,您可以使用按钮作为 TableCellRenderer,使用文本字段作为 TableCellEditor。

It sounds like instead of a grid of buttons, what you really want is an editable table. You could actually use a button as TableCellRenderer and a textfield as TableCellEditor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文