1.ç¼åä¸ä¸ªJava GUI
ç¼åä¸ä¸ªJava GUI
è¯ä¸ä¸ä¸é¢ç代ç
ï¼å¦æç¹å»æé®å没æä»»ä½ååï¼å°çªå£æå°åä¸ä¸å°±æäºï¼
没æåºç°è¿ä¸ªé®é¢çè¯ï¼ä¹è¯·åè¯æä¸ä¸~
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class painting extends JFrame implements ActionListener{
private JButton round,rectangle,ellipse,beeline;
private JLabel xaxis,yaxis,remain,information;
private JTextField xTF,yTF;
private BorderLayout layout;
private Container cp;
private JPanel pCenter;
Vector<Object> v=new Vector<Object>(); //å®ä¹ä¸ä¸ªéåç±»ç¨äºåå¨æé®å¯¹è±¡
public painting(){ //æé æ¹æ³ ------------------æ¡æ¶åå§å-------------------
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("painting");
setSize(,);
layout = new BorderLayout();
cp = getContentPane();
cp.setLayout(layout);
round= new JButton("ç»å");
rectangle= new JButton("ç»ç©å½¢");
ellipse= new JButton("ç»æ¤å");
beeline= new JButton("ç»ç´çº¿");
xaxis=new JLabel("xåæ ");
yaxis=new JLabel("yåæ ");
remain=new JLabel("å³ä¸è§åæ (,) ");
xTF=new JTextField("0",5);
yTF=new JTextField("0",5);
JPanel pUp= new JPanel();//第ä¸ä¸ªé¢æ¿ å¨ä¸é¨
pUp.add(remain);
pUp.add(xaxis);//置两个ææ¬æ¡
pUp.add(xTF);
pUp.add(yaxis);
pUp.add(yTF);
cp.add(pUp, "North");
//pCenter=new JPanel();//第äºä¸ªé¢æ¿ å¨ä¸é¨
//pCenter.add(information);//ç½®æ¾ç¤ºè¯´æä¸ç»å¾åº
//cp.add(pCenter,"Center");
JPanel pDown= new JPanel();//第ä¸ä¸ªé¢æ¿ å¨ä¸é¨
pDown.add(round);// ç½®å个æé®
pDown.add(rectangle);
pDown.add(ellipse);
pDown.add(beeline);
cp.add(pDown, "South");
round.addActionListener(this); //ç½®æé®çå¬--------------æé®è¡ä¸ºçå¬ä¸ååº-------------
rectangle.addActionListener(this);
ellipse.addActionListener(this);
beeline.addActionListener(this);
}
public void actionPerformed(ActionEvent e) { //çå¬ååº
v.add(e.getSource());//å°æé®æ åµåå ¥vä¸
}
public void paint(Graphics g) { //--------------ç»å¾ååº-------------
super.paint(g);
int xx=Integer.parseInt(xTF.getText());//è·åä½ç½®å¼
int yy=Integer.parseInt(yTF.getText());
int size=0;
Object o;
//while(v.size()!=size){ //å½ç¨æ·ç¹å»æé®éæ©æä¸ç§å¾å½¢æ¶ï¼vç大å°å°±ä¼æ¯sizeå¼å¤§1ï¼å½ç»å¾å®æåï¼v.sizeåçäºsizeï¼ææå°±æ¯ï¼åºç°ç¹å» å³å»å¤ç
o=v.lastElement();
if(o == round) { g.drawOval(xx,yy,,);}
else if (o == rectangle){ g.drawRect(xx,yy,,);}
else if (o == ellipse) { g.drawOval(xx,yy,,);}
else if(o == beeline) { g.drawLine(xx,yy,xx+,yy);}
size++;
}
}
public static void main(String[] args){ // ------------ç¨åºå ¥å£-------------
JFrame frame = new painting();
frame.setVisible(true);
}
}