awt是swing的前身!
用awt讲一些底层的实现,用swing画一些界面
包含了很多的类和接口! GUI:图形用户界面编程
元素:窗口,按钮,文本框
Java.awt
package com.cly.gui; import java.awt.*; public class TestFrame { public static void main(String[] args) { //Frame,jdk, 看源码 Frame frame = new Frame("我的第一个Java图形界面窗口");//Frame构造方法里面可以传入 String、可以传入画笔 //需要设置可见性 frame.setVisible(true); //设置窗口可见大小 frame.setSize(400,400); //设置背景颜色 frame.setBackground(Color.black); //frame.setBackground(new Color(6,6,6)); //弹出的初始位置 frame.setLocation(200,200); //设置大小固定(不能拉动) frame.setResizable(false); } }
问题:发现窗口关闭不掉,停止java程序就可以了!
尝试回顾封装
package com.cly.gui; import com.cly.method.Test; import java.awt.*; public class TestFrame2 { public static void main(String[] args) { //展示多个窗口 new MyFrame myFrame = new MyFrame(100, 100, 200, 200, Color.blue); MyFrame myFrame1 = new MyFrame(300, 100, 200, 200, Color.yellow); MyFrame myFrame2 = new MyFrame(100, 300, 200, 200, Color.red); MyFrame myFrame3 = 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 color){ super("Myframe+"+(++id)); setBackground(color); setBounds(x,y,w,h); setVisible(true); } }
package com.cly.gui; import java.awt.*; //Panel 可以看成是一个空间,但是不能单独存在,panel里存在流布局的概念 public class TestPanel { public static void main(String[] args) { Frame frame = new Frame(); Panel panel = new Panel(); //面板里存在布局的概念 frame.setLayout(null);//设置布局 默认的管理器 //坐标 frame.setBounds(300,300,500,500); frame.setBackground(new Color(40,161,35)); //panel设置坐标(内部面板的坐标) panel.setBounds(50,50,400,400); panel.setBackground(new Color(193,15,60)); //frame.add(panel) frame.add(panel); frame.setVisible(true); /* //监听事件,监听窗口关闭事件 System.exit(0); Windows的监听事件有很多,但是咱们只需要实现关闭窗口的监听事件就可以了 此时 用23种设计模式中的适配器模式:这个时候new windowAdapter(),然后可以选择其中的一个进行重写 frame.addWindowListener(new windowAdapter(){ @Override public void windowClosing(windowEvent e){ //结束程序 System.exit(0); } });*/ } }
流式布局
package com.cly.gui; import java.awt.*; public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); //组件-按钮 Button button1 = new Button("button1"); Button button2 = new Button("button1"); Button button3 = new Button("button1"); //设置为流式布局 //frame.setLayout(new FlowLayout()); //frame.setLayout(new FlowLayout(FlowLayout.LEFT)); frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); frame.setSize(200,200); //添加按钮 frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); } }
东西南北中
package com.cly.gui; import java.awt.*; public class TestBorderLayout { public static void main(String[] args) { Frame frame = new Frame("TestBorderLayout"); 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); } }
表格 Grid
package com.cly.gui; import java.awt.*; public class TestGridLayout { public static void main(String[] args) { Frame frame = new Frame(" TestGridLayout"); 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();//java函数!功能:自动选择一个最优秀的布局,自动填充大小 frame.setVisible(true); } }
4.1 代码实现
package com.cly.gui; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Demo { public static void main(String[] args) { //总的 Frame布局 Frame frame = new Frame(); frame.setSize(400,300); frame.setLocation(300,400); frame.setBackground(Color.BLACK); 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)); //上面ok p1.add(new Button("East-1"),BorderLayout.EAST); p1.add(new Button("West-1"),BorderLayout.WEST); p2.add(new Button("p2-btn-1")); p2.add(new Button("p2-btn-2")); p1.add(p2,BorderLayout.CENTER); //下面 p3.add(new Button("East-2"),BorderLayout.EAST); p3.add(new Button("West-2"),BorderLayout.WEST); //中间四个 for (int i = 0; i < 4; i++) { p4.add(new Button("for-"+i)); } p3.add(p4,BorderLayout.CENTER); frame.add(p1); frame.add(p3); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
4.2 代码运行结果
1. Frame是一个顶级窗口 2. Panel无法单独显示,必须添加到某个容器中 3. 布局管理器 1. 流式 2. 东西南北中 3. 表格 4. 大小,定位,背景颜色,可见性,监听
事件监听:当某个事情发生的时候,干什么?
案例一:
package com.cly.gui; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestActionEvent { public static void main(String[] args) { //按下按钮的时候触发一些事件 Frame frame = new Frame(); Button button = new Button(); //因为,addActionListener();需要一个ActionListener,所以我们需要构造一个ActionListener MyActionListener myActionListener = new MyActionListener(); button.addActionListener(myActionListener); frame.add(button,BorderLayout.CENTER); frame.pack();//让按钮自适应窗口大小,充满窗口 windowClose(frame);//关闭窗口 frame.setVisible(true); } //网络编程:按下按钮的时候会发送一条消息 //关闭窗口的事件 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"); } }
思考:
按钮的监听事件中 new 接口 然后实现匿名内部类 这种方法实现!
package com.cly.GUI; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class ActionEvent_ { public static void main(String[] args) { //按下按钮的时候触发一些事件 Frame frame = new Frame(); Button button = new Button(); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("饮茶先!"); } }); frame.add(button,BorderLayout.CENTER); frame.pack(); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
案例二:
package com.cly.GUI; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestActionEvent { public static void main(String[] args) { //两个按钮同时监听一个事件 //开始-停止 Frame frame = new Frame("开始-停止"); Button button1 = new Button("start"); Button button2 = new Button("stop"); //此方法可以显式的定义触发会返回的命令,如果不用此方法就会走默认的值 button2.setActionCommand("button2-stop"); MyMonitor myMonitor = new MyMonitor(); button1.addActionListener(myMonitor); button2.addActionListener(myMonitor); frame.add(button1,BorderLayout.NORTH); frame.add(button2,BorderLayout.SOUTH); frame.pack(); frame.setSize(300,300); frame.setLocation(10,10); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } class MyMonitor implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //e.getActionCommand()获取按钮上的一些信息 System.out.println("按钮被点击了:msg "+e.getActionCommand()); } }
package com.cly.GUI; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestText01 { public static void main(String[] args) { MyFrame myFrame = new MyFrame(); } } class MyFrame extends Frame{ public MyFrame(){ TextField textField = new TextField(); add(textField); //监听文本框输入的文字 MyActionListener2 myActionListener2 = new MyActionListener2(); //按下回车就会触发这个输入框的事件 textField.addActionListener(myActionListener2); //设置输入字符的替换编码 textField.setEchoChar('*'); setVisible(true); pack(); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(1); } }); } } class MyActionListener2 implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { TextField field=(TextField)e.getSource();//获得一些资源,返回了一个对象 System.out.println(field.getText());//获得输入框中的文本 field.setText(""); } }
oop原则:组合,大于继承!
写法一:采用继承的写法
package com.cly.GUI; //加法的GUI计算器 import javax.xml.soap.Text; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //简易计算器 public class TestCalc { 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(15); //一个按钮 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); setLocation(10,10); setSize(500,300); setVisible(true); //点击叉 退出程序 addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } //监听器类 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) { //1.获得加数和被加数 int n1 = Integer.parseInt(num1.getText()); int n2 = Integer.parseInt(num2.getText()); //2.将这个值 + 法运算后,放到第三个框 num3.setText(""+(n1+n2)); //3.清除前两个框 num1.setText(""); num2.setText(""); } }
采用组合的方法
package com.cly.GUI; import javax.xml.soap.Text; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //简易计算器 public class TestCalc { 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(15); Button button = new Button("="); Label label = new Label("+"); button.addActionListener(new MyCalculatorListener(this)); //布局 setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); setLocation(10,10); setSize(500,300); setVisible(true); //点击叉 退出程序 addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(1); } }); } } //监听器类 class MyCalculatorListener implements ActionListener{ //获取计算器这个对象,在一个类中组合另外一个类 Calculator calculator=null; public MyCalculatorListener(Calculator calculator){ this.calculator=calculator; } @Override public void actionPerformed(ActionEvent e) { //1.获得加数和被加数 //2.将这个值 + 法运算后,放到第三个框 //3.清除前两个框 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(""); } }
采用内部类方法
package com.cly.GUI; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //简易计算器 public class TestCalc { 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(15); Button button = new Button("="); Label label = new Label("+"); button.addActionListener(new MyCalculatorListener()); //布局 setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); setLocation(10,10); setSize(500,300); setVisible(true); //点击叉 退出程序 addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(1); } }); } //监听器类 //内部类的最大好处就是能够畅通无阻的访问外部类的属性和方法! private class MyCalculatorListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //1.获得加数和被加数 //2.将这个值 + 法运算后,放到第三个框 //3.清除前两个框 int n1 = Integer.parseInt(num1.getText()); int n2 = Integer.parseInt(num2.getText()); num3.setText(""+(n1+n2)); num1.setText(""); num2.setText(""); } } }
package com.cly.GUI; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestPaint { public static void main(String[] args) { new MyPaint().loadFrame(); } } class MyPaint extends Frame{ public void loadFrame(){ setLocation(50,50); setSize(200,200); setVisible(true); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(1); } }); } @Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawRect(100,100,100,100);//养成画笔用完把它还原成原来的颜色 } }
目的:实现鼠标画画!
package com.cly.GUI; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.Iterator; //鼠标监听事件 public class TestMouesListener { public static void main(String[] args) { new MyFrame1("画图"); } } //自己的类 class MyFrame1 extends Frame{ //画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点 ArrayList points; public MyFrame1(String title){ super(title); setBounds(200,200,400,300); //存鼠标点击的点 points=new ArrayList<>(); setVisible(true); //鼠标监听器,针对的是这个窗口 this.addMouseListener(new MyMouseListener()); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } @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 void addPaint(Point point){ points.add(point); } //由于重写所有的方法会显的代码冗余,所以采用适配器模式 private class MyMouseListener extends MouseAdapter { //鼠标 按压,长按,弹起 @Override//点击 public void mouseClicked(MouseEvent e) { super.mouseClicked(e); } @Override//按压 public void mousePressed(MouseEvent e) { MyFrame1 myFrame1 = (MyFrame1)e.getSource(); //这里我们点击的时候,就会在界面上产生一个点 //这个点就是鼠标的点 myFrame1.addPaint(new Point(e.getX(),e.getY())); //每次点击鼠标都需要重新画一遍 myFrame1.repaint(); } } }
内在关系:
package com.cly.GUI; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestWindows { public static void main(String[] args) { new WindowsFrame(); } } class WindowsFrame extends Frame{ public WindowsFrame(){ setBackground(Color.BLUE); setBounds(10,10,300,200); setVisible(true); //这个地方通过实现自己创建的内部类,也可以实现匿名内部类的方式实现 addWindowListener(new MyWindowsFrame()); } class MyWindowsFrame extends WindowAdapter{ @Override public void windowClosing(WindowEvent e) { System.exit(0);//0 是正常退出,1 是非正常退出 } } }
package com.cly.GUI; import javafx.scene.input.KeyCode; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestKeyListener { public static void main(String[] args) { new MyKeyListener(); } } class MyKeyListener extends Frame{ public MyKeyListener(){ setBounds(10,10,300,300); setBackground(Color.cyan); setVisible(true); addKeyListener(new KeyAdapter() { //键盘按下 @Override public void keyPressed(KeyEvent e) { //键盘按下的是哪一个键,当前码 int keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_A){ System.out.println("你按下了A"); }else if (keyCode == KeyEvent.VK_UP){ System.out.println("你按下了上键"); } } }); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
JFrameDemo.java
package com.cly.GUI.Swing; import javax.swing.*; import java.awt.*; public class JFrameDemo { public static void main(String[] args) { //建立一个窗口 new JFrameDemo().init(); } //init();初始化 public void init(){ //顶级窗口 JFrame jFrame = new JFrame("这是一个JFrame窗口"); jFrame.setBounds(10,10,300,300); jFrame.setBackground(Color.gray); jFrame.setVisible(true); //设置文字Jlabel JLabel jLabel = new JLabel("ありがとうございます。"); jFrame.add(jLabel); //关闭事件 jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }
JFrameDemo02.java
package com.cly.GUI.Swing; import javax.swing.*; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class JFrameDemo02 { public static void main(String[] args) { new MyJFrame2().init(); } } class MyJFrame2 extends JFrame{ public void init(){ //获得一个容器 Container container=this.getContentPane(); container.setBackground(Color.yellow); setBounds(10,10,200,200); //设置文字Jlabel JLabel jLabel = new JLabel("ありがとうございます。"); add(jLabel); //让我们的文本居中 jLabel.setHorizontalAlignment(SwingConstants.CENTER); setVisible(true); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
package com.cly.GUI.Swing; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //主窗口 public class DialogDemo extends JFrame { //初始化 该方法也可以用 DialogDemo设置成构造方法的那种样式 public void init(){ setBounds(10,10,200,200); setBackground(Color.gray); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //JFrame 放东西,容器 Container container = this.getContentPane(); //绝对布局 container.setLayout(null);//设置为绝对定位 //按钮 JButton button = new JButton("点击弹出一个对话框");//创建对象 button.setBounds(30,30,200,200); //点击按钮,弹出一个弹窗 button.addActionListener(new ActionListener() {//监听器 @Override public void actionPerformed(ActionEvent e) { //弹窗 new MyDialogDemo(); } }); container.add(button); } public static void main(String[] args) { new DialogDemo().init(); } } //弹窗的窗口 class MyDialogDemo extends JDialog{ public MyDialogDemo() { setVisible(true); setBounds(100,100,500,500); //弹窗是可以关掉的,不用设置此方法了 //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container container = this.getContentPane();//容器里添加组件 container.setLayout(null);//设置为绝对定位 container.add(new Label("秦老师带你学java")); } }
label没有弹出
label
new Label("文字");
图标ICON
package com.cly.GUI.Swing; import javax.swing.*; import java.awt.*; //图标,需要实现类,Frame继承 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); setBounds(10,10,200,200); 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
package com.cly.GUI.Swing; import javax.swing.*; import java.awt.*; import java.net.URL; public class ImageIconDemo extends JFrame { public ImageIconDemo(){ //获取图片地址 JLabel jLabel = new JLabel("ImageIconDemo"); URL url = ImageIconDemo.class.getResource("泰罗.jpg"); ImageIcon imageIcon = new ImageIcon(url); jLabel.setIcon(imageIcon); jLabel.setHorizontalAlignment(SwingConstants.CENTER); Container container = getContentPane(); container.add(jLabel); setBounds(20,20,800,800); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { new ImageIconDemo(); } }
JPanel
package com.cly.GUI.Swing;import javax.swing.*;import java.awt.*;public class TestJPanel extends JFrame { public TestJPanel(){ Container container = getContentPane(); container.setLayout(new GridLayout(2,1,10,10));//后两个参数就是间距的意思 JPanel jPanel = 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)); jPanel.add(new JButton("1")); jPanel.add(new JButton("1")); jPanel.add(new JButton("1")); jPanel2.add(new JButton("2")); jPanel2.add(new JButton("2")); 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")); container.add(jPanel); container.add(jPanel2); container.add(jPanel3); container.add(jPanel4); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(10,10,200,200); setVisible(true); } public static void main(String[] args) { new TestJPanel(); }}//这个是怎么装这么多的
JScrollPanel
package com.cly.GUI.Swing; import javax.swing.*; import java.awt.*; public class JScrollDemo extends JFrame { public JScrollDemo(){ Container container = getContentPane(); //文本域 JTextArea jTextArea = new JTextArea(20, 50); jTextArea.setText("欢迎学习Java!"); //Scroll面板 JScrollPane jScrollPane = new JScrollPane(jTextArea); container.add(jScrollPane); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(10,10,300,300); setVisible(true); } public static void main(String[] args) { new JScrollDemo(); } }
图片按钮:
package com.cly.GUI.Swing; import javax.swing.*; import java.awt.*; import java.net.URL; public class JButtonDemo01 extends JFrame { public JButtonDemo01(){ //用JFrame要先获得一个容器 Container container = getContentPane(); //将一个图片变为图标 URL resource = JButtonDemo01.class.getResource("泰罗.jpg"); ImageIcon icon = new ImageIcon(resource); //把这个图标放在按钮上 JButton button = new JButton(); button.setIcon(icon); button.setToolTipText("图片按钮"); //add container.add(button); setVisible(true); setBounds(10,10,200,200); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo01(); } }
单选按钮
package com.cly.GUI.Swing; import javax.swing.*; import java.awt.*; public class JButtonDemo02 extends JFrame { public JButtonDemo02(){ //用JFrame要先获得一个容器 Container container = getContentPane(); //单选框 JRadioButton radioButton1 = new JRadioButton("单选框1"); JRadioButton radioButton2 = new JRadioButton("单选框2"); JRadioButton radioButton3 = new JRadioButton("单选框3"); //由于单选框只能选择一个,分组,一个组中只能选择一个 //如果把分组干掉就变成多选了,但是复选框不叫 radioButton 所以看下面的 ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(radioButton1); buttonGroup.add(radioButton2); buttonGroup.add(radioButton3); container.add(radioButton1,BorderLayout.NORTH); container.add(radioButton2,BorderLayout.CENTER); container.add(radioButton3,BorderLayout.SOUTH); setVisible(true); setBounds(10,10,200,200); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo02(); } }
复选按钮
package com.cly.GUI.Swing; import javax.swing.*; import java.awt.*; public class JButtonDemo03 extends JFrame { public JButtonDemo03(){ //用JFrame要先获得一个容器 Container container = getContentPane(); JCheckBox jCheckBox1 = new JCheckBox("checkbox1"); JCheckBox jCheckBox2 = new JCheckBox("checkbox2"); JCheckBox jCheckBox3 = new JCheckBox("checkbox3"); container.add(jCheckBox1,BorderLayout.NORTH); container.add(jCheckBox2,BorderLayout.CENTER); container.add(jCheckBox3,BorderLayout.SOUTH); setVisible(true); setBounds(10,10,200,200); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo03(); } }
下拉框
package com.cly.GUI.Swing; import javax.swing.*; import java.awt.*; public class DropDownBox01 extends JFrame { public DropDownBox01(){ //用JFrame要先获得一个容器 Container container = getContentPane(); JComboBox<Object> status = new JComboBox<>(); status.addItem(null); status.addItem("正在上映"); status.addItem("已下架"); status.addItem("即将上映"); container.add(status); setVisible(true); setBounds(10,10,200,200); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new DropDownBox01(); } }
列表框
package com.cly.GUI.Swing; import javax.swing.*; import java.awt.*; public class ListViewBox extends JFrame { public ListViewBox(){ //用JFrame要先获得一个容器 Container container = getContentPane(); //生成列表内容 String[] contents={"1","2","3","4","5"}; //列表中放入内容 JList jList = new JList(contents); container.add(jList); setVisible(true); setBounds(10,10,200,200); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new ListViewBox(); } }
文本框
package com.cly.GUI.Swing; import javax.swing.*; import java.awt.*; public class TextDemo01 extends JFrame{ public TextDemo01(){ Container container = getContentPane(); TextField field1 = new TextField("打工人"); TextField field2 = new TextField("打工魂"); TextField field3 = new TextField("打工都是人上人!"); container.add(field1,BorderLayout.NORTH); container.add(field2,BorderLayout.CENTER); container.add(field3,BorderLayout.SOUTH); setVisible(true); setBounds(10,10,200,200); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TextDemo01(); } }
密码框
package com.cly.GUI.Swing; import javafx.scene.control.PasswordField; import javax.swing.*; import java.awt.*; import java.net.PasswordAuthentication; public class TextDemo02 extends JFrame { public TextDemo02(){ Container container=getContentPane(); JPasswordField passwordField = new JPasswordField();//密码框 passwordField.setEchoChar('*'); container.add(passwordField); setVisible(true); setBounds(10,10,200,200); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TextDemo02(); } }
文本域
package com.cly.GUI.Swing; import javax.swing.*; import java.awt.*; public class JScrollDemo extends JFrame { public JScrollDemo(){ Container container = getContentPane(); //文本域 JTextArea jTextArea = new JTextArea(20, 50); jTextArea.setText("欢迎学习Java!"); //Scroll面板 JScrollPane jScrollPane = new JScrollPane(jTextArea); container.add(jScrollPane); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(10,10,300,300); setVisible(true); } public static void main(String[] args) { new JScrollDemo(); } }