java GUI編程(swing)之五swing面板,文本框,密碼框,標簽


面板組件(Jpanel)。一個界面只有一個JFrame,但可以有多個Jpanel 組件
而Jpanel組件上可以放置FlowLayout, BorderLayout,GridLayout組件,這樣的組合使用
達到比較復雜的布局效果
 
例如:
 
package gui;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** 面板組件的介紹
* Created by admin on 2017/7/2.
*/
public class MyJPanel extends JFrame{
// 定義需要用到的組件
private JButton jButton[] = new JButton[6];
private JPanel jPanel1, jPanel2;
public static void main(String[] args){
MyJPanel myJPanel = new MyJPanel();
}
// 初始化構造函數
public MyJPanel(){
// 創建兩個JPanel 面板 JPanel 默認是流式布局
jPanel1 = new JPanel();
jPanel2 = new JPanel();
// 創建按鈕組件
for (int i=0; i<jButton.length; i++){
jButton[i] = new JButton(String.valueOf(i));
}
// 添加組件到JPanel上
// jPanel1.add(jButton[0]);
jPanel1.add(jButton[1]);
jPanel1.add(jButton[2]);
jPanel2.add(jButton[3]);
jPanel2.add(jButton[4]);
jPanel2.add(jButton[5]);
// 把JPanel添加到JFrame 並指定位置
this.add(jPanel1, BorderLayout.NORTH);
this.add(jButton[0], BorderLayout.CENTER);
this.add(jPanel2, BorderLayout.SOUTH);
// 設置JFrame屬性
this.setTitle("面板JPanel的使用");
this.setLocation(500, 250);
this.setSize(350, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
 
JPanel注意事項:
JPanel也是屬於容器類組件,上面可以放置其他組件
JPanel上放置的組件默認的布局是流式布局FlowLayout
 
文件框組件(JTextField) 在awt包中
 
密碼框JPasswordField 在swing包中
 
使用標簽 + 文本框 + 密碼框 + 面板 做的登陸頁面
 
package gui;
import java.awt.GridLayout;
import java.awt.TextField;
import javax.swing.JPasswordField;
import javax.swing.*;
 
/** 文件框, 密碼框, 標簽的介紹
* 使用這幾個組件做一個登錄框
* Created by admin on 2017/7/2.
*/
public class MyTextField extends JFrame{
// 定義需要使用的控件, 按鈕2 個 面板3 個 標簽3
private JButton jButton, jButton2;
private JPanel jPanel, jPanel2, jPanel3;
private TextField textField;
private JLabel jLabel, jLabel2;
private JPasswordField jPasswordField;
public static void main(String[] args){
MyTextField myTextField = new MyTextField();
}
// 類的初始化,在初始化時創建控件
public MyTextField(){
// 創建面板組件
jPanel = new JPanel();
jPanel2 = new JPanel();
jPanel3 = new JPanel();
//創建標簽組件
jLabel = new JLabel("用戶名:");
jLabel2 = new JLabel("密 碼: ");
// 創建文本框組件 10 為寬度
textField = new TextField(10);
// 創建密碼框組件 10 為寬度
jPasswordField = new JPasswordField(10);
// 創建按鈕組件
jButton = new JButton("登陸");
jButton2 = new JButton("注冊");
// 設置JFrame的布局樣式為網格布局, 3行 1列
this.setLayout(new GridLayout(3, 1));
// 加入各個組件
// 用戶名標簽 + 文本框
jPanel.add(jLabel);
jPanel.add(textField);
// 密碼標簽 + 密碼框
jPanel2.add(jLabel2);
jPanel2.add(jPasswordField);
// 登陸 + 注冊按鈕
jPanel3.add(jButton);
jPanel3.add(jButton2);
// 將面板加入JFrame
this.add(jPanel);
this.add(jPanel2);
this.add(jPanel3);
// 設置JFrame屬性
this.setTitle("登陸");
this.setLocation(500, 250);
this.setSize(350, 200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM