常用组件:
1.JTextField(文本框)
允许用户在文本框中输入单行文本
2.JTextArea(文本区)
允许用户文本区中输入多行文本
3.JLabel(标签)
标签为用户提供信息
4.JButton(按钮)
允许用户单击按钮
5.JCheckBox(复选框)
为用户提供多种选择
6.JComboBox(下拉列表)
为用户提供单项选择
7.JPasswordField密码框)
允许用户在密码中输入单行密码
public class Example_3 {public static void main(String args[]){ComponentInWindow win =new ComponentInWindow();//创建一个对象win.setBounds(100,100,450,260);//设置窗口的初始位置win.setTitle("常用组件");//设置窗口的名字}}
import java.awt.FlowLayout;import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;public class ComponentInWindow extends JFrame{//四个组件JCheckBox checkBox1,checkBox2;//复选框 喜欢音乐和喜欢旅行JRadioButton radioM,radioF;//单选按钮 男女ButtonGroup group;//单选按钮建立一个小组 男女JComboBox<String> comBox;//下拉列表public ComponentInWindow()//构造函数{init();setVisible(true);//窗口可见setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击×号后的反应 关闭}void init(){setLayout(new FlowLayout());//流体布局comBox = new JComboBox<String>();下拉列表comBox.addItem("音乐天地");comBox.addItem("武术天地");checkBox1 = new JCheckBox("喜欢音乐");//复选框 喜欢音乐和喜欢旅行checkBox2 = new JCheckBox("喜欢旅行");this.add(checkBox1);this.add(checkBox2);//把复选框添加到窗口中去group = new ButtonGroup();//单选按钮 男女radioM = new JRadioButton("男");radioF = new JRadioButton("女");group.add(radioM);group.add(radioF);this.add(radioM);this.add(radioF);//把单选按钮添加到窗口中去this.add(comBox);}
}
密码框:
Java中使用JPasswordField是很常见的,怎么获取其文本内容呢?
查看Java API发现有个getPassword()方法,但使用这个方法得到的
却不是我们所输入的内容,可以通过以下方法获取密码框中的内容:
JPasswordField passwordField=new JPasswordField();
String password=new String(passwordField.getPassword());
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Text extends JFrame implements ActionListener
{JLabel lb=new JLabel("请输入密码:");///设置一个标签对象JPasswordField txt1=new JPasswordField(25);///创建密码框对象JButton bn=new JButton("确定");///添加按钮JTextField txt2=new JTextField(25);///创建一个文本框public Text() {setSize(300,200);setVisible(true);//可见setTitle("密码验证");setDefaultCloseOperation(EXIT_ON_CLOSE);setLayout(new FlowLayout());add(lb);add(txt1);txt1.setEchoChar('*');///密码框中的字符加密,以'*'形式显示add(bn);add(txt2);validate();bn.addActionListener(this);//为按钮添加注册监视器}public void actionPerformed(ActionEvent e) {String password=new String(txt1.getPassword());if(password.equals("abc"))///比较字符串相等txt2.setText("密码正确");else {txt2.setText("密码错误");} }public static void main(String[] args) {new Text();}}
密码:abc
常用容器:(中间容器)
1.JPanel(面板)
2.JTabbedPane(选项卡窗格)
3.JScrollPane(滚动窗格)
4.JSplitPane(拆分窗格)
5.JLayerPane(分层窗格)
常用布局:
1.FlowLayout布局(流式布局)
2.BorderLayout布局(边界布局)
3.CardLayout布局(卡片布局)
4.GridLayout布局(网格布局)
5.null布局(空布局)
6.BoxLayout布局(盒式布局)
public class Example9_4 {public static void main(String args[]) {new ShowLayout();}}
import java.awt.*;
import javax.swing.*;
public class ShowLayout extends JFrame {PanelGridLayout pannelGrid; //网格布局的面板PanelNullLayout panelNull ; //空布局的面板JTabbedPane p; //选项卡窗格ShowLayout() {pannelGrid = new PanelGridLayout();panelNull = new PanelNullLayout();p = new JTabbedPane();p.add("网格布局的面板",pannelGrid);p.add("空布局的面板",panelNull);add(p,BorderLayout.CENTER);add(new JButton("窗体是BorderLayout布局"),BorderLayout.NORTH);add(new JButton("南"),BorderLayout.SOUTH);add(new JButton("西"),BorderLayout.WEST);add(new JButton("东"),BorderLayout.EAST);setBounds(10,10,570,390);setVisible(true);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);validate();}}
import java.awt.*;
import javax.swing.*;
public class PanelGridLayout extends JPanel {PanelGridLayout () {GridLayout grid=new GridLayout(12,12); //网格布局setLayout(grid);Label label[][]=new Label[12][12];for(int i=0;i<12;i++) {for(int j=0;j<12;j++) {label[i][j]=new Label();if((i+j)%2==0)label[i][j].setBackground(Color.black);elselabel[i][j].setBackground(Color.white);add(label[i][j]); }}}
}
import javax.swing.*;
public class PanelNullLayout extends JPanel {JButton button;JTextField text;PanelNullLayout() {setLayout(null); //空布局button = new JButton("确定");text = new JTextField();add(text);add(button);text.setBounds(100,30,90,30);button.setBounds(190,30,66,30);}}
有两个列型盒式容器BoxVOne BoxVTwo和一个行型式容器BoxH
将BoxVOne,BoxVTwo添加到BoxH中,并在它们之间添加水平支撑
public class Example9_5 {public static void main(String args[]) {WindowBoxLayout win=new WindowBoxLayout();win.setBounds(100,100,310,260);win.setTitle("嵌套盒式布局容器");}}
import javax.swing.*;
public class WindowBoxLayout extends JFrame {Box boxH; //行式盒Box boxVOne,boxVTwo; //列式盒 public WindowBoxLayout() {setLayout(new java.awt.FlowLayout());init();setVisible(true);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}void init() {boxH =Box.createHorizontalBox();boxVOne=Box.createVerticalBox();boxVTwo=Box.createVerticalBox();boxVOne.add(new JLabel("姓名:"));boxVOne.add(new JLabel("职业:"));boxVTwo.add(new JTextField(10));boxVTwo.add(new JTextField(10));boxH.add(boxVOne);boxH.add(Box.createHorizontalStrut(10));boxH.add(boxVTwo);add(boxH); }
}