AWT
组件和容器
Frame 窗口
新建一个frame窗口
public static void main(String[] args) {
Frame frame = new Frame("我的第一个java图像界面");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
//frame.setBackground(Color.green);
frame.setBackground(new Color(118, 194, 114));
//弹出初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);//默认true为可以改变窗口大小
}
注意:左上角的坐标为(x:0,y:0)
封装窗口
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.BLUE);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.red);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.magenta);
}
}
class MyFrame extends Frame{
static int id=0;
public MyFrame(int x,int y,int w,int h,Color c){
super("MyFrame"+(++id));
setVisible(true);
setBounds(x,y,w,h);
setBackground(c);
}
}
Panel 面板
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
frame.setBackground(Color.green);
frame.setBounds(100,100,500,500);
frame.setVisible(true);
panel.setBackground(Color.blue);
panel.setBounds(50,50,400,400);
frame.add(panel);
//监听事件 监听窗口关闭事件 System.exit(0);
//适配器模式:不用重写每一种方法
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
布局管理器
-
流式布局
public static void main(String[] args) { Frame frame = new Frame(); Button button1 = new Button("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); //设置为流式布局 //frame.setLayout(new FlowLayout()); frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); frame.add(button1); frame.add(button2); frame.add(button3); frame.setSize(400,400); frame.setVisible(true); }
-
东西南北中
public static void main(String[] args) { Frame frame = new Frame(); Button east = new Button("east"); Button west = new Button("west"); Button south = new Button("south"); Button north = new Button("north"); Button center = new Button("center"); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(south,BorderLayout.SOUTH); frame.add(north,BorderLayout.NORTH); frame.add(center,BorderLayout.CENTER); frame.setSize(200,200); frame.setVisible(true); }
-
表格布局
public static void main(String[] args) { Frame frame = new Frame(); Button btn1 = new Button("btn1"); Button btn2 = new Button("btn2"); Button btn3 = new Button("btn3"); Button btn4 = new Button("btn4"); Button btn5 = new Button("btn5"); Button btn6 = new Button("btn6"); frame.setLayout(new GridLayout(3,2)); frame.add(btn1); frame.add(btn2); frame.add(btn3); frame.add(btn4); frame.add(btn5); frame.add(btn6); frame.pack();//自动填充窗口大小 frame.setVisible(true); }
-
综合运用
打印如下窗口:
public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(200,200);
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2, 1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2, 2));
p1.add(new Button("E"),BorderLayout.EAST);
p1.add(new Button("W"),BorderLayout.WEST);
p2.add(new Button("1"));
p2.add(new Button("2"));
p1.add(p2,BorderLayout.CENTER);
p3.add(new Button("E"),BorderLayout.EAST);
p3.add(new Button("W"),BorderLayout.WEST);
p4.add(new Button("1"));
p4.add(new Button("2"));
p4.add(new Button("3"));
p4.add(new Button("4"));
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}
事件监听
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button();
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
WindowClose(frame);
}
private static void WindowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
一个类实现多个按钮的监听事件
public class TestAction2 {
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("start");
Button button2 = new Button("stop");
//可以显示地定义触发会返回的命令,如果不定义则为默认值
button1.setActionCommand("button1");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("msg:"+e.getActionCommand());
if(e.getActionCommand().equals("button1")){
}else if (e.getActionCommand().equals("stop")){
}
}
}
输入框TextField 监听
public class TestText01 {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);//继承了Frame,不需要用frame.add()
//监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
//按下回车就会触发这个输入框的事件
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
pack();
setVisible(true);
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField t=(TextField) e.getSource();//获得一些资源,返回的一个对象
System.out.println(t.getText());//获得输入框文本
t.setText("");//替换回车后输入框中的文本
}
}
简易计算器
简单实现加法计算:面向过程
public class TestCalculate {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame {
public Calculator() {
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1,num2,num3));
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
private TextField num1,num2,num3;
public MyCalculatorListener(TextField num1,TextField num2,TextField num3){
this.num1=num1;
this.num2=num2;
this.num3=num3;
}
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
优化实现加法计算:面向对象
public class TestCalculate {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame {
TextField num1,num2,num3;
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(this));//把自己传过去
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
Calculator calculator=null;
public MyCalculatorListener(Calculator calculator){
this.calculator=calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
int n1=Integer.parseInt(calculator.num1.getText());
int n2=Integer.parseInt(calculator.num2.getText());
calculator.num3.setText(""+(n1+n2));
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类,更好地包装
public class TestCalculate {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame {
TextField num1,num2,num3;
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener());
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听器类
//内部类最大的好处就是可以轻松访问外部的属性和方法
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
int n1=Integer.parseInt(num1.getText());
int n2=Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
}
画笔
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame {
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
//g.drawOval(100,100,100,100);
g.fillOval(100,100,100,100);
g.setColor(Color.green);
g.drawRect(100,200,100,100);
}
}
鼠标监听
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("drawing");
}
}
class MyFrame extends Frame {
ArrayList points;
@Override
public void paint(Graphics g) {
//迭代器
Iterator iterator = points.iterator();
while (iterator.hasNext()) {
Point point = (Point) iterator.next();
g.setColor(Color.blue);
g.fillOval(point.x, point.y, 10, 10);
}
}
//画画需要画笔,需要监听鼠标当前位置,需要集合来存储这个点
public MyFrame(String title) {
super(title);
setBounds(200, 200, 400, 300);
setVisible(true);
//存鼠标点击的位置
points = new ArrayList<>();
this.addMouseListener(new MyMouseListener());
}
//用implements MouseListener需要重写全部方法
//适配器模式不用全部重写方法
private class MyMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame = (MyFrame) e.getSource();
points.add(new Point(e.getX(), e.getY()));
//每次点击鼠标都需要重新画一遍
frame.repaint();//刷新
}
}
}
窗口监听
public class TestWindowListener {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBounds(100,100,400,400);
setVisible(true);
this.addWindowListener(
new WindowAdapter() {
//匿名内部类
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
}
}
);
}
}
键盘监听
public class TestKeyboardListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(50,50,300,300);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//获取键盘按下的键是哪一个
int keyCode=e.getKeyCode();
if(keyCode==KeyEvent.VK_UP){
System.out.println("你按下了↑键");
}
}
});
}
}
Swing
窗口、面板
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJFrame().init();
}
}
class MyJFrame extends JFrame{
public void init(){
setVisible(true);
setBounds(100,100,100,100);
//获得一个容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.yellow);
JLabel jLabel = new JLabel("永失吾爱,举目破败",SwingConstants.CENTER);
//jLabel.setHorizontalAlignment(SwingConstants.CENTER);设置水平居中
add(jLabel);
}
}
弹窗
- 弹窗默认有关闭事件
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
setVisible(true);
setSize(500,600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西,容器
Container contentPane = getContentPane();
//绝对布局
contentPane.setLayout(null);
JButton jButton = new JButton("点我弹出对话框");
jButton.setBounds(30,30,200,50);
//点击这个按钮时弹出弹窗
add(jButton);
jButton.addActionListener(new ActionListener() {//监听器
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗
class MyDialog extends JDialog{
public MyDialog(){
setVisible(true);
setBounds(100,100,500,500);
//setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = getContentPane();
container.setLayout(null);
JLabel l = new JLabel("痛");
l.setSize(50,50);
container.add(l);
}
}
标签
一般标签
new JLabel("text");
图标icon
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(){}
public IconDemo(int width,int height){
this.width=width;
this.height=height;
}
public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
//图标可以放在标签和按钮上
JLabel label = new JLabel("iconTest", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
图片icon
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获取图片路径
URL url = ImageIconDemo.class.getResource("img32.jpg");
JLabel label = new JLabel("ImageIcon");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
面板
JPanel
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(2,1,10,10));//后面两个参数的作用是设置间距
JPanel jPanel1 = new JPanel(new GridLayout(1,3));
JPanel jPanel2 = new JPanel(new GridLayout(1,2));
JPanel jPanel3 = new JPanel(new GridLayout(2,1));
JPanel jPanel4 = new JPanel(new GridLayout(3,2));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("2"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
//网格布局组件数超过其容纳个数会自动优化以容纳其他组件
contentPane.add(jPanel1);
contentPane.add(jPanel2);
contentPane.add(jPanel3);
contentPane.add(jPanel4);
setSize(500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScrollPanel
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container contentPane = getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("永失吾爱,举目破败");
//SCroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(300,350);
setVisible(true);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
按钮
图片按钮
public class JButtonDemo01 extends JFrame {
public JButtonDemo01(){
Container contentPane = getContentPane();
//将一张图片变为图标
URL resource = JButtonDemo01.class.getResource("img32.jpg");
ImageIcon imageIcon = new ImageIcon(resource);
//把这个图标放在按钮上
JButton jButton = new JButton();
jButton.setIcon(imageIcon);
jButton.setToolTipText("这是悬浮提示文本");
contentPane.add(jButton);
setSize(300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选按钮 JRadioButton
public class JButtonDemo02 extends JFrame {
public JButtonDemo02(){
Container contentPane = getContentPane();
//单选框
JRadioButton jRadioButton01 = new JRadioButton("JRadioButton01");
JRadioButton jRadioButton02 = new JRadioButton("JRadioButton02");
JRadioButton jRadioButton03 = new JRadioButton("JRadioButton03");
//分组,一个组中只能选一个
ButtonGroup group = new ButtonGroup();
group.add(jRadioButton01);
group.add(jRadioButton02);
group.add(jRadioButton03);
contentPane.add(jRadioButton01,BorderLayout.NORTH);
contentPane.add(jRadioButton02,BorderLayout.CENTER);
contentPane.add(jRadioButton03,BorderLayout.SOUTH);
setSize(300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
复选按钮 JCheckBox
public class JButtonDemo03 extends JFrame {
public JButtonDemo03(){
Container contentPane = getContentPane();
JCheckBox box01 = new JCheckBox("box01");
JCheckBox box02 = new JCheckBox("box02");
contentPane.add(box01,BorderLayout.NORTH);
contentPane.add(box02,BorderLayout.SOUTH);
setSize(300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
列表
下拉框
public class TestComboBox01 extends JFrame {
public TestComboBox01(){
Container contentPane = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映");
contentPane.add(status);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new TestComboBox01();
}
}
列表框
public class TestComboBox02 extends JFrame {
public TestComboBox02(){
Container contentPane = this.getContentPane();
//String[] contents={"1","2","3"};
//JList jList = new JList(contents);
Vector vector = new Vector();
JList jList = new JList(vector);
vector.add("zhangsan");
vector.add("lisi");
contentPane.add(jList);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new TestComboBox02();
}
}
文本框
文本框、密码框
public class TestText01 extends JFrame {
public TestText01(){
Container contentPane = this.getContentPane();
//文本框
TextField textField = new TextField("hello");
TextField textField2 = new TextField(",world",20);
//密码框
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
contentPane.add(textField,BorderLayout.NORTH);
contentPane.add(textField2,BorderLayout.SOUTH);
contentPane.add(passwordField,BorderLayout.CENTER);
contentPane.add(passwordField);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new TestText01();
}
}
文本域
- 见Swing -> 面板 ->JScrollPanel