GUI編程
組建
- 窗口
- 彈窗
- 面板
- 文本框
- 列表框
- 按鈕
- 圖片
- 監聽事件
- 鼠標
- 鍵盤事件
- 破解工具
1、簡介
GUI的核心技術:Swing AWT
為什么不流行?
- 界面不美觀。
- 需要jre環境。(沒必要為一個5M的小游戲下載幾百M的jre)
但是學了java的GUI編程,有助於了解MVC架構,了解監聽。
2、AWT
2.1、Awt介紹
- 包括很多類和接口
- 元素:窗口,按鈕,文本框
- java.awt
2.2、Frame
import java.awt.*;
//GUI的第一個程序
public class TestFrame {
public static void main(String[] args) {
// 不會看源碼
Frame frame = new Frame("我的第一個java圖形界面窗口");
// 需要設置可見性
frame.setVisible(true);
// 設置窗口大小
frame.setSize(400, 400);
// 設置背景顏色 Color
frame.setBackground(Color.yellow);
// 彈出的初始位置
frame.setLocation(300, 300);
// 設置大小固定
frame.setResizable(false);
}
}
進一步封裝
import java.awt.*;
//GUI的第一個程序
public class TestFrame {
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.yellow);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.red);
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);
}
}
2.3、面板Panel
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public abstract class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
// 設置布局
frame.setLayout(null);
// 坐標
frame.setBounds(300, 300, 200, 200);
frame.setBackground(new Color(168, 123, 82));
// panel設置坐標,相對於Frame
panel.setBounds(50,50,100,100 );
panel.setBackground(new Color(68, 52, 134));
frame.add(panel);
frame.setVisible(true);
// 監聽事件,監聽窗口關閉事件 System.exit(0)
// 適配器模式
frame.addWindowListener(new WindowAdapter() {
// 窗口點擊關閉時需要做的事情
@Override
public void windowClosing(WindowEvent e) {
// 結束程序
System.exit(0);
}
});
}
}
2.4、布局管理器
- 流式布局
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.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setSize(200,200);
// 把按鈕添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
- 東西南北中
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);
}
}
- 表格布局
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();
frame.setVisible(true);
}
}
總結:
- Frame是一個頂級窗口。
- Panel無法單獨顯示,必須添加到某個容器中。
- 布局管理器
- 流式
- 東西南北中
- 表格
- 流式
- 大小,定位,背景顏色,定位,監聽。
2.5、事件監聽
事件監聽:當某個事情發生的時候,干什么?
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");
}
}
多個按鈕共用一個監聽器
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) {
if(e.getActionCommand().equals("start")){
System.out.println("開始按鈕監聽");
}else{
System.out.println("停止按鈕監聽");
}
}
}
2.6、輸入框 TextField 監聽
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestTest01 {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
// 監聽這個文本框輸入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
// 按下enter 就會觸發這個輸入框的事件
textField.addActionListener(myActionListener2);
// 設置替換編碼
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField filed = (TextField) e.getSource(); //獲得的一些資源,返回的一個對象
System.out.println(filed.getText()); //獲得輸入框的文本
filed.setText(""); //回車清空
}
}
2.7、簡易計算器,組合+內部類回顧復習
oop原則:組合,大於繼承!
目前代碼:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextText02 {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame{
public Calculator(){
// 3個文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
// 1個按鈕
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(button);
add(num3);
pack();
setVisible(true);
}
}
//監聽器類
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("");
}
}
完全改造為面向對象寫法:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextText02 {
public static void main(String[] args) {
new Calculator().loading();
}
}
class Calculator extends Frame{
// 屬性
TextField num1, num2, num3;
// 方法
public void loading(){
num1 = new TextField(10);//字符數
num2 = new TextField(10);//字符數
num3 = new TextField(20);//字符數
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(this));
Label label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
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) {
// 1.獲得加數和被加數
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
// 2.將這個值 + 運算后,放到第三個框
calculator.num3.setText(""+(n1+n2));
// 3.清除前兩個框
calculator.num1.setText("");
calculator.num2.setText("");
}
}
內部類:
- 更好的包裝
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextText02 {
public static void main(String[] args) {
new Calculator().loading();
}
}
class Calculator extends Frame{
// 屬性
TextField num1, num2, num3;
// 方法
public void loading(){
num1 = new TextField(10);//字符數
num2 = new TextField(10);//字符數
num3 = new TextField(20);//字符數
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener());
Label label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
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());
// 2.將這個值 + 運算后,放到第三個框
num3.setText(""+(n1+n2));
// 3.清除前兩個框
num1.setText("");
num2.setText("");
}
}
}
2.8、畫筆
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loading();
}
}
class MyPaint extends Frame{
public void loading(){
setBounds(200,200,600,600);
setVisible(true);
}
// 畫筆
@Override
public void paint(Graphics g) {
// 畫筆,畫筆需要顏色,畫筆可以畫畫
g.setColor(Color.red);
g.drawOval(100,100,100,100);
// 養成習慣,畫筆用完,將他還原為最初的顏色
}
}
2.9、鼠標監聽
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
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,500,500);
points = new ArrayList<>();
// 鼠標監聽器,正對這個窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
@Override
public void paint(Graphics g) {
Iterator iterator = points.iterator();
while(iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.BLACK);
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.10、窗口監聽
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindowListener {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBounds(100,100,400,400);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("關閉窗口");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("激活窗口");
}
});
}
}
2.11、鍵盤監聽
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 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、窗口、面板
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);
// 設置文字
JLabel label = new JLabel("這是一段話");
jf.add(label);
label.setHorizontalAlignment(SwingConstants.CENTER);
// 獲得一個容器
Container contentPane = jf.getContentPane();
contentPane.setBackground(Color.yellow);
// 關閉窗口
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JFrameDemo().init();
}
}
3.2、彈窗
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.setBounds(100,100,600,600);
Container contentPane = this.getContentPane();
// 絕對布局
contentPane.setLayout(null);
contentPane.setBackground(Color.red);
JButton button = new JButton("點擊彈出一個對話框");
button.setBounds(50,50,50,50);
// 點擊這個按鈕時,彈出一個窗口
button.addActionListener(new ActionListener() { //監聽器
@Override
public void actionPerformed(ActionEvent e) {
new MyDialogDemo();
}
});
contentPane.add(button);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new DialogDemo();
}
}
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(500,500,500,500);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
contentPane.add(new JLabel("哈哈哈"));
}
}
3.3、標簽
label
new JLabel("xxx");
圖標icon
import javax.swing.*;
import java.awt.*;
public class IconDemo extends JFrame implements Icon {
private int width;
private int heigth;
public IconDemo(){}
public IconDemo(int width, int heigth){
this.width = width;
this.heigth = heigth;
}
public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container contentPane = this.getContentPane();
contentPane.add(label);
this.setVisible(true);
this.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,heigth);
}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return heigth;
}
}
圖片icon
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo() {
// 獲取圖片的地址
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(label);
}
}
3.4、面板
- JPanel
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container contentPane = this.getContentPane();
contentPane.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"));
contentPane.add(panel1);
contentPane.add(panel2);
contentPane.add(panel3);
contentPane.add(panel4);
this.setBounds(200,200,200,200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
- JScrollPanel
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container contentPane = this.getContentPane();
JTextArea jTextArea = new JTextArea();
jTextArea.setText("這是一個文本框");
// Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.5、按鈕
- 圖片按鈕
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01(){
Container contentPane = this.getContentPane();
// 將一個圖片變為圖標
URL resource = JButtonDemo01.class.getResource("tx.jpg");
Icon icon = new ImageIcon(resource);
// 把這個圖標放到按鈕上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("圖片按鈕");
contentPane.add(button);
this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
- 單選按鈕
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02(){
Container contentPane = this.getContentPane();
// 將一個圖片變為圖標
URL resource = JButtonDemo01.class.getResource("tx.jpg");
Icon icon = new ImageIcon(resource);
// 單選框
JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3");
// 由於單選框只能選擇一個,分組,一組中只能選擇一個
ButtonGroup group = new ButtonGroup();
group.add(jRadioButton1);
group.add(jRadioButton2);
group.add(jRadioButton3);
contentPane.add(jRadioButton1, BorderLayout.NORTH);
contentPane.add(jRadioButton2, BorderLayout.CENTER);
contentPane.add(jRadioButton3, BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
- 復選按鈕
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame {
public JButtonDemo03(){
Container contentPane = this.getContentPane();
// 將一個圖片變為圖標
URL resource = JButtonDemo01.class.getResource("tx.jpg");
Icon icon = new ImageIcon(resource);
// 多選框
JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
contentPane.add(jCheckBox1, BorderLayout.NORTH);
contentPane.add(jCheckBox2, BorderLayout.CENTER);
this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
3.6、列表
- 下拉框
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01(){
Container contentPane = this.getContentPane();
JComboBox<Object> status = new JComboBox();
status.addItem(null);
status.addItem("正在上映");
status.addItem("已下架");
status.addItem("即將上映");
contentPane.add(status);
this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
- 列表框
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02(){
Container contentPane = this.getContentPane();
// 生成列表的內容
Vector objects = new Vector();
objects.add("zhangsan");
objects.add("lisi");
objects.add("wangwu");
JList jList = new JList(objects);
contentPane.add(jList);
this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
- 應用場景
- 選擇地區,或者一些單個選項。
- 列表,展示信息,一般是動態擴容。
- 選擇地區,或者一些單個選項。
3.7、文本框
- 文本框
import javax.swing.*;
import java.awt.*;
public class TestTestDemo01 extends JFrame {
public TestTestDemo01(){
Container contentPane = this.getContentPane();
JTextField jTextField01 = new JTextField("hellow");
JTextField jTextField02 = new JTextField("world");
contentPane.add(jTextField01, BorderLayout.NORTH);
contentPane.add(jTextField02, BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTestDemo01();
}
}
- 密碼框
import javax.swing.*;
import java.awt.*;
public class TestTestDemo02 extends JFrame {
public TestTestDemo02(){
Container contentPane = this.getContentPane();
JPasswordField jPasswordField = new JPasswordField(); //默認已經是***了
jPasswordField.setEchoChar('*');
contentPane.add(jPasswordField);
this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTestDemo02();
}
}
- 文本域
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container contentPane = this.getContentPane();
JTextArea jTextArea = new JTextArea();
jTextArea.setText("這是一個文本框");
// Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
this.setVisible(true);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
