java小程序实例代码 java小程序例子

java一个简单小程序,求高手帮忙编写,万分感谢class Ball {
public void play() {
System.out.println("玩球儿...");
}
}
class Football extends Ball {
public void play() {
System.out.println("使用足球运动");
}
}
class Basketball extends Ball {
public void play() {
System.out.println("使用篮球运动");
}
}
public class TestMain {
public static void main(String[] args) {
TestMain tm = new TestMain();
tm.testPlay();
}
public void testPlay() {
Ball ball = new Football();
ball.play();
ball = new Basketball();
ball.play();
}
}
/*
D:\javac TestMain.java
D:\java TestMain
使用足球运动
使用篮球运动
*/
求java经典小程序代码代码如下java小程序实例代码:
public class HelloWorld {
public static void main(String []args) {
int a = 3, b = 7 ;
System.out.println("Hello World!");
}
public static int f(int a, int b){
return a*a + a*b + b*b;
}
}
结果如下java小程序实例代码:
如何用JAVA语言编写计算器小程序?具体代码如下java小程序实例代码:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculatorextends JFrame implements ActionListener{
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;
public Calculator() {
//对图形组件实例化
jf=new JFrame("任静java小程序实例代码的计算器1.0java小程序实例代码: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=0;iallButtons.length;i++){
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=0;i16;i++){
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=0;iallButtons.length;i++){
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=0;i16;i++){
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();

推荐阅读