java - 使用从 TextField 获取的相应大小绘制一个矩形

标签 java swing paintcomponent rectangles

我有一个程序,有人输入宽度和长度,然后在对话框上绘制相应的尺寸。

场景

这就是发生的事情。我启动它,

Length and width

然后我按“开始”:

Dialogue

对话没有任何内容。

代码

这是事件处理代码:

public void actionPerformed(ActionEvent e){
d.init();
}

d 是显示对话的类。我没想到我会显示它,但 init 所做的只是向其中添加一个 DrawRectangle 面板(这是 DrawRectangle` 类):

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

public class DrawRectangle extends JPanel{
   int x = 100;
    int y = 50;
    int h;
    int w ;

    private void Dodrawing(Graphics g, int w, int h, int x, int y){
        g2d.fillRect(x, y, w, h);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        //Graphics, width, heigth, x coordinate, y coordinate
        Dodrawing(g, w, h, x, y);
    }
}

问题

我可以将 hw 的值更改为文本字段中的值,然后更新绘图吗?

编辑

这是一个 SSCCE:

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Area;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SSCCE extends JFrame implements ActionListener{

    //Variable declaration
    JLabel LengthLabel = new JLabel("Length");
    JLabel WidthLabel = new JLabel("Width");
    JLabel Area = new JLabel ();
    JLabel Perimeter = new JLabel ();
    JLabel Volume = new JLabel();
    JTextField Length = new JTextField();
    JTextField Width = new JTextField();
    int LengthInt;
    int WidthInt;

    String LengthStr;
    String WidthStr;

    JDialog dialog;  

Color darkGreen = new Color(50, 150, 50);
JButton close = new JButton("Close");
boolean visi = true;
    JButton go = new JButton("Go");

  public SSCCE(){
      super("Geometry");
   setSize(500, 600);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      GridLayout grid = new GridLayout(20 , 10);

      setLayout(grid);

      add(LengthLabel); 
      add(Length);
      add(WidthLabel);
      add(Width); 
      add(go);
      add(Area);
      add(Perimeter);
      go.addActionListener(this);
     setVisible(true);   
}
  JPanel p = new JPanel();
   int x = 100;
    int y = 50;
    int h ;
    int w ;

    private void Dodrawing(Graphics g, int w, int h, int x, int y){
Graphics2D g2d = (Graphics2D) g;
        g2d.fillRect(x, y, w, h);
    }

    protected void paintComponent(Graphics g)
    {
        p.paintComponents(g);

        //Graphics, width, heigth, x coordinate, y coordinate
        Dodrawing(g, w, h, x, y);
    }

  //Action Peformed method
   public void actionPerformed(ActionEvent e){
       //Getting the text from the input fields
        LengthStr = Length.getText().toString();
       WidthStr = Width.getText().toString();
try{
LengthInt = Integer.parseInt(LengthStr);
WidthInt = Integer.parseInt(WidthStr);
init();
}catch(Exception event){
System.out.println(event);
}    
  }

protected void init() {  

dialog = new JDialog(this, "Copie", true);
dialog.setResizable(false);  

dialog.add(p);  

dialog.pack();  
dialog.setSize(300, 200);
Dimension Size = Toolkit.getDefaultToolkit().getScreenSize();  
dialog.setLocation(new Double((Size.getWidth()/2) - (dialog.getWidth()/2)).intValue(), new Double((Size.getHeight()/2) - (dialog.getHeight()/2)).intValue());  

dialog.setVisible(visi);
}  

protected void close() {  
this.dialog.dispose();  
this.dialog.setVisible(false);  
}  
     public static void main(String[] args){
         SSCCE ge = new SSCCE();
     }
}

最佳答案

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

public class SSCCE extends JFrame implements ActionListener {

    //Variable declaration
    JLabel LengthLabel = new JLabel("Length");
    JLabel WidthLabel = new JLabel("Width");
    JLabel Area = new JLabel();
    JLabel Perimeter = new JLabel();
    JLabel Volume = new JLabel();
    JTextField Length = new JTextField();
    JTextField Width = new JTextField();
    int LengthInt;
    int WidthInt;
    String LengthStr;
    String WidthStr;
    JDialog dialog;
    Color darkGreen = new Color(50, 150, 50);
    JButton close = new JButton("Close");
    boolean visi = true;
    JButton go = new JButton("Go");

    public SSCCE() {
        super("Geometry");
        setSize(500, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GridLayout grid = new GridLayout(20, 10);

        setLayout(grid);

        add(LengthLabel);
        add(Length);
        add(WidthLabel);
        add(Width);
        add(go);
        add(Area);
        add(Perimeter);
        go.addActionListener(this);
        setVisible(true);
    }
    JPanel p = new JPanel();
    int x = 100;
    int y = 50;
    int h;
    int w;

    private void Dodrawing(Graphics g, int w, int h, int x, int y) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.fillRect(x, y, w, h);
    }

    protected void paintComponent(Graphics g) {
        p.paintComponents(g);

        //Graphics, width, heigth, x coordinate, y coordinate
        Dodrawing(g, w, h, x, y);
    }

    //Action Peformed method
    public void actionPerformed(ActionEvent e) {
        //Getting the text from the input fields
        LengthStr = Length.getText().toString();
        WidthStr = Width.getText().toString();
        try {
            LengthInt = Integer.parseInt(LengthStr);
            WidthInt = Integer.parseInt(WidthStr);
            init();
        } catch (Exception event) {
            System.out.println(event);
        }
    }

    protected void init() {

        dialog = new JDialog(this, "Copie", true);
        dialog.setResizable(false);

        dialog.add(p);

        dialog.pack();
        Dimension Size = Toolkit.getDefaultToolkit().getScreenSize();
        dialog.setLocation(new Double((Size.getWidth() / 2) - (dialog.getWidth() / 2)).intValue(), new Double((Size.getHeight() / 2) - (dialog.getHeight() / 2)).intValue());
        dialog.setLayout(new BorderLayout());
        dialog.add(new DrawRectangle(WidthInt, LengthInt));
        dialog.pack();
        dialog.setSize(300, 200);
        dialog.setVisible(visi);
    }

    protected void close() {
        this.dialog.dispose();
        this.dialog.setVisible(false);
    }

    public static void main(String[] args) {
        SSCCE ge = new SSCCE();
    }
}

class DrawRectangle extends JPanel {

    int x = 100;
    int y = 50;
    int h = 100;
    int w = 100;

    DrawRectangle(int w, int h) {
        this.w = w;
        this.h = h;
    }

    private void Dodrawing(Graphics g, int w, int h, int x, int y) {
        g.setColor(Color.RED);
        g.fillRect(x, y, w, h);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.println("paintComponent");
        //Graphics, width, heigth, x coordinate, y coordinate
        Dodrawing(g, w, h, x, y);
    }
}

其他提示

  1. 对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。
  2. 源代码中的一个空白行总是就足够了。
  3. 请学习常用Java naming conventions (特别是名称的大小写)用于类、方法和属性名称并一致地使用它们。
  4. 不要设置顶级容器的大小。相反,布局内容并调用 pack()
  5. Java GUI 应在 EDT 上启动和更新。
  6. 在应用程序中使用 JSpinner 而不是文本字段。需要数字。

关于java - 使用从 TextField 获取的相应大小绘制一个矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17789873/

相关文章:

java - 颜色标题不起作用

java - 当 JCheckBox 由表模型生成时初始化它们

java - 使用 Graphics2D 绘制 JPanel 程序在调整窗口大小时消失

java - 谁能帮我使用 StartingClass 中的 loadMap() 方法加载 map (文本文件)?

java - 函数内部的paintComponent

java - 使用 Swing Java 进行绘画

java - 如何查看 JTable 中的列标签?

java - 如何在 MacOS 上使用 Java(使用 JNA)获取前景窗口/进程?

java - 多线程程序中的Swing图形

java - 有没有办法重写 MenuItemActionPerformed 中的 PaintComponent 方法?