[英]Draw a rectangle with the corresponding size taken from a TextField
我有一個程序,有人在其中輸入寬度和長度,並在對話框上繪制了相應的大小。
這是發生了什么。 我開始
然后我按go:
對話毫無結果。
所以這是事件處理代碼:
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);
}
}
我可以將h
和w
的值更改為文本字段中的值,然后更新圖形嗎?
這是一個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);
}
}
pack()
。 JSpinner
而不是文本字段。 需要數字。 您必須在g2d.fillRect(x,y,w,h)
之后執行g.repaint()
g2d.fillRect(x,y,w,h)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.