这是我的在B站上的听课时,码的老师讲课的代码,没有过多的解释,有什么可以不懂得可以去看课。
之后的贪吃蛇之后再发,以及一个简单的画图板。
B站链接
package lesson1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestFrame {
public static void main(String args[])
{
//Frame,JDK,看源码
Frame frame = new Frame("我的第一个Java窗口!");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400, 400);
//设置背景颜色
frame.setBackground(new Color(85,150,68));
//设置大小固定
frame.setResizable(false);
frame.addWindowListener(new WindowAdapter() {
//窗口关闭的时候需要做的事情
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
关闭事件,窗口关不了
package lesson1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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.red);
MyFrame myframe3 = new MyFrame(100,300,200,200,Color.yellow);
MyFrame myframe4 = new MyFrame(300,300,200,200,Color.green);
}
}
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 lesson1;
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(40,160,32));
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(193,15,60));
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口关闭的时候需要做的事情
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
package lesson1;
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("button2");
Button button3 = new Button("button3");
//设置为流式布局
//frame.setLayout(new FlowLayout());
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200, 200);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
package lesson1;
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);
}
}
package lesson1;
import java.awt.*;
public class TestGridLayout {
public static void main(String args[]) {
Frame frame = new Frame("TestBorderLayout");
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,1,1));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack();
frame.setVisible(true);
}
}
package lesson1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ExerciseDemo {
public static void main(String args[]) {
Frame frame = new Frame();
frame.setSize(400,400);
frame.setLocation(300, 300);
frame.setBackground(Color.blue);
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("East-1"),BorderLayout.EAST);
p1.add(new Button("West-1"),BorderLayout.WEST);
p2.add(new Button("p2-button-1"));
p2.add(new Button("p2-button-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= 1;i<=4;i++) {
p4.add(new Button("for-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
package lesson2;
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() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
package lesson2;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionEvent2 {
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.EAST);
frame.add(button2,BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//
System.out.println("按钮被点击了"+e.getActionCommand());
}
}
package lesson2;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.sound.midi.Soundbank;
public class TestText01 {
public static void main(String args[]) {
//启动!
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame() {
TextField textField = new TextField();//写单行文本
add(textField);
//监听这个文本框输入的文字
MyActionListen02 myActionListen02 = new MyActionListen02(); //监听这个文本框输入的文字
//按下enter就会触发这个输入框的时间
textField.addActionListener(myActionListen02);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListen02 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField)e.getSource();//获得一些资源
System.out.println(field.getText());//获得输入框中的文本
field.setText("");//清空
}
}
package lesson2;
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(this));
//布局
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("");
}
}
//监听器类
/*
*
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("");
}
}
*/
package lesson3;
import java.awt.*;
import java.awt.Paint;
import java.awt.PaintContext;
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);
}
//画笔
public void paint(Graphics g) {
//画笔,需要有颜色,画笔可以画画
g.setColor(Color.red);
g.drawOval(100, 100, 100, 100);
g.fillOval(300, 300, 100, 100);
g.setColor(Color.GREEN);
g.fillRect(150, 200, 200, 200);
}
}
目的:想要实现鼠标画画!
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 TestMouseListen {
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());
}
public void paint( 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 addPoint(Point point) {
points.add(point);
}
//适配器模式
private class MyMouseListener extends MouseAdapter{
//按下,弹起,按住不放
public void mouseClicked(MouseEvent e) {
MyFrame myFrame = (MyFrame) e.getSource();
//我们点击的时候,就会在界面上产生一个点
//这个点就是鼠标的点
myFrame.addPoint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重画一遍
myFrame.repaint();//每次刷新
}
}
}
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String aegs[]) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame() {
setBackground(Color.DARK_GRAY);
setBounds(100, 100, 200, 200);
setVisible(true);
addWindowListener(new MyWindowListen());
}
class MyWindowListen extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);//隐藏窗口,通过按钮
System.exit(0);//正常退出
}
}
}
优化后:
package lesson3;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String aegs[]) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame() {
setBackground(Color.DARK_GRAY);
setBounds(100, 100, 200, 200);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing!");
setVisible(false);//隐藏窗口,通过按钮
System.exit(0);//正常退出
}
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("你好棒!");
System.out.println("windowActivated!");
}
});
}
}
package lesson3;
import java.awt.Frame;
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(1, 2, 300, 400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获取键盘下的键是什么
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_UP) {
System.out.println("你按下了上键!");
}
//根据按下不同的操作,产生不同的结果
}
});
}
}
package lesson4;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init();初始化
public void init() {
//顶级窗口
JFrame jFrame = new JFrame("这是一个JFrame窗口");
jFrame.setVisible(true);
jFrame.setBounds(100,100,200,200);
jFrame.setBackground(Color.darkGray);
//设置文字Jlabel
JLabel label = new JLabel("不欢迎请滚!");
jFrame.add(label);
//让我们的文本标签居中
label.setHorizontalAlignment(SwingConstants.CENTER);
//实例化对象
Container container = jFrame.getContentPane();
container.setBackground(Color.YELLOW);
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
//建立一个窗口
new JFrameDemo().init();
}
}
package lesson4;
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.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();
}
}
//弹窗的窗口
class MydialogDemo extends JDialog{
public MydialogDemo() {
this.setVisible(true);
this.setBounds(100,100,500,500);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.setBounds(30, 30, 60, 60);
container.add(new Label("真丑"));
}
}
new Jlable("XXX");
package lesson4;
import java.awt.*;
import javax.swing.*;
//图标,需要实现类,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);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@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;
}
public static void main(String[] args) {
new IconDemo().init();
}
}
package lesson4;
import java.net.URL;
import java.awt.*;
import javax.swing.*;
public class ImageIconDemo extends JFrame {
public ImageIconDemo() {
//获取图片地址
JLabel lable = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("tp.jpg");
ImageIcon imageIcon = new ImageIcon(url);
lable.setIcon(imageIcon);
lable.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(lable);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(500, 500, 800, 800);
}
public static void main(String args[]) {
new ImageIconDemo();
}
}
package lesson5;
import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.*;;
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));
JPanel panel2 = new JPanel(new GridLayout(2,3));
JPanel panel3 = new JPanel(new GridLayout(1,2));
JPanel panel4 = new JPanel(new GridLayout(2,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"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
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"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String aegs[]) {
new JPanelDemo();
}
}
package 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("欢迎来到王者荣耀");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new JscrollDemo();
}
}
package lesson5;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo1 extends JFrame{
public JButtonDemo1() {
Container container = this.getContentPane();
//将一个图片变为图标
URL resource = JButtonDemo1.class.getResource("tp.jpg");
Icon icon = new ImageIcon(resource);
//将图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
button.setSize(30,80);
container.add(button);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new JButtonDemo1();
}
}
package lesson5;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo2 extends JFrame{
public JButtonDemo2() {
Container container = this.getContentPane();
//将一个图片变为图标
URL resource = JButtonDemo1.class.getResource("tp.jpg");
Icon icon = new ImageIcon(resource);
//单选框
JRadioButton radioButton1 = new JRadioButton("RadioButton1");
JRadioButton radioButton2 = new JRadioButton("RadioButton2");
JRadioButton radioButton3 = new JRadioButton("RadioButton3");
//由于单选框只能选择一个,分组,一个组中只能选一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1,BorderLayout.NORTH);
container.add(radioButton2,BorderLayout.CENTER);
container.add(radioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new JButtonDemo2();
}
}
package lesson5;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo3 extends JFrame{
public JButtonDemo3() {
Container container = this.getContentPane();
//将一个图片变为图标
URL resource = JButtonDemo1.class.getResource("tp.jpg");
Icon icon = new ImageIcon(resource);
//多选框
JCheckBox checkBox1 = new JCheckBox("checkBox1");
JCheckBox checkBox2 = new JCheckBox("checkBox2");
container.add(checkBox1,BorderLayout.NORTH);
container.add(checkBox2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new JButtonDemo3();
}
}
package lesson6;
import javax.swing.*;
import java.awt.*;
public class TestCoboboxDemo1 extends JFrame{
public TestCoboboxDemo1() {
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 TestCoboboxDemo1();
}
}
package lesson6;
import javax.swing.*;
import java.awt.*;
public class TestCoboboxDemo2 extends JFrame{
public TestCoboboxDemo2() {
Container container = this.getContentPane();
//生成列表内容
String[] contents = {"1","2","3"};
//列表中需要放入内容
JList jList = new JList(contents);
container.add(jList);
this.setVisible(true);
this.setSize(500, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new TestCoboboxDemo2();
}
}
package lesson6;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestCoboboxDemo2 extends JFrame{
public TestCoboboxDemo2() {
Container container = this.getContentPane();
Vector contents = new Vector();
//列表中需要放入内容
JList jList = new JList(contents);
contents.add("张三");
contents.add("李四");
contents.add("王二");
container.add(jList);
this.setVisible(true);
this.setSize(500, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new TestCoboboxDemo2();
}
}
package lesson6;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo1 extends JFrame{
public TestTextDemo1() {
Container container = this.getContentPane();
JTextField textField1 = new JTextField("Hello");
JTextField textField2 = new JTextField("World");
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new TestTextDemo1();
}
}
package lesson6;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo2 extends JFrame{
public TestTextDemo2() {
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 TestTextDemo2();
}
}
package 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("欢迎来到王者荣耀");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new JscrollDemo();
}
}