java编写窗体程序,实现在窗体的画布上根据用户的输入数据 画一个矩形。要求: a) 运行效果如下图
b)运行程序时,输入长度和宽度后当点击“确定”按钮时,在画布上绘制一个指定长度和宽度的矩形;用java语音哦...
b) 运行程序时,输入长度和宽度后当点击“确定”按钮时,在画
布上绘制一个指定长度和宽度的矩形;
用java语音哦 展开
布上绘制一个指定长度和宽度的矩形;
用java语音哦 展开
展开全部
package com.paint;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Paint extends JFrame {
private JPanel panel1 = null;
private Panel panel2 = null;
private JPanel panel3 = null;
private JButton button = null;
private JLabel label1 = null;
private JLabel label2 = null;
private JLabel label3 = null;
private JLabel label4 = null;
private JTextField editField1 = null;
private JTextField editField2 = null;
private JTextField editField3 = null;
private JTextField editField4 = null;
public Paint(){
panel1 = new JPanel();
panel2 = new Panel();
panel3 = new JPanel();
button = new JButton("确定");
label1 = new JLabel("坐标x:");
label2 = new JLabel("坐标y:");
label3 = new JLabel("长度:");
label4 = new JLabel("宽度:");
editField1 = new JTextField(5);
editField2 = new JTextField(5);
editField3 = new JTextField(5);
editField4 = new JTextField(5);
panel1.setPreferredSize(new Dimension(250, 100));
panel1.add(label1);
panel1.add(editField1);
panel1.add(label2);
panel1.add(editField2);
panel1.add(label3);
panel1.add(editField3);
panel1.add(label4);
panel1.add(editField4);
panel3.setPreferredSize(new Dimension(250, 450));
panel3.add(panel1);
panel3.add(panel2);
panel3.add(button);
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
panel2.setX(Integer.parseInt(editField1.getText().toString()));
panel2.setY(Integer.parseInt(editField2.getText().toString()));
panel2.setWidth(Integer.parseInt(editField3.getText().toString()));
panel2.setHeight(Integer.parseInt(editField4.getText().toString()));
panel2.repaint();
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(panel3);
this.pack();
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Paint paint = new Paint();
}
class Panel extends JPanel{
private int x = 0;
private int y = 0;
private int width = 0;
private int height = 0;
public Panel(){
this.setBackground(Color.blue);
this.setPreferredSize(new Dimension(250, 300));
}
public void paint(Graphics g){
super.paintComponent(g);
g.setColor(Color.black);
g.drawRect(x, y, width, height);
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Paint extends JFrame {
private JPanel panel1 = null;
private Panel panel2 = null;
private JPanel panel3 = null;
private JButton button = null;
private JLabel label1 = null;
private JLabel label2 = null;
private JLabel label3 = null;
private JLabel label4 = null;
private JTextField editField1 = null;
private JTextField editField2 = null;
private JTextField editField3 = null;
private JTextField editField4 = null;
public Paint(){
panel1 = new JPanel();
panel2 = new Panel();
panel3 = new JPanel();
button = new JButton("确定");
label1 = new JLabel("坐标x:");
label2 = new JLabel("坐标y:");
label3 = new JLabel("长度:");
label4 = new JLabel("宽度:");
editField1 = new JTextField(5);
editField2 = new JTextField(5);
editField3 = new JTextField(5);
editField4 = new JTextField(5);
panel1.setPreferredSize(new Dimension(250, 100));
panel1.add(label1);
panel1.add(editField1);
panel1.add(label2);
panel1.add(editField2);
panel1.add(label3);
panel1.add(editField3);
panel1.add(label4);
panel1.add(editField4);
panel3.setPreferredSize(new Dimension(250, 450));
panel3.add(panel1);
panel3.add(panel2);
panel3.add(button);
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
panel2.setX(Integer.parseInt(editField1.getText().toString()));
panel2.setY(Integer.parseInt(editField2.getText().toString()));
panel2.setWidth(Integer.parseInt(editField3.getText().toString()));
panel2.setHeight(Integer.parseInt(editField4.getText().toString()));
panel2.repaint();
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(panel3);
this.pack();
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Paint paint = new Paint();
}
class Panel extends JPanel{
private int x = 0;
private int y = 0;
private int width = 0;
private int height = 0;
public Panel(){
this.setBackground(Color.blue);
this.setPreferredSize(new Dimension(250, 300));
}
public void paint(Graphics g){
super.paintComponent(g);
g.setColor(Color.black);
g.drawRect(x, y, width, height);
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询