生活随笔
收集整理的這篇文章主要介紹了
java使用教程——组件及事件处理——常用组件与布局
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
常用組件:
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
); }
}
總結
以上是生活随笔為你收集整理的java使用教程——组件及事件处理——常用组件与布局的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。