JavaGUI菜單欄與文本和密碼及文本域組件使用詳解
1.菜單欄
1.一級(jí)菜單欄
- 創(chuàng)建菜單條 JMenuBar
- 創(chuàng)建菜單 JMenu
- 創(chuàng)建菜單項(xiàng) JMenuItem
總結(jié):菜單項(xiàng)依附菜單、菜單依附菜單條
- 創(chuàng)建菜單欄
- 創(chuàng)建菜單
- 創(chuàng)建菜單項(xiàng)
- 菜單添加一個(gè)菜單項(xiàng)
- 菜單欄添加菜單
示例:
import javax.swing.*;
import java.awt.*;
public class Jmenu {
public static void main(String[] args){
JFrame jf=new JFrame("JMenu");
jf.setBounds(400,300,400,200);
jf.setLayout(new FlowLayout(FlowLayout.CENTER));
JMenuBar bar=new JMenuBar();
JMenu menu=new JMenu("菜單一");
JMenuItem item=new JMenuItem("選項(xiàng)1");
JMenuItem item2=new JMenuItem("選項(xiàng)2");
JMenuItem item3=new JMenuItem("選項(xiàng)3");
menu.add(item);
menu.add(item2);
menu.add(item3);
bar.add(menu);
jf.add(bar);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
2.多級(jí)菜單欄
- 創(chuàng)建菜單欄
- 創(chuàng)建多個(gè)菜單
- 創(chuàng)建多個(gè)菜單項(xiàng)
- 菜單添加多個(gè)菜單項(xiàng)
- 菜單欄添加菜單
import javax.swing.*;
import java.awt.*;
public class Jmenu {
public static void main(String[] args){
JFrame jf=new JFrame("JMenu");
jf.setBounds(400,300,400,200);
jf.setLayout(new FlowLayout(FlowLayout.CENTER));
JMenuBar bar=new JMenuBar();
JMenu menu=new JMenu("菜單一");
JMenu menu2=new JMenu("菜單二");
JMenu menu3=new JMenu("菜單三");
JMenuItem item=new JMenuItem("選項(xiàng)1");
JMenuItem item2=new JMenuItem("選項(xiàng)2");
JMenuItem item3=new JMenuItem("選項(xiàng)3");
JMenuItem item4=new JMenuItem("選項(xiàng)4");
JMenuItem item5=new JMenuItem("選項(xiàng)5");
JMenuItem item6=new JMenuItem("選項(xiàng)6");
menu.add(item);
menu.add(item2);
menu.add(item3);
menu2.add(item4);
menu3.add(item5);
menu3.add(item6);
bar.add(menu);
bar.add(menu2);
bar.add(menu3);
jf.add(bar);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}示例:



2.文本
類:JTextField
構(gòu)造函數(shù):
new JTextField();????
new JTextField(String text);//指定默認(rèn)文字
new JTextField(int fieldWidth);//指定文本框長(zhǎng)
new JTextField(String text,int fieldWidth);//指定默認(rèn)文字+指定文本框長(zhǎng)度
new JTextField(Documented docModel,String text, int fieldWidth);//指定文本框模式+指定默認(rèn)文字+指定文本框長(zhǎng)度
示例:
import javax.swing.*;
import java.awt.*;
public class JtextField {
public static void main(String[] args){
JFrame jf=new JFrame("JTextFiled");
jf.setLayout(new FlowLayout(FlowLayout.CENTER));
jf.setBounds(400,300,400,300);
JTextField text=new JTextField("請(qǐng)輸入",20);
jf.add(text);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
3.密碼框
類:JPasswordField
構(gòu)造函數(shù):
new JPasswordField();
????new JPasswordField(String text);//指定默認(rèn)文字
new JPasswordField(int fieldWidth);//指定文本框長(zhǎng)
new JPasswordField(String text,int fieldWidth);//指定默認(rèn)文字+指定文本框長(zhǎng)度
new JPasswordField(Documented docModel,String text, int fieldWidth);//指定文本框模式+指定默認(rèn)文字+指定文本框長(zhǎng)度
其他常用方法:
- setEchoChar("*");//設(shè)置回顯字符
示例:
import javax.swing.*;
import java.awt.*;
public class JpasswordField{
public static void main(String[] args){
JFrame jf=new JFrame("JTextFiled");
jf.setLayout(new FlowLayout(FlowLayout.LEFT));
jf.setBounds(400,300,400,300);
JLabel jl=new JLabel("密碼");
JPasswordField password=new JPasswordField("",20);
jf.add(jl);
jf.add(password);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
4.文本域
類:JTextArea
構(gòu)造函數(shù):
new JTextArea();
????new JTextArea(String text);//指定默認(rèn)文字
new JTextArea(int rows,int cols);//指定行 列
new JTextFieldArea(Documented docModel);//指定文本框模式
new JTextArea(Documented docModel,String text, int rows,int cols);//指定文本框模式+指定默認(rèn)文字+指定行 列
其他常用方法:
- setLineWrap();//設(shè)置文本域是否自動(dòng)換行
示例:
import javax.swing.*;
import java.awt.*;
public class JtextArea {
public static void main(String[] args) {
JFrame jf = new JFrame("JTextArea");
jf.setLayout(new FlowLayout(FlowLayout.LEFT));
jf.setBounds(400, 300, 400, 300);
JTextArea area=new JTextArea(20,10);
area.setLineWrap(true);
jf.add(area);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
到此這篇關(guān)于JavaGUI菜單欄與文本和密碼及文本域組件使用詳解的文章就介紹到這了,更多相關(guān)JavaGUI菜單欄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入了解Java8中的時(shí)區(qū)日期時(shí)間
Java?在?java.time?包中也提供了幾個(gè)類用于處理需要關(guān)注時(shí)區(qū)的日期時(shí)間?API,本文將通過簡(jiǎn)單的示例講講它們的用法,需要的可以參考一下2023-04-04
關(guān)于ThreadLocal和InheritableThreadLocal解析
這篇文章主要介紹了關(guān)于ThreadLocal和InheritableThreadLocal解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03

