创建GUI四个步骤回顾
创建window(JFrame)。
JFrame frame = new JFrame();
创建组件。
JButton button = new JButton();
把组件加到frame上。
frame.getContentPane().add(BorderLayout.EAST, button);
显示出来。
frame.setSize(300,300); frame.setVisible(true);
布局管理器(Layout Managers)
布局管理器是个与特定组件相关联的Java对象,它大多数是背景组件。布局管理器用来控制所关联组件上携带的其他组件。也就是说,如果某个框架带有面板,而面板带有按钮,则面板的布局管理器控制着按钮的大小与位置,而框架的布局管理器则控制着面板的大小与位置。
携带就是加入到上面:
myPanel.add(button);
三个常用布局管理器
BordLayout布局五个区域
import javax.swing.*;
import java.awt.*;
public class Button1 {
public static void main(String[] args) {
Button1 gui = new Button1();
gui.go();
}
public void go() {
JFrame frame = new JFrame();
JButton button = new JButton("click me..............");
Font bigFont = new Font("serif", Font.BOLD,28);//改字体
button.setFont(bigFont);
frame.getContentPane().add(BorderLayout.NORTH, button);
frame.setSize(300,300);
frame.setVisible(true);
}
}
总的来说:南北可以控制高度,宽度一定;东西可以控制宽度,高度一定。中间只能捡剩下的。
FlowLayout布局组件的流向:依次从左至右,从上至下
JPanel的默认布局管理器是FlowLayout。
import javax.swing.*;
import java.awt.*;
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("shock me");
JButton button2 = new JButton("sleep");
JButton button3 = new JButton("go home");
frame.getContentPane().add(BorderLayout.EAST, panel);
panel.add(button);
panel.add(button2);
panel.add(button3);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
BoxLayout布局:可以指定组件排列方向
import javax.swing.*;
import java.awt.*;
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);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));//构造函数要知道管理哪个组件,和用哪个轴
JButton button = new JButton("shock me");
JButton button2 = new JButton("sleep");
JButton button3 = new JButton("go home");
frame.getContentPane().add(BorderLayout.EAST, panel);
panel.add(button);
panel.add(button2);
panel.add(button3);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
操作Swing组件
JTextField
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();
JPanel panel2 = new JPanel();
panel.setBackground(Color.darkGray);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));//构造函数要知道管理哪个组件,和用哪个轴
JButton button = new JButton("shock me");
JButton button2 = new JButton("sleep");
JButton button3 = new JButton("go home");
JTextField field = new JTextField(40);//构造JTextField,参数是字款
JLabel label = new JLabel("your name:");
frame.getContentPane().add(BorderLayout.NORTH, panel2);
frame.getContentPane().add(BorderLayout.EAST, panel);
panel.add(button);
panel.add(button2);
panel.add(button3);
panel2.add(label);
panel2.add(field);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
JTextArea
JTextArea可以有超过一行以上的文字。如果要让JTextArea滚动,则要将它粘在ScrollPane上。
import javax.swing.*;
import java.awt.*;
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();
JPanel panel2 = new JPanel();
panel.setBackground(Color.darkGray);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));//构造函数要知道管理哪个组件,和用哪个轴
JButton button = new JButton("shock me");
JButton button2 = new JButton("sleep");
JButton button3 = new JButton("go home");
JLabel label = new JLabel("your name:");
JTextArea text = new JTextArea(10, 20);//10行高,20字宽
JScrollPane scroller = new JScrollPane(text);//把text赋给JScrollpane
text.setLineWrap(true);//自动换行
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);//只垂直滚动
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel2.add(label);
panel2.add(scroller);
frame.getContentPane().add(BorderLayout.CENTER, panel2);
frame.getContentPane().add(BorderLayout.EAST, panel);
panel.add(button);
panel.add(button2);
panel.add(button3);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
一些常用方法
替换文字内容
text.setText("remove");
加入文字
text.append("append");
选取内容
text.selectAll();
把GUI目前焦点拉回到文本字段以便让用户输入操作
text.requestFocus();
JTextArea范例
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TextArea1 implements ActionListener {
JFrame frame;
JTextArea text;
public static void main(String[] args) {
TextArea1 gui = new TextArea1();
gui.go();
}
public void go() {
frame = new JFrame();
JPanel panel = new JPanel();
text = new JTextArea(10,20);
JScrollPane scroller = new JScrollPane(text);
text.setLineWrap(true);//自动换行
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);//只垂直滚动
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);
JButton button = new JButton("append sth.");
button.addActionListener(this);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.setSize(400,400);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ev) {
text.append("button clicked.\n");
}
}
JCheckBox
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CheckBox1 implements ItemListener {//监听item的事件(是否选取)
JCheckBox box;
JTextArea text;
public static void main(String[] args) {
CheckBox1 gui = new CheckBox1();
gui.go();
}
public void go () {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
text = new JTextArea(10,20);
box = new JCheckBox("go home");//创建JCheckBox
box.addItemListener(this);
JScrollPane scroller = new JScrollPane(text);
text.setLineWrap(true);//自动换行
scroller.setVerticalScrollBarPolicy 、(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);//只垂直滚动
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);
panel.add(box);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.setSize(400,400);
frame.setVisible(true);
}
public void itemStateChanged(ItemEvent ev) {
if (box.isSelected()) {//是否勾选
text.append("we go home!\n");
} else {
text.append("no no no.\n");
}
}
}
JList
import javax.swing.*;
import javax.swing.event.*;//ListSelectionListener在这里!
import java.awt.*;
public class List1 implements ListSelectionListener {
JTextField text;
JList list;
public static void main(String[] args) {
List1 gui = new List1();
gui.go();
}
public void go() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
text = new JTextField(20);
String [] listEntries = { "actor", "fireman", "police", "doctor", "lawer",
"nurse", "player", "dog" };
list = new JList(listEntries);
list.setVisibleRowCount(4);//设定显示的行数
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只能选一个
list.addListSelectionListener(this);//对选择事件注册
JScrollPane scroller = new JScrollPane(list);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);//只垂直滚动
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);
panel.add(text);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.setSize(400, 400);
frame.setVisible(true);
}
public void valueChanged(ListSelectionEvent lse) {
if (!lse.getValueIsAdjusting()) {//没有这个会得到两次事件
String selection = (String) list.getSelectedValue();//获得选择是个Object不一定是String
text.setText(selection);
}
}
}