Swing概述
Swing典型组件
JFrame
Swing按钮、菜单和工具条
Swing标准对话框
表格和树
定时器
JFrame实现了java.swing.WindowConstants接口
Eg:
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestJFrame {
public static void main(String[] args) {
JFrame jf = new JFrame( "JFrame");
JButton b1 = new JButton( "Start");
JButton b2 = new JButton( "Stop");
jf.add(b1);
jf.add(b2);
jf.setBackground(Color.blue);
jf.setLayout( new FlowLayout(FlowLayout.CENTER,20,20));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认的退出方式
jf.setLocation(400, 300);
jf.setSize(300, 200);
jf.setVisible( true);
// jf.dispose();
}
}
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class TestTime {
public static void main(String[] args) {
JFrame f = new JFrame( "Time");
Button b1 = new Button( "Start");
Button b2 = new Button( "Stop");
JLabel click = new JLabel( "显示日期");
f.add(b1);
f.add(b2);
f.add(click, "North");
click.setOpaque( true);
click.setDefaultLocale( null);
click.setBackground(Color.LIGHT_GRAY);
f.setSize(400, 400);
b1.setBackground(Color.green);
b2.setBackground(Color.cyan);
f.setLocation(400, 300);
f.setBackground(Color.blue);
f.setLayout( new FlowLayout(FlowLayout.CENTER,20,20));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认的退出方式
f.setVisible( true);
MyListener m = new MyListener(click);
Timer t = new Timer(1000,m);
t.restart();
}
}
class MyListener implements ActionListener{
private JLabel click;
public MyListener(JLabel click) {
this.click = click;
}
@Override
public void actionPerformed(ActionEvent e) {
Date d = new Date();
Calendar c = new GregorianCalendar();
String time = c.get(Calendar.YEAR)+ "-"+c.get(Calendar.MONTH)+ "-"+c.get(Calendar.DATE);
int h = c.get(Calendar.HOUR_OF_DAY);
int m = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);
time += "\n time:"+h + ": "+m + ": "+s;
click.setText(time);
System.out.println(d);
click.repaint();
}
}
Swing典型组件
JFrame
Swing按钮、菜单和工具条
Swing标准对话框
表格和树
定时器
JFrame实现了java.swing.WindowConstants接口
Eg:
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestJFrame {
public static void main(String[] args) {
JFrame jf = new JFrame( "JFrame");
JButton b1 = new JButton( "Start");
JButton b2 = new JButton( "Stop");
jf.add(b1);
jf.add(b2);
jf.setBackground(Color.blue);
jf.setLayout( new FlowLayout(FlowLayout.CENTER,20,20));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认的退出方式
jf.setLocation(400, 300);
jf.setSize(300, 200);
jf.setVisible( true);
// jf.dispose();
}
}
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class TestTime {
public static void main(String[] args) {
JFrame f = new JFrame( "Time");
Button b1 = new Button( "Start");
Button b2 = new Button( "Stop");
JLabel click = new JLabel( "显示日期");
f.add(b1);
f.add(b2);
f.add(click, "North");
click.setOpaque( true);
click.setDefaultLocale( null);
click.setBackground(Color.LIGHT_GRAY);
f.setSize(400, 400);
b1.setBackground(Color.green);
b2.setBackground(Color.cyan);
f.setLocation(400, 300);
f.setBackground(Color.blue);
f.setLayout( new FlowLayout(FlowLayout.CENTER,20,20));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认的退出方式
f.setVisible( true);
MyListener m = new MyListener(click);
Timer t = new Timer(1000,m);
t.restart();
}
}
class MyListener implements ActionListener{
private JLabel click;
public MyListener(JLabel click) {
this.click = click;
}
@Override
public void actionPerformed(ActionEvent e) {
Date d = new Date();
Calendar c = new GregorianCalendar();
String time = c.get(Calendar.YEAR)+ "-"+c.get(Calendar.MONTH)+ "-"+c.get(Calendar.DATE);
int h = c.get(Calendar.HOUR_OF_DAY);
int m = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);
time += "\n time:"+h + ": "+m + ": "+s;
click.setText(time);
System.out.println(d);
click.repaint();
}
}
Swing概述
Swing典型组件
JFrame
Swing按钮、菜单和工具条
Swing标准对话框
表格和树
定时器
JFrame实现了java.swing.WindowConstants接口
Eg:
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestJFrame {
public static void main(String[] args) {
JFrame jf = new JFrame( "JFrame");
JButton b1 = new JButton( "Start");
JButton b2 = new JButton( "Stop");
jf.add(b1);
jf.add(b2);
jf.setBackground(Color.blue);
jf.setLayout( new FlowLayout(FlowLayout.CENTER,20,20));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认的退出方式
jf.setLocation(400, 300);
jf.setSize(300, 200);
jf.setVisible( true);
// jf.dispose();
}
}
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class TestTime {
public static void main(String[] args) {
JFrame f = new JFrame( "Time");
Button b1 = new Button( "Start");
Button b2 = new Button( "Stop");
JLabel click = new JLabel( "显示日期");
f.add(b1);
f.add(b2);
f.add(click, "North");
click.setOpaque( true);
click.setDefaultLocale( null);
click.setBackground(Color.LIGHT_GRAY);
f.setSize(400, 400);
b1.setBackground(Color.green);
b2.setBackground(Color.cyan);
f.setLocation(400, 300);
f.setBackground(Color.blue);
f.setLayout( new FlowLayout(FlowLayout.CENTER,20,20));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认的退出方式
f.setVisible( true);
MyListener m = new MyListener(click);
Timer t = new Timer(1000,m);
t.restart();
}
}
class MyListener implements ActionListener{
private JLabel click;
public MyListener(JLabel click) {
this.click = click;
}
@Override
public void actionPerformed(ActionEvent e) {
Date d = new Date();
Calendar c = new GregorianCalendar();
String time = c.get(Calendar.YEAR)+ "-"+c.get(Calendar.MONTH)+ "-"+c.get(Calendar.DATE);
int h = c.get(Calendar.HOUR_OF_DAY);
int m = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);
time += "\n time:"+h + ": "+m + ": "+s;
click.setText(time);
System.out.println(d);
click.repaint();
}
}
Swing典型组件
JFrame
Swing按钮、菜单和工具条
Swing标准对话框
表格和树
定时器
JFrame实现了java.swing.WindowConstants接口
Eg:
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestJFrame {
public static void main(String[] args) {
JFrame jf = new JFrame( "JFrame");
JButton b1 = new JButton( "Start");
JButton b2 = new JButton( "Stop");
jf.add(b1);
jf.add(b2);
jf.setBackground(Color.blue);
jf.setLayout( new FlowLayout(FlowLayout.CENTER,20,20));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认的退出方式
jf.setLocation(400, 300);
jf.setSize(300, 200);
jf.setVisible( true);
// jf.dispose();
}
}
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class TestTime {
public static void main(String[] args) {
JFrame f = new JFrame( "Time");
Button b1 = new Button( "Start");
Button b2 = new Button( "Stop");
JLabel click = new JLabel( "显示日期");
f.add(b1);
f.add(b2);
f.add(click, "North");
click.setOpaque( true);
click.setDefaultLocale( null);
click.setBackground(Color.LIGHT_GRAY);
f.setSize(400, 400);
b1.setBackground(Color.green);
b2.setBackground(Color.cyan);
f.setLocation(400, 300);
f.setBackground(Color.blue);
f.setLayout( new FlowLayout(FlowLayout.CENTER,20,20));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认的退出方式
f.setVisible( true);
MyListener m = new MyListener(click);
Timer t = new Timer(1000,m);
t.restart();
}
}
class MyListener implements ActionListener{
private JLabel click;
public MyListener(JLabel click) {
this.click = click;
}
@Override
public void actionPerformed(ActionEvent e) {
Date d = new Date();
Calendar c = new GregorianCalendar();
String time = c.get(Calendar.YEAR)+ "-"+c.get(Calendar.MONTH)+ "-"+c.get(Calendar.DATE);
int h = c.get(Calendar.HOUR_OF_DAY);
int m = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);
time += "\n time:"+h + ": "+m + ": "+s;
click.setText(time);
System.out.println(d);
click.repaint();
}
}