import java.awt.*
import java.awt.event.*
import java.awt.geom.*
import javax.swing.*
//不规则图形的绘制
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath()//GeneralPath对象实例
Point aPoint
//构造函数
public IrregularShapeDemo() {
super("不规则图形的绘制")//调用父类构造函数
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK)//允许事件
setSize(300, 200)//设置窗口尺寸
setVisible(true)//设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//关闭窗口时退出程序
}
public void paint(Graphics g) { //重载窗口组件的paint()方法
Graphics2D g2D = (Graphics2D)g//获取图形环境
g2D.draw(gPath)//绘制路径
}
public static void main(String[] args) {
new IrregularShapeDemo()
}
protected void processMouseEvent(MouseEvent e) { //鼠标事件处理
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
aPoint = e.getPoint()//得到当前鼠标点
gPath = new GeneralPath()//重新实例化GeneralPath对象
gPath.moveTo(aPoint.x,aPoint.y)//设置路径点
}
}
protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint()//得到当前鼠标点
gPath.lineTo(aPoint.x, aPoint.y)//设置路径
gPath.moveTo(aPoint.x, aPoint.y)
repaint()//重绘组件
}
}
}
具体代码如下:
import javax.swing.*
import java.awt.event.*
import java.awt.*
public class Calculator extends JFrame implements ActionListener {
private JFrame jf
private JButton[] allButtons
private JButton clearButton
private JTextField jtf
public Calculator() {
//对图形组件实例化
jf=new JFrame("任静的计算器1.0:JAVA版")
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0)
}
})
allButtons=new JButton[16]
clearButton=new JButton("清除")
jtf=new JTextField(25)
jtf.setEditable(false)
String str="123+456-789*0.=/"
for(int i=0i<allButtons.lengthi++){
allButtons[i]=new JButton(str.substring(i,i+1))
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout())
JPanel northPanel=new JPanel()
JPanel centerPanel=new JPanel()
JPanel southPanel=new JPanel()
northPanel.setLayout(new FlowLayout())
centerPanel.setLayout(new GridLayout(4,4))
southPanel.setLayout(new FlowLayout())
northPanel.add(jtf)
for(int i=0i<16i++){
centerPanel.add(allButtons[i])
}
southPanel.add(clearButton)
jf.add(northPanel,BorderLayout.NORTH)
jf.add(centerPanel,BorderLayout.CENTER)
jf.add(southPanel,BorderLayout.SOUTH)
addEventHandler()
}
//添加事件监听
public void addEventHandler(){
jtf.addActionListener(this)
for(int i=0i<allButtons.lengthi++){
allButtons[i].addActionListener(this)
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("")
}
})
}
//事件处理
public void actionPerformed(ActionEvent e) {
//在这里完成事件处理 使计算器可以运行
String action=e.getActionCommand()
if(action=="+"||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋体",Font.BOLD,24)
jtf.setFont(f)
jtf.setBackground(new Color(0x8f,0xa0,0xfb))
for(int i=0i<16i++){
allButtons[i].setFont(f)
allButtons[i].setForeground(Color.RED)
}
}
public void showMe(){
init()
setFontAndColor()
jf.pack()
jf.setVisible(true)
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
public static void main(String[] args){
new Calculator().showMe()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)