嗨,我想通过放一个按钮来改变我的方块的大小,让它的名字加上它,例如,如果我的方块是1cm * 1cm加上按钮使其为10cm * 10cm我做了一些单位()测试它是否有效,但它没有说它在AddSquare中的那些变量在Compenent中不公开这里的代码让你看看我现在拥有的东西:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class Carre extends JFrame implements ActionListener {
JButton boutPlus, boutMoins, boutCouleur;
Squares squares;
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
}
public Carre() {
super("Carre");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
squares = new Squares();
getContentPane().add(squares);
//for (int i = 0; i < 1; i++) {
squares.addSquare(10, 10, 100, 100);
Insets insets = getInsets();
System.out.println("insets.left = " + insets.left);
System.out.println("insets.right = " + insets.right);
System.out.println("insets.top = " + insets.top);
System.out.println("insets.bottom = " + insets.bottom);
pack();
setLocationRelativeTo(null);
setVisible(true);
JPanel simplePanel = new JPanel();
simplePanel.setLayout(null);
add(simplePanel);
boutPlus = new JButton("PLUS");
boutPlus.setForeground(Color.white);
boutPlus.setBackground(new Color(63, 107, 220));
simplePanel.add(boutPlus);
boutPlus.setBounds(325, 50, 200, 80);
boutPlus.addActionListener(this);
boutMoins = new JButton("MOINS");
boutMoins.setForeground(Color.white);
boutMoins.setBackground(new Color(145, 110, 220));
simplePanel.add(boutMoins);
boutMoins.setBounds(325, 150, 200, 80);
boutMoins.addActionListener(this);
boutCouleur = new JButton("COULEUR");
boutCouleur.setForeground(Color.white);
boutCouleur.setBackground(new Color(150, 200, 80));
simplePanel.add(boutCouleur);
boutCouleur.setBounds(325, 250, 200, 80);
boutCouleur.addActionListener(this);
}
public static void main(String[] args) {
new Carre();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == boutPlus) {
squares.augmenter();
}
if (e.getSource() == boutMoins && squares.diminuer()< Double.MIN_VALUE ) { throw new HorsLimitException ();
squares.diminuer();
}
if (e.getSource() == boutCouleur) {
Graphics g=null;
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.YELLOW);
g2.fillRect(10,20,100,150);
}
}
}
class Squares extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
private List<Rectangle> squares = new ArrayList<Rectangle>();
public void addSquare(int x, int y, int width, int height) {
Rectangle rect = new Rectangle(x, y, width, height);
squares.add(rect);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (Rectangle rect : squares) {
g2.draw(rect);
}
}
public void diminuer() {
int x = 0;
squares.addSquare(x - 10);
}
public void augmenter() {
int x = 0;
squares.addSquare(x - 10);
}
}
class HorsLimitException extends ArithmeticException {
public HorsLimitException () { //constructeur par défaut
super( "Tentative de d\u00e9passement de limite" );
}
public HorsLimitException ( String message ) {
super( message );
}
}
您没有将足够的参数传递给addSquare()方法,并且您尝试使用不存在的ArrayList来调用它。方法addSquare()仅适用于Squares类。
public void diminuer() {
int x = 0;
squares.addSquare(x - 10); //too few parameters for your method
}
addSquare()方法有4个参数
public void addSquare(int x, int y, int width, int height)
您应该抓住添加到列表中的最后一个方块,并根据这些尺寸创建新方块。但是没有给出Rectangle类,所以我不知道如何检索成员变量,但是你可以为diminuer()方法执行类似的操作
public void diminuer() {
if(squares.size() > 0)
{
Rectangle obj = squares.get(squares.size() - 1);
addSquare(obj.x * 10, obj.y * 10, obj.width * 10, obj.height * 10);
}
}