目录
GUI编程
该怎么学?
组件
-
窗口
-
弹窗
-
面板
-
文本框
-
列表框
-
按钮
-
图片
-
监听事件
-
鼠标
-
键盘事件
-
外挂
1、简介
Gui的核心技术:Swing AWT
缺点:
-
需要jre环境
-
需要jre环境
-
因为界面不美观
为什么我们要学习?
-
可以写出自己心中想要的一些小工具
-
工作的时候,也可能需要维护到swing界面,概率极低
-
了解MVC架构,了解监听!
2、AWT
2.1、AWT介绍
-
包含了很多类和接口!GUI!
-
元素:窗口,按钮,文本框
-
Java.awt
2.2组件和容器
1、Frame
package com.wang.lesson01;
import java.awt.*;
//GUI第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame , JDK , 看源码!
Frame frame = new Frame("我的第一个java图像界面窗口");
//需要设置可见性 w h
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色 Color
frame.setBackground(new Color(121, 234, 122, 255));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
问题:发现窗口关闭不掉,停止java程序
尝试回归封装
package com.wang.lesson01;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口 new
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 color){
super("Myframe"+(++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}
2、面板Panel
解决了关闭事件
package com.wang.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//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(19, 101, 182));
//Panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(222, 9, 9));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
2.3、布局管理器
-
流式布局
package com.wang.lesson01;
import java.awt.*;
public class TestFowLayout {
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.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.wang.lesson01;
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(500,500);
frame.setVisible(true);
}
}
-
表格布局 Grid
package com.wang.lesson01;
import java.awt.*;
public class TestGridLayout {
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");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack();//java函数
frame.setVisible(true);
}
}
总结:
-
Frame是一个顶级窗口
-
Panel无法单独显示,必须添加到某个容器中
-
布局管理器
-
流式
-
东西南北中
-
表格
-
-
大小、定位、背景颜色、可见性、监听
练习
package com.wang.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//练习
public class ExDemo {
public static void main(String[] args) {
//总 Frame
Frame frame = new Frame();
frame.setVisible(true);
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setBackground(Color.BLACK);
frame.setLayout(new GridLayout(2,1));
//4个面板
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);
//中间4个
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);
}
});
}
}
2.4、事件监听
事件监听:当某个事情发生的时候,干什么
package com.wang.lesson02;
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("est");
//因为,addActionListener需要一个ActionListener ,所以我们需要构建一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.setSize(300,400);
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");
}
}
多个按钮共享一个事件
package com.wang.lesson02;
//多个按钮共享一个事件
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionTwo {
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.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获得按钮的信息
System.out.println("按钮被点击了:msg=>"+e.getActionCommand());
}
}
2.5、输入框TextField监听
package com.wang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText {
public static void main(String[] args) {
//启动
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();
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field= (TextField) e.getSource();//获得一些资源,返回一个对象
System.out.println(field.getText());
field.setText("");
}
}
2.6、简易计算器,组合+内部类回顾复习
oop原则:组合,大于继承!
运用内部类
-
更好的包装
package com.wang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
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(20);
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);
pack();
setVisible(true);
}
//监听器类
//内部类最大的好处,就是可以畅通无阻的访问外部类的属性和方法
class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
}
2.7、画笔
package com.wang.lesson03;
import java.awt.*;
public class TextPaint {
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.fillOval(100,100,100,100);//实心圆
//g.setColor(Color.green);
g.fillRect(150,200,200,200);
//养成习惯,画笔用完,将他还原成最初的颜色
}
}
2.8、鼠标监听
目的:想要实现鼠标画画
package com.wang.lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
//鼠标监听事件
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
//自己的类
class MyFrame extends Frame{
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
ArrayList points;
public MyFrame(String title){
super(title);
setBounds(200,200,400,400);
setVisible(true);
//存放鼠标点击的点
points = new ArrayList<>();
//鼠标监听器,正对这个窗口
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g) {
//画画,监听鼠标的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.RED);
g.fillOval(point.x,point.y,10,10);
}
}
//添加一个点到界面上
public void addPaint(Point point){
points.add(point);
}
//适配器模式
private class MyMouseListener extends MouseAdapter{
//鼠标按下、弹起、按住不放
@Override
public void mousePressed(MouseEvent e) {
Frame frame = (Frame) e.getSource();
//这个我们点击的时候,就会在界面上产生一个点!画
//这个点就是鼠标的点
addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
frame.repaint();
}
}
//paint是根据坐标画点,监听器产生坐标调用addPaint方法把坐标存到集合,画笔在遍历集合拿到坐标就开始画点
}
2.9、窗口监听
package com.wang.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.BLUE);
setBounds(200,200,300,300);
setVisible(true);
//addWindowListener(new MyWindowListener());
this.addWindowListener(
new WindowAdapter() {
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
MyFrame source = (MyFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("windowActivated");
}
}
);
}
}
2.10、键盘监听
package com.wang.lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//键盘
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(200,200,300,300);
setVisible(true);
this.addKeyListener(
new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();//不需要去记录这个数值,直接使用静态数值
if (keyCode==KeyEvent.VK_UP){ //获取键盘下的键是哪一个,当前的码UP){
System.out.println("你按下了上键");
}
//根据不同操作,产生不同结果
}
}
);
}
}
3、swing
3.1、窗口、面板
package com.wang.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init();初始化
public void init(){
//顶级窗口
JFrame jf = new JFrame("这是一个JFrame窗口");
jf.setVisible(true);
jf.setBounds(100,100,200,200);
jf.setBackground(Color.cyan);
//设置文字Jlable
JLabel lable = new JLabel("hello");
jf.add(lable);
//关闭事件.....JFrame默认点×可以隐藏窗口,但不结束程序
jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
标签居中
package com.wang.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJframe2().init();
}
}
class MyJframe2 extends JFrame{
public void init(){
this.setBounds(10,10,200,300);
this.setVisible(true);
JLabel lable = new JLabel("hello",SwingConstants.CENTER);
this.add(lable);
//让文本标签居中 设置水平对齐
//lable.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.yellow);
}
}
3.2、弹窗
package com.wang.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//JFrame 放东西 容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
//点击这个按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(100,100,500,500);
//默认存在关闭事件
//this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container container = this.getContentPane();
//container.setLayout(null);
container.setBounds(100,100,100,100);
container.add(new JLabel("跟着秦老师一起学java"));
}
}
3.3、标签
lable
new Label("xxx")
图标ICON
package com.wang.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame{
public ImageIconDemo(){
//获取图片地址
JLabel lable = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("图片1.jpg");
ImageIcon imageIcon = new ImageIcon(url);
lable.setIcon(imageIcon);
lable.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(lable);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
图片ICON
package com.wang.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame{
public ImageIconDemo(){
//获取图片地址
JLabel lable = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("图片1.jpg");
ImageIcon imageIcon = new ImageIcon(url);
lable.setIcon(imageIcon);
lable.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(lable);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
3.4、面板
JPanel
package com.wang.lesson5;
import javax.swing.*;
import java.awt.*;
public class PanelDemo extends JFrame {
public PanelDemo(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,2,10,10));//后面参数的意思:间距
JPanel panel1 = new JPanel(new GridLayout(1,3));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(2,1));
JPanel panel4 = new JPanel(new GridLayout(3,2));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new PanelDemo();
}
}
JScrollPanel(下拉框面板)
package com.wang.lesson5;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("hello");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.5、按钮
图片按钮
package com.wang.lesson5;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
//将一个图片变为图标
URL resource = JButtonDemo01.class.getResource("图片2.jpg");
ImageIcon icon = new ImageIcon(resource);
//把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
//把按钮放到容器里
container.add(button);
this.setVisible(true);
this.setSize(300,500);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
-
单选按钮
package com.wang.lesson5; import javax.swing.*; import java.awt.*; import java.net.URL; public class JButtonDemo02 extends JFrame { public JButtonDemo02() { Container container = this.getContentPane(); //单选框 JRadioButton radioButton1 = new JRadioButton("JRadioButton01"); JRadioButton radioButton2 = new JRadioButton("JRadioButton02"); JRadioButton radioButton3 = new JRadioButton("JRadioButton03"); //由于单选框智能选择一个,所以分组,一个组中只能选择一个 ButtonGroup group = new ButtonGroup(); group.add(radioButton1); group.add(radioButton2); group.add(radioButton3); container.add(radioButton1,BorderLayout.CENTER); container.add(radioButton2,BorderLayout.NORTH); container.add(radioButton3,BorderLayout.SOUTH); this.setVisible(true); this.setSize(300,500); this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo02(); } } -
复选按钮
package com.wang.lesson5; import javax.swing.*; import java.awt.*; import java.net.URL; public class JButtonDemo03 extends JFrame { public JButtonDemo03() { Container container = this.getContentPane(); //多选框 Checkbox checkbox1 = new Checkbox("checkbox1"); Checkbox checkbox2 = new Checkbox("checkbox2"); container.add(checkbox1,BorderLayout.NORTH); container.add(checkbox2,BorderLayout.SOUTH); this.setVisible(true); this.setSize(300,500); this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo03(); } }
3.6、列表
-
下拉框
package com.wang.lesson06; import javax.swing.*; import java.awt.*; public class TextComboboxDemo01 extends JFrame { public TextComboboxDemo01(){ Container container = this.getContentPane(); JComboBox status = new JComboBox(); status.addItem(null); status.addItem("正在热映"); status.addItem("已下架"); status.addItem("即将上映"); container.add(status); this.setVisible(true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TextComboboxDemo01(); } } -
列表框
package com.wang.lesson06; import javax.swing.*; import java.awt.*; import java.util.Vector; public class TextComboboxDemo02 extends JFrame { public TextComboboxDemo02(){ Container container = this.getContentPane(); //生成列表内容 //String[] contents = {"1","2","3"}; Vector contens = new Vector(); JList jList = new JList(contens); contens.add("zhangsan"); contens.add("lisi"); contens.add("wangwu"); container.add(jList); this.setVisible( true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TextComboboxDemo02(); } }
3.7、文本框
-
文本框
package com.wang.lesson06; import javax.swing.*; import java.awt.*; import java.util.Vector; public class TextTextDemo01 extends JFrame { public TextTextDemo01(){ Container container = this.getContentPane(); JTextField jTextField = new JTextField("hello"); JTextField jTextField2= new JTextField("word",20); container.add(jTextField,BorderLayout.SOUTH); container.add(jTextField2,BorderLayout.NORTH); this.setVisible( true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TextTextDemo01(); } } -
密码框
package com.wang.lesson06; import javax.swing.*; import java.awt.*; public class TextTextDemo02 extends JFrame { public TextTextDemo02(){ Container container = this.getContentPane(); JPasswordField passwordField = new JPasswordField();//*** passwordField.setEchoChar('*'); container.add(passwordField); this.setVisible( true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TextTextDemo02(); } } -
文本域
package com.wang.lesson5; import javax.swing.*; import java.awt.*; public class JScrollDemo extends JFrame { public JScrollDemo(){ Container container = this.getContentPane(); //文本域 JTextArea textArea = new JTextArea(20, 50); textArea.setText("hello"); //Scroll面板 JScrollPane scrollPane = new JScrollPane(textArea); container.add(scrollPane); this.setVisible(true); this.setBounds(100,100,300,350); this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } public static void main(String[] args) { new JScrollDemo(); } }