我有一个程序,有人输入宽度和长度,并在对话框中绘制相应的大小。
以下是发生的事情。我启动了,
然后我按下去:
对话没有任何结果。
所以这是事件处理代码:
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();
}
}
答案 0 :(得分:2)
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
代替文字字段。需要数字。答案 1 :(得分:0)
您必须在g.repaint()
g2d.fillRect(x,y,w,h)
进行操作