开发者

Take input through Buttons in java

开发者 https://www.devze.com 2023-01-01 20:47 出处:网络
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

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.


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);

    }
}

Take input through Buttons in java


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号