Gui的核心技術(shù):Swing AWT
package com.laomao.lesson01;
import java.awt.*;
// GUi 的第一個界面
public class TestFrame {public static void main(String[] args) {// Frame
Frame frame = new Frame("我的第一個Java圖形界面窗口");
//需要設(shè)置可見性 w h
frame.setVisible(true);
//設(shè)置窗口大小
frame.setSize(400, 400);
// 設(shè)置背景顏色 Color
frame.setBackground(new Color(97, 71, 128));
// 彈出的初始位置
frame.setLocation(200, 200);
// 設(shè)置大小固定
frame.setResizable(false);
}
}
問題:無法關(guān)閉窗口。
package com.laomao.lesson01;
import java.awt.*;
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.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; // 可以存在多個窗口,我們需要一個計數(shù)器
public MyFrame(int x, int y, int w, int h, Color color){super("Myframe"+(++id));
setBackground(color);
setBounds(x, y, w, h);
setVisible(true);
}
}
解決了關(guān)閉問題
package com.laomao.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
// Panel 可以看成是一個空間,但是不能單獨存在
public class TestPanel {public static void main(String[] args) {Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//設(shè)置布局
frame.setLayout(null);
//坐標(biāo)
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(40, 160, 35));
// panel 設(shè)置坐標(biāo),相對于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(193,15,60));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
// 監(jiān)聽事件,監(jiān)聽窗口關(guān)閉事件 System.exit(0)
//適配器模式
frame.addWindowListener(new WindowAdapter() {//窗口點擊關(guān)閉時要做的事情
@Override
public void windowClosing(WindowEvent e) {System.exit(0);
}
});
}
}
package com.laomao.lesson01;
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");
//設(shè)置為流式布局
// 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(button3);
frame.add(button2);
frame.setVisible(true);
}
}
package com.laomao.lesson01;
import java.awt.*;
public class TestBorderLayout {public static void main(String[] args) {Frame frame = new Frame();
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 com.laomao.lesson01;
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(); //Java函數(shù) 選擇最優(yōu)布局
frame.setVisible(true);
}
}
package com.laomao.lesson01;
import java.awt.*;
public class Exercise {public static void main(String[] args) {Frame frame = new Frame();
frame.setLayout(new GridLayout(2,1));
frame.setVisible(true);
frame.setLocation(300,400);
frame.setBackground(Color.black);
frame.setSize(400,300);
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("Eask-1"),BorderLayout.EAST);
p1.add(new Button("Wesk-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);
for (int i = 0; i< 4; i++) {p4.add(new Button("for-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}
}
當(dāng)某個事件發(fā)生時,干什么?
package com.laomao.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) {// 按下按鈕,觸發(fā)一些事件
Frame frame = new Frame();
Button button = new Button();
// 因為 addActionListener()需要一個ActionListen,所以我們需要構(gòu)造7一個ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
windowClose(frame);
frame.setVisible(true);
}
// 關(guān)閉窗體的事件
private static void windowClose(Frame frame){frame.addWindowListener(new WindowAdapter() {@Override
public void windowClosing(WindowEvent e) {System.exit(0);
}
});
}
}
// 事件監(jiān)聽
class MyActionListener implements ActionListener{@Override
public void actionPerformed(ActionEvent e) {System.out.println("aaa");
}
}
多個按鈕,共享一個事件
package com.laomao.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionEvent02 {public static void main(String[] args) {// 兩個按鈕,實現(xiàn)同一個監(jiān)聽
// 開始 停止
Frame frame = new Frame("開始-停止");
Button button1 = new Button();
Button button2 = new Button();
//可以顯示的定義觸發(fā)會返回的命令,如果不顯示定義,這回走默認(rèn)的值。
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());
if (e.getActionCommand().equals("start")){
}
}
}
輸入框TextField監(jiān)聽package com.laomao.lesson02;
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);
// 監(jiān)聽這個文本框輸入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
// 按下enter 就會觸發(fā)這個輸入框的事件
textField.addActionListener(myActionListener2);
//設(shè)置替換編碼
textField.setEchoChar('*');
pack();
setVisible(true);
}
}
class MyActionListener2 implements ActionListener{@Override
public void actionPerformed(ActionEvent e) {TextField field = (TextField) e.getSource(); //獲得一些資源,返回了一個對象
System.out.println(field.getText()); // 獲得輸入框中的文本
field.setText("");
}
}
簡易計算器oop原則:組合,大于繼承!
package com.laomao.lesson02;
import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;
import javax.swing.*;
import javax.xml.ws.soap.Addressing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//簡易計算器
public class TestCalc {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));
// 1個標(biāo)簽
Label label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//監(jiān)聽器類
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. 獲得加數(shù)和被加數(shù)
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
// 2. 將這個值 + 法運算后,放到第三個框
num3.setText(""+(n1+n2));
// 3. 清除前兩個框
num1.setText("");
num2.setText("");
}
}
組合
class A extends B{}
classA{public B b;
}
package com.laomao.lesson02;
import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;
import javax.swing.*;
import javax.xml.ws.soap.Addressing;
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);
}
}
//監(jiān)聽器類
class MyCalculatorListener implements ActionListener{//獲取計算機(jī)這個對象,在一個類中組合另外一個類
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator){this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {// 1. 獲得加數(shù)和被加數(shù)
// 2. 將這個值 + 法運算后,放到第三個框
// 3. 清除前兩個框
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("");
}
}
完全改造為面向?qū)ο蟆緝?nèi)部類:更好的包裝】
package com.laomao.lesson02;
import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;
import javax.swing.*;
import javax.xml.ws.soap.Addressing;
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);
}
//監(jiān)聽器類
//內(nèi)部類大的好處,就是可以暢通無阻的訪問外部類的屬性和方法
private class MyCalculatorListener implements ActionListener{@Override
public void actionPerformed(ActionEvent e) {// 1. 獲得加數(shù)和被加數(shù)
// 2. 將這個值 + 法運算后,放到第三個框
// 3. 清除前兩個框
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
}
畫筆package com.laomao.lesson03;
import java.awt.*;
import java.util.Collections;
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);
// 養(yǎng)成習(xí)慣,畫筆用完,將他還原到最初的顏色
g.setColor(Color.black);
}
}
鼠標(biāo)監(jiān)聽目的:想要實現(xiàn)鼠標(biāo)畫畫
package com.laomao.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;
// 鼠標(biāo)監(jiān)聽事件
public class TestMouseListener {public static void main(String[] args) {new MyFrame("畫圖");
}
}
//自己的類
class MyFrame extends Frame{// 畫畫需要畫筆,需要監(jiān)聽鼠標(biāo)當(dāng)前的位置,需要集合來存儲這個點
ArrayList points;
public MyFrame(String title){super(title);
setBounds(200,200,400,300);
// 存鼠標(biāo)點擊的點
points = new ArrayList<>();
setVisible(true);
// 鼠標(biāo)監(jiān)聽器,針對這個窗口
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g) {// 畫畫,監(jiān)聽鼠標(biāo)的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){Point point = (Point) iterator.next();
g.setColor(Color.blue);
g.fillOval(point.x,point.y,10,10);
}
}
// 添加一個點到界面上
public void addPoint(Point point){points.add(point);
}
private class MyMouseListener extends MouseAdapter{//鼠標(biāo) 按下,彈起,按住不放
@Override
public void mousePressed(MouseEvent e) {MyFrame frame = (MyFrame) e.getSource();
//這個我們點擊的時候,就會在界面上產(chǎn)生一個點!畫
frame.addPoint(new Point(e.getX(), e.getY()));
// 每次點擊鼠標(biāo)都需要重新畫一遍
frame.repaint();//刷新
}
}
}
package com.laomao.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(100,100,200,200);
setVisible(true);
// addWindowListener(new MyWindowListener());
this.addWindowListener(
new WindowAdapter() {//激活窗口
@Override
public void windowActivated(WindowEvent e) {System.out.println("windowActivated");
}
// 關(guān)閉窗口
@Override
public void windowClosing(WindowEvent e) {System.out.println("windowClosing");
System.exit(0);
}
}
);
}
}
鍵盤監(jiān)聽package com.laomao.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(1, 2, 300, 400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {// 鍵盤按下
@Override
public void keyPressed(KeyEvent e) {// 獲得鍵盤下的鍵是哪一個
int KeyCode = e.getKeyCode();
System.out.println(KeyCode); // 不需要去記錄這個數(shù)值,直接使用靜態(tài)屬性 VK_xxx
if (KeyCode == KeyEvent.VK_UP) {System.out.println("你按下了上鍵");
}
// 根據(jù)按下不同操作,產(chǎn)生不同的結(jié)果
}
});
}
}
Swing
窗口、面板package com.laomao.lesson;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
public class JFrameDemo {// init();初始化
public void init(){// 頂級窗口
JFrame jf = new JFrame("這是一個JFrame窗口");
jf.setVisible(true);
jf.setBounds(100,100,200,200);
jf.setBackground(Color.BLUE);
//設(shè)置文字 Jlabel
JLabel label = new JLabel("歡迎來到孝義市玩耍!");
jf.add(label);
label.setHorizontalAlignment(SwingConstants.CENTER);
//容器實例化
Container container = jf.getContentPane();
container.setBackground(Color.blue);
//關(guān)閉事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {//建立一個窗口
new JFrameDemo().init();
}
}
彈窗package com.laomao.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.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 Label("好好學(xué)習(xí),天天向上"));
}
}
標(biāo)簽label
new JButton("xxx");
圖標(biāo)Icon
package com.laomao.lesson04;
import javax.swing.*;
import java.awt.*;
// 圖標(biāo),需要實現(xiàn)類,F(xiàn)rame繼承
public class IconDemo extends JFrame implements Icon {private int width;
private int height;
public IconDemo(){} //無參構(gòu)造
public IconDemo(int width, int height){this.width = width;
this.height = height;
} //有參構(gòu)造
public void init(){IconDemo iconDemo = new IconDemo(15,15);
// 圖標(biāo)放在標(biāo)簽上,也可以放在按鈕上!
JLabel label = new JLabel("icontest",iconDemo,SwingConstants.CENTER);
Container container = getContentPane();
container.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,height);
}
@Override
public int getIconWidth() {return this.width;
}
@Override
public int getIconHeight() {return this.height;
}
}
面板package com.laomao.lesson05;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {public JPanelDemo(){Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10)); // 后面的參數(shù)的意思,間距
JPanel panel = new JPanel(new GridLayout(1,3));
panel.add(new JButton("1"));
panel.add(new JButton("1"));
panel.add(new JButton("1"));
container.add(panel);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {new JPanelDemo();
}
}
按鈕package com.laomao.lesson05;
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("icon.jpg");
Icon icon = new ImageIcon(resource);
// 把這個圖標(biāo)放在按鈕上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("圖片按鈕");
// add
container.add(button);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,300);
}
public static void main(String[] args) {new JButtonDemo01();
}
}
package com.laomao.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {public JButtonDemo02(){Container container = this.getContentPane();
// 將一個圖片變?yōu)閳D標(biāo)
URL resource = JButtonDemo02.class.getResource("icon.jpg");
Icon icon = new ImageIcon(resource);
//單選框
JRadioButton jRadioButton01 = new JRadioButton("JRadioButton01");
JRadioButton jRadioButton02 = new JRadioButton("JRadioButton02");
JRadioButton jRadioButton03 = new JRadioButton("JRadioButton03");
// 由于單選框只能選擇一個,分組,一個組中只能選擇一個
ButtonGroup group = new ButtonGroup();
group.add(jRadioButton01);
group.add(jRadioButton02);
group.add(jRadioButton03);
container.add(jRadioButton01,BorderLayout.CENTER);
container.add(jRadioButton02,BorderLayout.NORTH);
container.add(jRadioButton03,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {new JButtonDemo02();
}
}
package com.laomao.lesson05;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo03 extends JFrame {public JButtonDemo03(){Container container = this.getContentPane();
//多選框
JCheckBox checkBox01 = new JCheckBox("checkBox01");
JCheckBox checkBox02 = new JCheckBox("checkBox02");
container.add(checkBox01,BorderLayout.NORTH);
container.add(checkBox02,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {new JButtonDemo03();
}
}
package com.laomao.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo01 extends JFrame {public TestComboboxDemo01(){
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在熱映");
status.addItem("已下架");
status.addItem("即將上映");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(500,300);
}
public static void main(String[] args) {new TestComboboxDemo01();
}
}
package com.laomao.lesson06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboboxDemo02 extends JFrame {public TestComboboxDemo02(){Container container = this.getContentPane();
// 生成列表的內(nèi)容
// String[] contents = {"1","2","3"};
Vector contents = new Vector();
//將內(nèi)容放入列表
JList jList = new JList(contents);
contents.add("Zhangsan");
contents.add("Lisi");
contents.add("Wangwu");
container.add(jList);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,350);
}
public static void main(String[] args) {new TestComboboxDemo02();
}
}
應(yīng)用場景:
package com.laomao.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {public TestTextDemo01(){Container container = this.getContentPane();
JTextField textField = new JTextField("hello");
JTextField textField2 = new JTextField("world", 20);
container.add(textField,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,350);
this.setVisible(true);
}
public static void main(String[] args) {new TestTextDemo01();
}
}
package com.laomao.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {public TestTextDemo02(){Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,350);
this.setVisible(true);
}
public static void main(String[] args) {new TestTextDemo02();
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
當(dāng)前標(biāo)題:GUI編程-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://www.rwnh.cn/article40/pdseo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、Google、小程序開發(fā)、ChatGPT、電子商務(wù)、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容