gpt4 book ai didi

java - 一个简单的 Java 程序 - 将形状添加到面板时遇到问题

转载 作者:行者123 更新时间:2023-11-30 07:46:28 25 4
gpt4 key购买 nike

我是 Java 语言和 Stack Overflow 的新手。我想向面板添加形状以模仿 map 。标题中解释了问题 - 我不知道如何将形状添加到我的面板中。请注意,我已经完成了该程序的一半,这意味着它尚未具备我想要的所有功能。我有两个用于该程序的类 - Map.javaMapShapes.java

我的第一个类。

/** Program that maps a room in the house and sees how things will look from an overhead, 2d perspective. ;
* @author: James ;
*
* */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Map extends JPanel{
MapShapes[] shapeArray = new MapShapes[20];
int count = 0;
private JLabel askLength, askWidth, askX, askY;
private JTextField length, width, x, y;
private JButton addButton;
private DrawingPanel drawPanel = new DrawingPanel();

public static void main(String[] args){
JFrame frame = new JFrame("Map");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Map panel = new Map();
frame.getContentPane().add(panel);

frame.pack();
frame.setVisible(true);
}// end of main;

public Map(){
JPanel controlPanel = new JPanel();

addButton = new JButton("Add a shape");

askX = new JLabel("X coordinate: ");
askY = new JLabel("Y coordinate: ");
askLength = new JLabel("Length: ");
askWidth = new JLabel("Width: ");

x = new JTextField(3);
y = new JTextField(3);
length = new JTextField(3);
width = new JTextField(3);

length.addActionListener(new Listener());
width.addActionListener(new Listener());
x.addActionListener(new Listener());
y.addActionListener(new Listener());

controlPanel.add(askX);
controlPanel.add(x);
controlPanel.add(askY);
controlPanel.add(y);
controlPanel.add(askLength);
controlPanel.add(length);
controlPanel.add(askWidth);
controlPanel.add(width);
controlPanel.add(addButton);
controlPanel.setPreferredSize(new Dimension (100, 400));

add(controlPanel);
add(drawPanel);

}// end of constructor;

private class DrawingPanel extends JPanel{

/** This public DrawingPanel() constructor.* */
public DrawingPanel(){

setPreferredSize(new Dimension(400, 400));
setBackground(Color.pink);

} // End of DrawingPanel() method.

/** This public void paintComponent(Graphics g) method* */
public void paintComponent(Graphics g){
super.paintComponent(g);

for(int index = 0; index<count; index++)
shapeArray[index].display(g);

} // End of paintComponent(Graphics g) method.

}// End of private class;

/** This class allows actions to be added to the buttons and text fields;*/
private class Listener implements ActionListener{

public void actionPerformed(ActionEvent event){
int lengthVal, widthVal, yVal, xVal;

JButton button = (JButton) event.getSource () ;

String textX = x.getText();
String textY = y.getText();
String textLength = length.getText();
String textWidth = width.getText();

xVal = Integer.parseInt(textX);
yVal = Integer.parseInt(textY);
lengthVal = Integer.parseInt(textLength);
widthVal = Integer.parseInt(textWidth);

if(button.getText().equals("Add a shape")){
if(count<shapeArray.length){
shapeArray[count] = new MapShapes(lengthVal, widthVal, xVal, yVal);
count++;
} // end of nested if block;

} // End of if block;

repaint();

}// end of method;

}// end of private class;

}// end of class;

我的第二堂课。

/**  This class makes the rectangles for the Map program. ;
* @author: James ;
*/

import java.util.*; // So that we can use the Random class.
import java.awt.*; // So that we can use the Color class and the Graphics class.

/** This class makes rectangles.*/
public class MapShapes{

private int x;
private int y;
private int length;
private int width;
private Color colour;

public MapShapes(int length, int width, int x, int y){
this.length = length;
this.width = width;
this.x = x;
this.y = y;
this.colour = new Color(randInt(0,254), randInt(0,254), randInt(0,254));
}

/** This randInt(int lowest, int highest) method:
* Returns a random value within particular limits to instantiate the colour data field.
* */
public int randInt( int lowest, int highest){
Random generator = new Random();
int randomNum = generator.nextInt(highest - lowest) + lowest;
return randomNum;
}

public void display(Graphics j){
j.setColor(colour);
j.fillRect(x, y, length, width);
}
}// end of class;

最佳答案

您需要将一个 ActionListener 添加到您的 addButton,当触发时,它会从其他字段收集所需的信息并将其添加到 shapeArray并在屏幕上可见的 DrawingPanel 实例上调用 repaint

参见How to Write an Action ListenersHow to Use Buttons, Check Boxes, and Radio Buttons了解更多详情

关于java - 一个简单的 Java 程序 - 将形状添加到面板时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33863769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com