跪求大佬解答一道Java题!!!
跪求大佬解答一道Java题!!!编写一个Java小程序,程序界面如下图。要求在文本框中输入欲绘制的图形,若输入“线”,则在下部画5条直线;若输入“圆”,则在下部画5个圆;...
跪求大佬解答一道Java题!!!编写一个Java小程序,程序界面如下图。要求在文本框中输入欲绘制的图形,若输入“线”,则在下部画5条直线;
若输入“圆”,则在下部画5个圆;若输入“矩形”,则在下部画5个矩形; 展开
若输入“圆”,则在下部画5个圆;若输入“矩形”,则在下部画5个矩形; 展开
展开全部
Java程序如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
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 Main extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JLabel lblPrompt = null;
JTextField txtType = null;
JButton btnPaint = null;
JPanel pnlTop = null;
JPanel pnlCanvas = null;
String type = null;
public static void main(String[] args){
new Main();
}
public Main() {
super("绘制图形");
initComponent();
}
public void initComponent() {
lblPrompt = new JLabel("输入欲绘制的图形");
txtType = new JTextField(10);
btnPaint = new JButton("绘制");
pnlTop = new JPanel();
pnlCanvas = new JPanel();
pnlCanvas.setBackground(Color.WHITE);
BorderLayout layout_Main = new BorderLayout();
FlowLayout layout_Top = new FlowLayout();
this.setLayout(layout_Main);
pnlTop.setLayout(layout_Top);
pnlTop.add(lblPrompt);
pnlTop.add(txtType);
pnlTop.add(btnPaint);
this.add("North", pnlTop);
this.add("Center", pnlCanvas);
this.setSize(800, 800);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnPaint.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
JButton btn = (JButton) arg0.getSource();
if(btn == btnPaint) {
String type = null;
type = txtType.getText().trim();
Graphics g = this.getGraphics();
if(type.equals("线")) {
g.drawLine(100, 200, 700, 200);
g.drawLine(100, 300, 700, 300);
g.drawLine(100, 400, 700, 400);
g.drawLine(100, 500, 700, 500);
g.drawLine(100, 600, 700, 600);
}
else if(type.equals("圆")) {
g.drawOval(100, 100, 100, 100);
g.drawOval(200, 200, 100, 100);
g.drawOval(300, 300, 100, 100);
g.drawOval(400, 400, 100, 100);
g.drawOval(500, 500, 100, 100);
}
else if(type.equals("矩形")) {
g.drawRect(100, 200, 300, 100);
g.drawRect(150, 320, 300, 100);
g.drawRect(200, 440, 300, 100);
g.drawRect(250, 560, 300, 100);
g.drawRect(300, 680, 300, 100);
}
}
}
}
运行测试:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询