GUI编程学习代码
这是我的在B站上的听课时,码的老师讲课的代码,没有过多的解释,有什么可以不懂得可以去看课。 之后的贪吃蛇之后再发,以及一个简单的画图板。 B站链接
1、简介
2、AWT
2.1、AWT简介
2.2、组件和容器
·Frame
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 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);
}
}
·Panel
解决了关闭事件
package lesson1
;
import java
.awt
.*
;
import java
.awt
.event
.WindowAdapter
;
import java
.awt
.event
.WindowEvent
;
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
.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);
}
});
}
}
2.3、布局管理器
·流式布局
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(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);
}
});
}
}
2.4、事件监听
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();
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());
}
}
2.5、输入框TextField监听
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();
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("");
}
}
2.6、简易计算器,组合与内部类
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("");
}
}
2.7、画笔
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);
}
}
2.8、鼠标监听
目的:想要实现鼠标画画!
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();
}
}
}
2.9、窗口监听
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!");
}
});
}
}
2.10、键盘监听
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("你按下了上键!");
}
}
});
}
}
3、Swing
3.1、窗口,面板
package lesson4
;
import javax
.swing
.*
;
import java
.awt
.*
;
public class JFrameDemo {
public void init() {
JFrame jFrame
= new JFrame("这是一个JFrame窗口");
jFrame
.setVisible(true);
jFrame
.setBounds(100,100,200,200);
jFrame
.setBackground(Color
.darkGray
);
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();
}
}
3.2、弹窗
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
);
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);
Container container
= this.getContentPane();
container
.setLayout(null
);
container
.setBounds(30, 30, 60, 60);
container
.add(new Label("真丑"));
}
}
3.3、标签
·label
new Jlable("XXX");
·图标ICON
package lesson4
;
import java
.awt
.*
;
import javax
.swing
.*
;
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();
}
}
·图片ICON
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();
}
}
3.4、面板
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();
}
}
·Jscroll
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("欢迎来到王者荣耀");
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();
}
}
3.5、按钮
·图片 -> 图标
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();
}
}
3.6、列表
·下拉框
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();
}
}
3.7、文本框
·文本框
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("欢迎来到王者荣耀");
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();
}
}