好了,现在它显示了。面板被我画的线条遮住了,我目前正在弄清楚要使用什么布局。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);
}
}发布于 2014-12-06 06:38:50
我不得不修改你的代码来编译它。我做了几个更改,为您的继续发展提供了一个起点。
这是我版本的Go框架的样子。

所以,以下是我所做的更改。
首先,必须始终通过调用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棋盘的类。你将不得不在外面留出一些空间,这样一块完整的石头就会画出1,1个点。
我很大程度上保留了你的代码的原样,除非有人输入了石块坐标,否则你不能绘制石块。
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);
}
}
}https://stackoverflow.com/questions/27132206
复制相似问题