java 按一个按钮改变正方形的大小
java change size of square by pressing a button
你好,我想通过放置一个按钮来改变我的正方形的大小,让我们将其命名为加号,它会添加一个单位,例如,如果我的正方形是 1cm*1cm,那么加号按钮会使它变成 10cm*10cm I做了 diminuer() 来测试它是否有效,但它不是说 addSquare 中的那些变量不是 Compenent 中的 public 这里是供您查看我现在拥有的代码:
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 Class.
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 Class 没有给出所以我不知道如何检索成员变量但是你可以为 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);
}
}
你好,我想通过放置一个按钮来改变我的正方形的大小,让我们将其命名为加号,它会添加一个单位,例如,如果我的正方形是 1cm*1cm,那么加号按钮会使它变成 10cm*10cm I做了 diminuer() 来测试它是否有效,但它不是说 addSquare 中的那些变量不是 Compenent 中的 public 这里是供您查看我现在拥有的代码:
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 Class.
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 Class 没有给出所以我不知道如何检索成员变量但是你可以为 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);
}
}