一、Swing的组件
1.基础知识
几乎所有的GUI组件都来自于java.swing.JComponent组件就是放在GUI上的东西组件是可以嵌套的:将按钮放在框架(frame)或者面板(panel)上常见的组件:Text Field、button、scrollable list、radio button等
2.布局管理器
1)基础知识
是个与特定组件相关联的java对象,大多数是背景组件布局管理器用来控制所关联组件上携带的其他组件:某个框架上有面板,面板上有按钮,那么布局管理器控制按钮的大小与位置,框架的布局管理器控制面板的大小和位置
2)布局管理器的种类
<1>BorderLayout
将背景组件分割成五个区域,而且每个区域只能放上一个组件。分为东西南北中是框架默认的布局管理器将组件加入特定的区域
import java
.awt
.BorderLayout
;
import javax
.swing
.JButton
;
import javax
.swing
.JFrame
;
public class Button1 {
public static void main(String
[]args
){
Button1 gui
=new Button1();
gui
.go();
}
public void go(){
JFrame frame
=new JFrame();
JButton button1
=new JButton("Click Me!");
frame
.getContentPane().add(BorderLayout
.EAST
,button1
);
frame
.setSize(200, 200);
frame
.setVisible(true);
}
}
简单更改以上代码
public void go(){
JFrame frame
=new JFrame();
JButton button1
=new JButton("Click Me!Wow!you did it!");
frame
.getContentPane().add(BorderLayout
.EAST
,button1
);
frame
.setSize(200, 200);
frame
.setVisible(true);
}
会发现按钮的宽度变大,但BorderLayout布局管理器固定着按钮的高度尝试将按钮加入到框架的北方,就是上面
public void go(){
JFrame frame
=new JFrame();
JButton button1
=new JButton("Click Me!Wow!you did it!");
frame
.getContentPane().add(BorderLayout
.NORTH
,button1
);
frame
.setSize(200, 200);
frame
.setVisible(true);
}
此时按钮的高度是默认的,但是宽是与frame同样宽的。此时改变button的高度,可以利用改变按钮上的字体的大小实现
public void go(){
JFrame frame
=new JFrame();
JButton button1
=new JButton("Click Me!Wow!you did it!");
Font bigFont
=new Font("serif",Font
.BOLD
,28);
button1
.setFont(bigFont
);
frame
.getContentPane().add(BorderLayout
.NORTH
,button1
);
frame
.setSize(200, 200);
frame
.setVisible(true);
}
此时宽度不会改变,但是高度已经随着字体的改变而改变了在同时给东西南北中加组件的情况下,南北会首先占据位置,然后是东西,这样的话,东西方向的按钮就会扣除南北已经占据的位置,最后才轮到中间位置
public void go(){
JFrame frame
=new JFrame();
JButton south
=new JButton("south");
JButton north
=new JButton("north");
JButton east
=new JButton("east");
JButton west
=new JButton("west");
JButton center
=new JButton("center");
frame
.getContentPane().add(BorderLayout
.SOUTH
,south
);
frame
.getContentPane().add(BorderLayout
.NORTH
,north
);
frame
.getContentPane().add(BorderLayout
.EAST
,east
);
frame
.getContentPane().add(BorderLayout
.WEST
,west
);
frame
.getContentPane().add(BorderLayout
.CENTER
,center
);
frame
.setSize(200, 200);
frame
.setVisible(true);
}
<2>FlowLayout
每个组件都会按照理想的大小呈现,并且按照从左向右的顺序依次加入,有必要时可能会换行是面板默认的布局管理器但是在添加的过程还是用的BorderLayout
import java
.awt
.BorderLayout
;
import java
.awt
.Color
;
import javax
.swing
.JButton
;
import javax
.swing
.JFrame
;
import javax
.swing
.JPanel
;
public class Panel1 {
public static void main(String
[]args
){
Panel1 gui
=new Panel1();
gui
.go();
}
public void go(){
JFrame frame
=new JFrame();
JPanel panel
=new JPanel();
panel
.setBackground(Color
.darkGray
);
JButton button
=new JButton("click me");
panel
.add(button
);
frame
.getContentPane().add(BorderLayout
.EAST
,panel
);
frame
.setSize(200,200);
frame
.setVisible(true);
}
}
在此基础上再增加一个按钮,由于字体比较少的原因所以是横排 虽然理论上应该是在水平不够的情况下,按钮的位置会自动往下走,但在多次实践后发现并不会这样,会一直在水平方向上。
<3>BoxLayout
类似FlowLayout,但是它的排列顺序是自上往下,垂直排列。通过插入某种类似换行的机制来墙纸组件从新的一行开始排列
public void go(){
JFrame frame
=new JFrame();
JPanel panel
=new JPanel();
panel
.setBackground(Color
.darkGray
);
panel
.setLayout(new BoxLayout(panel
,BoxLayout
.Y_AXIS
));
JButton button
=new JButton("click me");
JButton button1
=new JButton("please click mea");
JButton button2
=new JButton("please click mea");
panel
.add(button
);
panel
.add(button1
);
panel
.add(button2
);
frame
.getContentPane().add(BorderLayout
.EAST
,panel
);
frame
.setSize(200,200);
frame
.setVisible(true);
}
3)通常用到的组件
<1>textFiled
import java
.awt
.BorderLayout
;
import java
.awt
.event
.ActionEvent
;
import java
.awt
.event
.ActionListener
;
import javax
.swing
.JButton
;
import javax
.swing
.JFrame
;
import javax
.swing
.JLabel
;
import javax
.swing
.JPanel
;
import javax
.swing
.JTextField
;
public class textFiled implements ActionListener{
JFrame frame
;
JTextField text
;
public static void main(String
[]args
){
textFiled gui
=new textFiled();
gui
.go();
}
public void go(){
frame
=new JFrame();
JPanel panel
=new JPanel();
JLabel label
=new JLabel("Dog'first name:");
text
=new JTextField(20);
text
.setText("Hello");
text
.addActionListener(this);
text
.selectAll();
text
.requestFocus();
panel
.add(label
);
panel
.add(text
);
frame
.getContentPane().add(BorderLayout
.CENTER
,panel
);
frame
.setSize(300,300);
frame
.setVisible(true);
}
public void actionPerformed(ActionEvent event
){
System
.out
.println(text
.getText());
}
}
<2>textArea
import java
.awt
.BorderLayout
;
import java
.awt
.event
.ActionEvent
;
import java
.awt
.event
.ActionListener
;
import javax
.swing
.JButton
;
import javax
.swing
.JFrame
;
import javax
.swing
.JPanel
;
import javax
.swing
.JScrollPane
;
import javax
.swing
.JTextArea
;
import javax
.swing
.ScrollPaneConstants
;
public class textArea1 implements ActionListener{
JTextArea text
;
public static void main(String
[]args
){
textArea1 gui
=new textArea1();
gui
.go();
}
public void go(){
JFrame frame
=new JFrame();
JPanel panel
=new JPanel();
JButton button
=new JButton("Just clicked it");
button
.addActionListener(this);
text
=new JTextArea(10,20);
text
.setLineWrap(true);
JScrollPane scroller
=new JScrollPane(text
);
scroller
.setVerticalScrollBarPolicy(ScrollPaneConstants
.VERTICAL_SCROLLBAR_ALWAYS
);
scroller
.setHorizontalScrollBarPolicy(ScrollPaneConstants
.HORIZONTAL_SCROLLBAR_NEVER
);
panel
.add(scroller
);
frame
.getContentPane().add(BorderLayout
.CENTER
,panel
);
frame
.getContentPane().add(BorderLayout
.SOUTH
,button
);
frame
.setSize(350, 300);
frame
.setVisible(true);
}
public void actionPerformed(ActionEvent ev
){
text
.append("button clicked \n");
}
}
<3>JList
import java
.awt
.BorderLayout
;
import javax
.swing
.JFrame
;
import javax
.swing
.JList
;
import javax
.swing
.JPanel
;
import javax
.swing
.JScrollPane
;
import javax
.swing
.ListSelectionModel
;
import javax
.swing
.ScrollPaneConstants
;
import javax
.swing
.event
.ListSelectionEvent
;
import javax
.swing
.event
.ListSelectionListener
;
public class list1 implements ListSelectionListener {
JList list
;
public static void main(String
[]args
){
list1 gui
=new list1();
gui
.go();
}
public void go(){
JFrame frame
=new JFrame();
JPanel panel
=new JPanel();
String
[]listEntries
={"alpha","beta","gama","delta","epsion","zeta","eta","theta"};
list
=new JList(listEntries
);
JScrollPane scroller
=new JScrollPane(list
);
list
.setVisibleRowCount(4);
list
.setSelectionMode(ListSelectionModel
.SINGLE_SELECTION
);
scroller
.setVerticalScrollBarPolicy(ScrollPaneConstants
.VERTICAL_SCROLLBAR_ALWAYS
);
scroller
.setHorizontalScrollBarPolicy(ScrollPaneConstants
.HORIZONTAL_SCROLLBAR_NEVER
);
panel
.add(scroller
);
list
.addListSelectionListener(this);
frame
.getContentPane().add(BorderLayout
.CENTER
,panel
);
frame
.setSize(350, 300);
frame
.setVisible(true);
}
public void valueChanged(ListSelectionEvent lse
){
if(!lse
.getValueIsAdjusting()){
String selection
=(String
)list
.getSelectedValue();
System
.out
.println(selection
);
}
}
}
<4>JCheckBox
import java
.awt
.BorderLayout
;
import java
.awt
.event
.ItemEvent
;
import java
.awt
.event
.ItemListener
;
import javax
.swing
.JCheckBox
;
import javax
.swing
.JFrame
;
import javax
.swing
.JList
;
import javax
.swing
.JPanel
;
import javax
.swing
.JScrollPane
;
import javax
.swing
.ListSelectionModel
;
import javax
.swing
.ScrollPaneConstants
;
import javax
.swing
.event
.ListSelectionEvent
;
public class check implements ItemListener {
JCheckBox check1
;
public static void main(String
[]args
){
check gui
=new check();
gui
.go();
}
public void go(){
JFrame frame
=new JFrame();
JPanel panel
=new JPanel();
check1
=new JCheckBox("Goes to 11");
panel
.add(check1
);
check1
.setSelected(false);
check1
.addItemListener(this);
frame
.getContentPane().add(BorderLayout
.CENTER
,panel
);
frame
.setSize(350, 300);
frame
.setVisible(true);
}
public void itemStateChanged(ItemEvent ev
){
String onOrOff
="off";
if(check1
.isSelected())onOrOff
="on";
System
.out
.println("check box is "+onOrOff
);
}
}