GUI编程
因为java实验要用到这个,于是上b站粗略学了一下,感觉很多和前端都挺类似的,记录一下。
1.简介
Gui 的核心技术: Swing AWT
为什么要学习:
- 写出想要的小工具
- 维护swing界面
- 了解MVC架构,了解监听
2.AWT
2.1 AWT介绍
- 包含了很多类和接口
- 元素:窗口,按钮,文本框
- java.awt
2.2 组件和容器
1.Frame
public class TestFrame {
public static void main(String[] args) {
//Frame
Frame frame = new Frame("我的第一个java图形界面窗口");
//需要设置可见性 w h
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(Color.lightGray);
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
问题:发现窗口关闭不掉,停止java程序就行
尝试回顾封装:
package com.q.lesson1;
import java.awt.*;
/**
* @author anonymity-0
* @date 2020/10/18 - 20:02
*/
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.black);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.blue);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.red);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.gray);
}
}
class MyFrame extends Frame{
static int id =0; //可能存在多个窗口,需要一个计数器
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame"+(++id));
setBounds(x,y,w,h);
setVisible(true);
setBackground(color);
}
}
2. 面板
解决关闭事件
package main.java.com.q.lesson1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @author anonymity-0
* @date 2020/10/18 - 20:11
* 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(1,1,1));
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(253, 252, 252));
//frame.add
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭时做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
2.3.布局管理器
-
流式布局
package main.java.com.q.lesson1; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** * @author anonymity-0 * @date 2020/10/19 - 16:44 */ public class TestFlowLayout { 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(FlowLayout.LEFT)); frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); frame.setSize(200,200); //把按钮添加 frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); //关闭事件 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
-
东西南北中
package main.java.com.q.lesson1; import java.awt.*; /** * @author anonymity-0 * @date 2020/10/19 - 16:52 */ 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 main.java.com.q.lesson1; import java.awt.*; /** * @author anonymity-0 * @date 2020/10/19 - 16:57 */ 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();//自动布局 // frame.setSize(200,200); frame.setVisible(true); } }
总结:
- Frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器中
- 布局管理器
- 流式布局
- 东西南北中
- 表格
- 大小,定位,背景颜色,可监听
2.4.事件监听
当某件事情发生的时候要干什么
package main.java.com.q.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @author anonymity-0
* @date 2020/10/20 - 8:00
*/
public class TestActionEvent {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame("main.java.com.q.lesson2.TestActionEvent");
Button button = new Button("button");
//addActionListener需要一个ActionListener所以我们需要一个
myActionListener myActionListener = new myActionListener();
button.addActionListener(myActionListener);
frame.add(button);
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("hello world!");
}
}
package main.java.com.q.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author anonymity-0
* @date 2020/10/20 - 8:37
*/
public class TestActionTwo {
public static void main(String[] args) {
//两个按钮实现同一个监听
Frame frame = new Frame("开始-停止");
Button btn1 = new Button("strat");
Button btn2 = new Button("stop");
btn2.setActionCommand("button2-stop");
MyMonitor myMonitor = new MyMonitor();
btn1.addActionListener(myMonitor);
btn2.addActionListener(myMonitor);
frame.add(btn1,BorderLayout.NORTH);
frame.add(btn2,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 监听
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author anonymity-0
* @date 2020/10/20 - 9:20
*/
public class TestText01 {
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 textField = (TextField) e.getSource();//获得一些资源,返回了一个对象
System.out.println(textField.getText());
textField.setText("");
}
}
2.6.简易计算器
package main.java.com.q.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author anonymity-0
* @date 2020/10/20 - 9:35
* 简易计算器
*/
public class TestCalc {
public static void main(String[] args) {
Caculator caculator = new Caculator();
caculator.loadFrame();
}
}
//计算器类
class Caculator extends Frame{
//属性
TextField num1,num2,num3;
public void loadFrame(){
//3个文本框
num1 = new TextField(10);//字符数
num2 = new TextField(10);
num3 = new TextField(20);
//一个按钮
Button button = new Button("=");
//一个标签
Label label = new Label("+");
button.addActionListener(new MyCaculateListener(this));
setLayout(new FlowLayout());
//布局
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
pack();
}
//监听器类
private class MyCaculateListener implements ActionListener{
//获取计算器,在一个类中组合另外一个类
Caculator caculator =null;
public MyCaculateListener(Caculator caculator) {
this.caculator=caculator;
}
@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("");
}
}
}
组合+内部类回顾复习
opp原则:组合大于继承
//继承
class A extends B{
}
//组合
class A{
pubulic B b;
}
内部类
更好的包装,内部类最大的好处,就是可以畅通无阻的访问外部类
2.7画笔 Paint
package main.java.com.q.lesson3;
import java.awt.*;
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.fillRect(150,200,200,200);
//养成习惯,画笔用完,将他还原到最初的颜色
}
2.8 鼠标监听
目的:实现鼠标点击画画
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JnQe00RW-1603721896992)(C:\Users\86151\AppData\Roaming\Typora\typora-user-images\image-20201025225202775.png)]
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
/**
* @author anonymity-0
* @date 2020/10/25 - 22:20
*/
//鼠标监听事件
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,300);
//存鼠标点击的点
points = new ArrayList<>();
//鼠标监听器,针对这个窗口
setVisible(true);
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.CYAN);
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) {
MyFrame frame = (MyFrame) e.getSource();
//点击的时候就会在界面上产生一个点
//这个点就是鼠标的点
frame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都要重新画一遍
frame.repaint();
}
}
}
2.9 窗口监听
package main.java.com.q.lesson3;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @author anonymity-0
* @date 2020/10/25 - 22:55
*/
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.white);
setBounds(100,100,200,200);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
//匿名内部类
@Override
//关闭窗口
public void windowClosing(WindowEvent e) {
System.out.println("你点击了关闭");
System.exit(0);
}
@Override
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("windowClosed");
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("windowActivated");
}
});
}
}
2.10 键盘监听
package main.java.com.q.lesson3;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @author anonymity-0
* @date 2020/10/25 - 22:55
*/
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.white);
setBounds(100,100,200,200);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
//匿名内部类
@Override
//关闭窗口
public void windowClosing(WindowEvent e) {
System.out.println("你点击了关闭");
System.exit(0);
}
@Override
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("windowClosed");
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("windowActivated");
}
});
}
}
3.Swing
3.1窗口、面板
package main.java.com.q.lesson4;
import javax.swing.*;
import java.awt.*;
/**
* @author anonymity-0
* @date 2020/10/25 - 23:32
*/
public class JFrameDemo {
//init();初始化
public void init(){
//顶级窗口
JFrame jf = new JFrame("这是一个Jframe窗口");
jf.setBounds(100,100,200,200);
jf.setVisible(true);
jf.setBackground(Color.cyan); // 无效
//设置文字 Jlable
JLabel label = new JLabel("haha");
jf.add(label);
//让文本标签居中
label.setHorizontalAlignment(SwingConstants.CENTER);
//容器实例化 获得容器
Container contentPane = jf.getContentPane();
contentPane.setBackground(Color.CYAN);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
3.2 弹窗
package main.java.com.q.lesson4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author anonymity-0
* @date 2020/10/25 - 23:45
*/
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_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);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new JLabel("今天不学习,明天变垃圾"));
}
}
3.3 标签
label
new JLabel("xxx");
图标 ICON
package main.java.com.q.lesson4;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
* @author anonymity-0
* @date 2020/10/26 - 16:47
*/
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获取图片的地址,根据类获得路径的地址
JLabel label = new JLabel("ImageIconDemo");
URL url = ImageIconDemo.class.getResource("test.jpeg");
ImageIcon imageIcon = new ImageIcon();
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
3.4 面板
JPaner
package main.java.com.q.lesson5;
import javax.swing.*;
import java.awt.*;
/**
* @author anonymity-0
* @date 2020/10/26 - 16:58
*/
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//后面参数的意思
JPanel panel1 = new JPanel(new GridLayout(1, 3));
panel1.add(new JButton("1"));
panel1.add(new JButton("2"));
panel1.add(new JButton("3"));
container.add(panel1);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
带滚动条的面板
JScrollPannel
package main.java.com.q.lesson5;
import javax.swing.*;
import java.awt.*;
/**
* @author anonymity-0
* @date 2020/10/26 - 17:05
*/
public class JscrollDemo extends JFrame {
public static void main(String[] args) {
new JscrollDemo();
}
public JscrollDemo() {
Container container = this.getContentPane();
//文本框
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("hahaha");
container.add(textArea);
//scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3.5 按钮
-
图片按钮
package main.java.com.q.lesson5; import javax.swing.*; import java.awt.*; import java.net.URL; /** * @author anonymity-0 * @date 2020/10/26 - 17:46 */ public class ButtonDemo01 extends JFrame { public ButtonDemo01() { Container container = this.getContentPane(); //将图片变成图标 URL url = ButtonDemo01.class.getResource("test.jpeg"); Icon icon = new ImageIcon(url); //把这个图标放在按钮上 JButton button = new JButton(); button.setIcon(icon); button.setToolTipText("图片按钮"); container.add(button); this.setSize(500,300); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new ButtonDemo01(); } }
-
单选按钮
package main.java.com.q.lesson5; import javax.swing.*; import java.awt.*; import java.net.URL; /** * @author anonymity-0 * @date 2020/10/26 - 17:56 */ public class JButtonDemo02 extends JFrame{ public JButtonDemo02() { Container container = this.getContentPane(); //将图片变成图标 URL url = ButtonDemo01.class.getResource("test.jpeg"); Icon icon = new ImageIcon(url); //单选框 JRadioButton radioButton01 = new JRadioButton("JRadioButton01"); JRadioButton radioButton02 = new JRadioButton("JRadioButton02"); JRadioButton radioButton03 = new JRadioButton("JRadioButton03"); //由于单选框只能选择一个,所以要分组 ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(radioButton01); buttonGroup.add(radioButton02); buttonGroup.add(radioButton03); container.add(radioButton01,BorderLayout.CENTER); container.add(radioButton02,BorderLayout.NORTH); container.add(radioButton03,BorderLayout.SOUTH); this.setSize(500,300); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo02(); } }
-
复选按钮
package main.java.com.q.lesson5;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
* @author anonymity-0
* @date 2020/10/26 - 20:44
*/
public class JButtonDemo03 extends JFrame{
public JButtonDemo03() {
Container container = this.getContentPane();
//将图片变成图标
URL url = ButtonDemo01.class.getResource("test.jpeg");
Icon icon = new ImageIcon(url);
JCheckBox checkBox01 = new JCheckBox("checkBox01");
JCheckBox checkBox02 = new JCheckBox("checkBox02");
container.add(checkBox01,BorderLayout.NORTH);
container.add(checkBox02,BorderLayout.SOUTH);
this.setSize(500,300);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
3.6 列表
-
下拉框
package main.java.com.q.lesson6; import javax.swing.*; import java.awt.*; /** * @author anonymity-0 * @date 2020/10/26 - 20:51 */ public class TestComboboxDemo01 extends JFrame { public TestComboboxDemo01(){ Container container = getContentPane(); JComboBox comboBox = new JComboBox(); comboBox.addItem(null); comboBox.addItem("学习中"); comboBox.addItem("游戏中"); comboBox.addItem("睡觉中"); container.add(comboBox); this.setVisible(true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestComboboxDemo01(); } }
-
列表框
package main.java.com.q.lesson6; import javax.swing.*; import java.awt.*; /** * @author anonymity-0 * @date 2020/10/26 - 20:55 */ public class TestComboboxDemo02 extends JFrame { public TestComboboxDemo02() { Container container = getContentPane(); //生成列表内容 String[] contents ={"1","2","3"}; //列表中放入内容 JList list = new JList(contents); container.add(list); this.setVisible(true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestComboboxDemo02(); } }
-
应用场景
- 选择地区
- 展示信息
3.7 文本框
- 文本框
import javax.swing.*;
import java.awt.*;
/**
* @author anonymity-0
* @date 2020/10/26 - 21:01
*/
public class TestTextDemo01 extends JFrame {
public static void main(String[] args) {
new TestTextDemo01();
}
public TestTextDemo01() {
Container container = this.getContentPane();
JTextField textField = new JTextField("Hello");
JTextField textField2 = new JTextField("world!");
container.add(textField,BorderLayout.SOUTH);
container.add(textField2,BorderLayout.NORTH);
this.setVisible(true);
this.setSize(300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
-
密码框
package main.java.com.q.lesson6; import javax.swing.*; import java.awt.*; /** * @author anonymity-0 * @date 2020/10/26 - 21:59 */ public class TestTextDemo02 extends JFrame { public static void main(String[] args) { new TestTextDemo02(); } public TestTextDemo02() { Container container = this.getContentPane(); JPasswordField passwordField = new JPasswordField(); passwordField.setEchoChar('/'); container.add(passwordField); this.setVisible(true); this.setSize(300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }
-
文本域
package main.java.com.q.lesson5; import javax.swing.*; import java.awt.*; /** * @author anonymity-0 * @date 2020/10/26 - 17:05 */ public class JscrollDemo extends JFrame { public static void main(String[] args) { new JscrollDemo(); } public JscrollDemo() { Container container = this.getContentPane(); //文本框 JTextArea textArea = new JTextArea(20, 50); textArea.setText("hahaha"); container.add(textArea); //scroll面板 JScrollPane scrollPane = new JScrollPane(textArea); container.add(scrollPane); this.setVisible(true); this.setBounds(100,100,300,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }
{
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('/');
container.add(passwordField);
this.setVisible(true);
this.setSize(300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
- 文本域
```java
package main.java.com.q.lesson5;
import javax.swing.*;
import java.awt.*;
/**
* @author anonymity-0
* @date 2020/10/26 - 17:05
*/
public class JscrollDemo extends JFrame {
public static void main(String[] args) {
new JscrollDemo();
}
public JscrollDemo() {
Container container = this.getContentPane();
//文本框
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("hahaha");
container.add(textArea);
//scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}