问题描述
请求java动态小程序 滚动的小球或者图形的自动变形(圆变方,变三角),计算器,计时器之类…… 可以用 j buider 运行的 万分感谢 问题补充:我试试高级java工程师 写道
解决方案
我编了个计时器: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Timer extends JFrame implements ActionListener,Runnable { private JTextField text_timer; private JButton button_start,button_pause,button_stop; private Thread TimerThread; public int changedata(String str) { int x=(str.charAt(0)-48)*10+str.charAt(1)-48; return x; } public void addone() { String str1=text_timer.getText(),strh,strm,strs,strl; int h,m,s,l; h=changedata(str1.substring(0,2)); m=changedata(str1.substring(3,5)); s=changedata(str1.substring(6,8)); l=changedata(str1.substring(9,11))+1; if(l==100) { l=0; s=s+1; } if(s==60) { s=0; m=m+1; } if(m==60) { m=0; h=h+1; } if(h<10) { strh="0"+h; } else { strh=""+h; } if(m<10) { strm="0"+m; } else { strm=""+m; } if(s<10) { strs="0"+s; } else { strs=""+s; } if(l<10) { strl="0"+l; } else { strl=""+l; } text_timer.setText(strh+":"+strm+":"+strs+":"+strl); } public Timer() { super("计时器"); this.setSize(200,100); this.setLocation(300,240); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLayout(new GridLayout(2,1)); text_timer=new JTextField("00:00:00:00"); button_start=new JButton("start"); button_pause=new JButton("pause"); button_stop=new JButton("stop"); this.add(text_timer); this.add(button_start); this.add(button_pause); this.add(button_stop); text_timer.addActionListener(this); button_start.addActionListener(this); button_pause.addActionListener(this); button_stop.addActionListener(this); TimerThread=new Thread(this); this.setVisible(true); } public void run() { while(TimerThread.isAlive()&&!TimerThread.isInterrupted()) { try { TimerThread.sleep(10); addone(); } catch(InterruptedException e) { break; } } } public void actionPerformed(ActionEvent e) { if(e.getSource()==button_start) { button_pause.setEnabled(true); button_start.setEnabled(false); button_stop.setEnabled(true); TimerThread=new Thread(this); TimerThread.setPriority(10); TimerThread.start(); } if(e.getSource()==button_pause) { button_pause.setEnabled(false); button_start.setEnabled(true); button_stop.setEnabled(true); TimerThread.interrupt(); } if(e.getSource()==button_stop) { TimerThread.interrupt(); button_pause.setEnabled(false); button_start.setEnabled(true); button_stop.setEnabled(false); text_timer.setText("00:00:00:00"); } } public static void main(String arg[]) { new Timer(); } }
解决方案二:
滚动的小球,这个网上很多啊,随便百度一个就有了,JBUILDER只是一个开发IDE吧,只要是JAVA的多能运行 贴个滚动的小球代码//Checkers.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;//Checkers类public class Checkers extends JFrame implements ActionListener { //变量定义 CheckersPanel checkers = new CheckersPanel(); JButton startButton = new JButton("start"); JButton stopButton = new JButton("stop"); //构造函数 public Checkers(){ super("Checkers"); setSize(210,170); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel pane = new JPanel(); BorderLayout border = new BorderLayout(); pane.setLayout(border); pane.add(checkers,"Center"); JPanel buttonPanel = new JPanel(); startButton.addActionListener(this); buttonPanel.add(startButton); stopButton.addActionListener(this); stopButton.setEnabled(false); buttonPanel.add(stopButton); pane.add(buttonPanel,"South"); setContentPane(pane); show(); } //响应用户动作 public void actionPerformed(ActionEvent evt){ if(evt.getSource() == startButton){ checkers.playAnimation(); startButton.setEnabled(false); stopButton.setEnabled(true); }else{ checkers.stopAnimation(); startButton.setEnabled(true); stopButton.setEnabled(false); } } //主函数 public static void main(String[] arguments){ Checkers ck = new Checkers(); }}//CheckersPanel类class CheckersPanel extends JPanel implements Runnable{ //变量定义 private Thread runner; int xPos = 5; int xMove = 4; //播放动画 void playAnimation(){ if (runner ==null);{ runner = new Thread(this); runner.start(); } } //停止动画 void stopAnimation(){ if (runner !=null);{ runner = null; } } //运行 public void run(){ Thread thisThread = Thread.currentThread(); while(runner ==thisThread){ xPos += xMove; if ((xPos > 105)|(xPos < 5)) xMove *= -1; repaint(); try{ Thread.sleep(100); }catch(InterruptedException e){} } } //画图形 public void paintComponent(Graphics comp){ Graphics2D comp2D = (Graphics2D)comp; comp2D.setColor(Color.blue); comp2D.fillRect(0,0,100,100); comp2D.setColor(Color.white); comp2D.fillRect(100,0,100,100); comp2D.setColor(Color.black); comp2D.fillOval(xPos,5,90,90); }}
解决方案三:
白天上班,晚上还得忙。。。哎
解决方案四:
果然如此,^_^