在JFrame中绘制多行(最小9x9)

时间:2014-11-25 16:45:17

标签: java jframe

好的,现在就显示出来了。专家组被我绘制的专线所掩盖,我目前正在研究使用哪种布局。 BorderLayout似乎不像我想要的那样工作,但还有很多其他的尝试。感谢您的帮助,如果您有任何其他建议,我很乐意听到。

在我登录之前的另一个补充:我已经确定了GridLayout,因为我可以控制所有组件的大小,然后调整框架的大小以适应它们。现在我正在研究投入。如何从JPanel中的TextFields获取信息到JComponent中以用作数据?问题出现在靠近底部的JComponent类的paintComponent方法中。我将使用此解决方案来帮助项目的网络部分。

这是我到目前为止的代码:

import javax.swing.*;
import java.awt.*;
public class GoBoard extends JFrame
{
   public static void main(String[] args)
   {
      Graphics g;
      JFrame goframe=new JFrame("Go");
      goframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
      goframe.setSize(600,700);
      goframe.setLayout(new GridLayout());
      JPanel panel=new JPanel();
      JComponent component=new gocomponent();
      panel.setBackground(Color.WHITE);
      JButton passbutton=new JButton("Pass");
      JButton sendbutton=new JButton("Send");
      JLabel label=new JLabel("Enter the coordinates of the intesection you want to play on.");
      JLabel label2=new JLabel("Click send, Or click pass");
      JLabel blank=new JLabel("        ");
      JLabel blank2=new JLabel("        ");
      JTextField text1=new JTextField(10);
      JTextField text2=new JTextField(10);
      panel.add(label);
      panel.add(label2);
      panel.add(blank);
      panel.add(blank2);
      panel.add(passbutton);
      panel.add(sendbutton);
      panel.add(text1);
      panel.add(text2);
      component.setOpaque(false);
      component.setSize(450,450);
      component.setVisible(true);
      goframe.add(component);
      goframe.add(panel);
      goframe.setVisible(true);
   }
}

class gocomponent extends JComponent
{
   public void paintComponent(Graphics g)
   {
     Graphics2D g2 = (Graphics2D) g;
     for(int i=0;i<=9;i++)
     {
         drawhorizontal(g2, 0, i*50, 450, i*50);
         drawvertical(g2, i*50, 0, i*50, 450);
     }
     drawpiece(g2, text1.getText(), text2.getText(), true);
   }
   public void drawhorizontal(Graphics2D g2, int x1, int y1, int x2, int y2)
   {
      g2.setColor(Color.black);
      g2.drawLine(x1, y1, x2, y2);
   }
   public void drawvertical(Graphics2D g2, int x1, int y1, int x2, int y2)
   {
      g2.setColor(Color.black);
      g2.drawLine(x1, y1, x2, y2);
   }
   public void drawpiece(Graphics2D g2, int x, int y, boolean player)
   {
      if(player==true)
      {
         g2.setColor(Color.black);
      }
      if(player==false)
      {
         g2.setColor(Color.white);
      }
      g2.fillOval(x, y, 10, 10);
   }
}

1 个答案:

答案 0 :(得分:0)

我必须修改你的代码才能让它编译。我做了几处修改,为您的持续发展提供了一个起点。

这是我的Go Frame版本的样子。

enter image description here

所以,这是我所做的改变。

首先,必须始终通过调用SwingUtilities.invokeLater启动Swing应用程序。

public static void main(String[] args) {
    SwingUtilities.invokeLater(new GoBoard());
}

其次,我将JFrame和JPanels的构建分离为单独的方法/类。这使您可以一次关注GUI的一部分。

这里是JFrame的代码。

public void run() {
    JFrame goframe = new JFrame("Go");
    goframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel mainPanel = new JPanel();
    mainPanel.add(new GoPanel());
    mainPanel.add(createButtonPanel());

    goframe.add(mainPanel);

    goframe.pack();
    goframe.setLocationByPlatform(true);
    goframe.setVisible(true);
}

这里是JPanel按钮的代码。我使用GridBagLayout来获取文本字段和按钮以更好地排列。

private JPanel createButtonPanel() {
    JPanel panel = new JPanel();
    panel.setBackground(Color.WHITE);
    panel.setLayout(new GridBagLayout());

    int gridy = 0;

    JLabel label = new JLabel(
            "Enter the coordinates of the intesection you want to play on.");
    addComponent(panel, label, 0, gridy++, 2, 1,
            normalInsets, GridBagConstraints.LINE_START,
            GridBagConstraints.HORIZONTAL);

    JLabel label2 = new JLabel("Click send, Or click pass");
    addComponent(panel, label2, 0, gridy++, 2, 1,
            normalInsets, GridBagConstraints.LINE_START,
            GridBagConstraints.HORIZONTAL);

    text1 = new JTextField(10);
    addComponent(panel, text1, 0, gridy, 1, 1,
            normalInsets, GridBagConstraints.LINE_START,
            GridBagConstraints.HORIZONTAL);

    text2 = new JTextField(10);
    addComponent(panel, text2, 1, gridy++, 1, 1,
            normalInsets, GridBagConstraints.LINE_START,
            GridBagConstraints.HORIZONTAL);

    JButton passbutton = new JButton("Pass");
    addComponent(panel, passbutton, 0, gridy, 1, 1,
            normalInsets, GridBagConstraints.LINE_START,
            GridBagConstraints.HORIZONTAL);

    JButton sendbutton = new JButton("Send");
    addComponent(panel, sendbutton, 1, gridy++, 1, 1,
            normalInsets, GridBagConstraints.LINE_START,
            GridBagConstraints.HORIZONTAL);

    return panel;
}

private void addComponent(Container container, Component component,
        int gridx, int gridy, int gridwidth, int gridheight, 
        Insets insets, int anchor, int fill) {
    GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
            gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, 
            insets, 0, 0);
    container.add(component, gbc);
}

最后,这里是Go棋盘的课程。你将不得不在外面留出一些空间,这样一块完整的石头就会在第一点上画出来。

我几乎把你的代码留下了原样,除非你在某人进入石头坐标之前就不能画石头。

public class GoPanel extends JPanel {

    private static final long serialVersionUID = -5957209004151437002L;

    public GoPanel() {
        this.setPreferredSize(new Dimension(451, 451));
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        for (int i = 0; i <= 10; i++) {
            drawhorizontal(g2, 0, i * 50, 450, i * 50);
            drawvertical(g2, i * 50, 0, i * 50, 450);
        }
        String a = text1.getText();
        String b = text2.getText();

        if (a.equals("") || b.equals("")) {

        } else {
            int x = Integer.valueOf(text1.getText());
            int y = Integer.valueOf(text2.getText());
            drawpiece(g2, x, y, true);
        }
    }

    public void drawhorizontal(Graphics2D g2, int x1, int y1, int x2, int y2) {
        g2.setColor(Color.black);
        g2.drawLine(x1, y1, x2, y2);
    }

    public void drawvertical(Graphics2D g2, int x1, int y1, int x2, int y2) {
        g2.setColor(Color.black);
        g2.drawLine(x1, y1, x2, y2);
    }

    public void drawpiece(Graphics2D g2, int x, int y, boolean player) {
        if (player == true) {
            g2.setColor(Color.black);
        }
        if (player == false) {
            g2.setColor(Color.white);
        }
        g2.fillOval(x, y, 10, 10);
    }
}

最后,这里是整个可运行的代码。我把这些类放在GoBoard类中,以便更容易粘贴。您应该将每个类放在一个单独的文件中。

package com.ggl.testing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GoBoard implements Runnable {

    private static final Insets normalInsets = 
            new Insets(0, 0, 5, 5);

    private JTextField text1;
    private JTextField text2;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new GoBoard());
    }

    @Override
    public void run() {
        JFrame goframe = new JFrame("Go");
        goframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.add(new GoPanel());
        mainPanel.add(createButtonPanel());

        goframe.add(mainPanel);

        goframe.pack();
        goframe.setLocationByPlatform(true);
        goframe.setVisible(true);
    }

    private JPanel createButtonPanel() {
        JPanel panel = new JPanel();
        panel.setBackground(Color.WHITE);
        panel.setLayout(new GridBagLayout());

        int gridy = 0;

        JLabel label = new JLabel(
                "Enter the coordinates of the intesection you want to play on.");
        addComponent(panel, label, 0, gridy++, 2, 1,
                normalInsets, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        JLabel label2 = new JLabel("Click send, Or click pass");
        addComponent(panel, label2, 0, gridy++, 2, 1,
                normalInsets, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        text1 = new JTextField(10);
        addComponent(panel, text1, 0, gridy, 1, 1,
                normalInsets, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        text2 = new JTextField(10);
        addComponent(panel, text2, 1, gridy++, 1, 1,
                normalInsets, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        JButton passbutton = new JButton("Pass");
        addComponent(panel, passbutton, 0, gridy, 1, 1,
                normalInsets, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        JButton sendbutton = new JButton("Send");
        addComponent(panel, sendbutton, 1, gridy++, 1, 1,
                normalInsets, GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        return panel;
    }

    private void addComponent(Container container, Component component,
            int gridx, int gridy, int gridwidth, int gridheight, 
            Insets insets, int anchor, int fill) {
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
                gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, 
                insets, 0, 0);
        container.add(component, gbc);
    }

    public class GoPanel extends JPanel {

        private static final long serialVersionUID = -5957209004151437002L;

        public GoPanel() {
            this.setPreferredSize(new Dimension(451, 451));
        }

        @Override
        public void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            for (int i = 0; i <= 10; i++) {
                drawhorizontal(g2, 0, i * 50, 450, i * 50);
                drawvertical(g2, i * 50, 0, i * 50, 450);
            }
            String a = text1.getText();
            String b = text2.getText();

            if (a.equals("") || b.equals("")) {

            } else {
                int x = Integer.valueOf(text1.getText());
                int y = Integer.valueOf(text2.getText());
                drawpiece(g2, x, y, true);
            }
        }

        public void drawhorizontal(Graphics2D g2, int x1, int y1, int x2, int y2) {
            g2.setColor(Color.black);
            g2.drawLine(x1, y1, x2, y2);
        }

        public void drawvertical(Graphics2D g2, int x1, int y1, int x2, int y2) {
            g2.setColor(Color.black);
            g2.drawLine(x1, y1, x2, y2);
        }

        public void drawpiece(Graphics2D g2, int x, int y, boolean player) {
            if (player == true) {
                g2.setColor(Color.black);
            }
            if (player == false) {
                g2.setColor(Color.white);
            }
            g2.fillOval(x, y, 10, 10);
        }
    }
}