Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带矩形的kakuro gui布局网格_Java_User Interface_Puzzle - Fatal编程技术网

Java 带矩形的kakuro gui布局网格

Java 带矩形的kakuro gui布局网格,java,user-interface,puzzle,Java,User Interface,Puzzle,我是Java图形新手,需要一些帮助。我正在尝试为游戏kakuro创建一个gui。在这里,您可以看到电路板外观的图像。我已经有了一些用矩形创建网格的代码,但我不知道如何准确地在矩形的中心填充数字,最重要的是,如何用一条线将一些矩形对角分割,在其中需要放置两个不同的数字,如示例所示 import java.awt.Graphics; import javax.swing.JComponent; import javax.swing.JFrame; class Gri

我是Java图形新手,需要一些帮助。我正在尝试为游戏kakuro创建一个gui。在这里,您可以看到电路板外观的图像。我已经有了一些用矩形创建网格的代码,但我不知道如何准确地在矩形的中心填充数字,最重要的是,如何用一条线将一些矩形对角分割,在其中需要放置两个不同的数字,如示例所示

    import java.awt.Graphics;
    import javax.swing.JComponent;
    import javax.swing.JFrame;

    class Grid extends JComponent {
    public void paint(Graphics g) {
    int width=30;
    int height=30;    
    for(int x=0;x<10;x++)
    {
        for(int y=0 ;y < 10;y++)
        {
            // create rectangles
            g.drawRect(x*width,y*height,width,height);
            // Fill in values. However, how to make it in the centre?
            g.drawString("2", x*width,y*height);
        }
    }
   }
  }

   public class Cube {

   public static void main(String[] a) {
    JFrame window = new JFrame();
    window.setSize(200,200);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().add(new Grid());
    window.setVisible(true);
  }
}
导入java.awt.Graphics;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
类网格扩展了JComponent{
公共空间涂料(图g){
整数宽度=30;
整数高度=30;

对于(int x=0;x您可以在
JButton上使用一对
JLabel
,使用
BorderLayout

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class KakuroCorner {

    private JFrame frame;
    private JButton myButton1;
    private JLabel myButton1_Label_E;
    private JLabel myButton1_Label_S;

    public KakuroCorner() {
        myButton1_Label_E = new JLabel("3");
        myButton1_Label_E.setHorizontalAlignment(JLabel.CENTER);
        myButton1_Label_E.setForeground(Color.white);

        myButton1_Label_S = new JLabel("45");
        myButton1_Label_S.setHorizontalAlignment(JLabel.CENTER);
        myButton1_Label_S.setForeground(Color.white);

        myButton1 = new JButton();
        myButton1.setBackground(Color.black);
        myButton1.setLayout(new BorderLayout());
        myButton1.add(myButton1_Label_E, BorderLayout.EAST);
        myButton1.add(myButton1_Label_S, BorderLayout.SOUTH);
        myButton1.setEnabled(false);

        frame = new JFrame();
        frame.add(myButton1);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() { new KakuroCorner(); }
        });
    }
}