java经典小程序代码 java小程序源代码( 四 )


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();
}
}
想设计一个java小程序,在文本框输入一个数,然后每一秒+1,求给完整代码package com.demo;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TimerTest {
private Timer timer;
private JTextField field;
private JButton button;
private boolean flag = true;
public TimerTest() {
timer = new Timer();
addview();
}
private void addview() {
JFrame frame = new JFrame("Timer test");
field = new JTextField();
field.setPreferredSize(new Dimension(0, 30));
button = new JButton("start");
button.setPreferredSize(new Dimension(100, 30));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (flag) {
auto();
flag = false;
button.setText("stop");
} else {
timer.cancel();
flag = true;
button.setText("start");
}
}
});
frame.add(field, BorderLayout.CENTER);
frame.add(button, BorderLayout.EAST);
frame.setBounds(200, 200, 300, 60);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TimerTest();
}
private void auto() {
timer.schedule(new TimerTask() {
@Override
public void run() {
int num = Integer.parseInt(field.getText().trim());
num += 1;
field.setText(num + "");
auto();
}
}, 1000);
}
}
求java经典小程序代码代码如下:
public class HelloWorld {
public static void main(String []args) {
int a = 3, b = 7 ;
System.out.println("Hello World!");
}

推荐阅读