GUI这个技术在企业中几乎用不到,但学习GUI编程,可以很好的学习和理解java面向对象编程的思想和代码的结构。swing是awt的优化版。下面,我主要用代码和注释的方式学习GUI编程。
学习GUI
1、AWT
1.1 AWT介绍
(1)包含很多类和接口。
(2)AWT含有元素:窗口、按钮、文本框。
(3)java.awt包。
1.2 组件和容器
(1)Frame
Frame f = new Frame("my first frame");
//设置窗口背景颜色
//f.setBackground(Color.lightGray);//使用设定好的颜色
//new Color(r,g,b);自定义颜色
f.setBackground(new Color(25, 38, 41));
/*//设置窗口坐标
f.setLocation(400,200);
//设置窗口大小
f.setSize(400,320);*/
//设置窗口坐标和大小
f.setBounds(400,200,400,320);
//设置窗口可见
f.setVisible(true);
//设置窗口不可拉伸
f.setResizable(false);
(2)panel面板
//面板可以看成一个空间,但是不能单独存在,要放到Frame里
public class TestPanel {
public static void main(String[] args) {
Frame fr = new Frame();
//new一个面板
Panel pa = new Panel();
//设置布局
fr.setLayout(null);
fr.setBounds(200,200,500,500);
fr.setBackground(new Color(89, 127, 208));
//设置面板,坐标相对于Frame
pa.setBounds(50,50,400,400);
pa.setBackground(new Color(118, 208, 85));
//将Panel 添加到Frame
fr.add(pa);
fr.setVisible(true);
fr.setResizable(false);
//添加监听窗口关闭事件,system.exit(0);
//适配器模式
fr.addWindowListener(new WindowAdapter() {
@Override
//窗口点击关闭时,需要做的事情
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
(3)布局管理器
-
流式布局
//创建Frame,设置坐标大小背景 Frame frame = new Frame("流式布局"); frame.setBounds(200,150,300,300); frame.setBackground(new Color(255, 29, 46)); //创建按钮几个Button Button button1 = new Button("a"); Button button2 = new Button("b"); Button button3 = new Button("c"); Button button4 = new Button("d"); //Frame设置成流式布局 //frame.setLayout(new FlowLayout());//默认居中 //frame.setLayout(new FlowLayout(1)); frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //将按钮加到Frame frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4);
-
边界布局
//创建Frame,设置坐标大小背景 Frame frame = new Frame("边界布局"); frame.setBounds(200,150,300,300); frame.setBackground(new Color(255, 29, 46)); //创建按钮几个Button Button button1 = new Button("east"); Button button2 = new Button("west"); Button button3 = new Button("south"); Button button4 = new Button("north"); Button button5 = new Button("center"); //将按钮加到Frame,设置东西南北中 frame.add(button1, BorderLayout.EAST); frame.add(button2, BorderLayout.WEST); frame.add(button3, BorderLayout.SOUTH); frame.add(button4, BorderLayout.NORTH); frame.add(button5, BorderLayout.CENTER);表格布局
Frame frame = new Frame("表格布局"); //创建按钮几个Button Button button1 = new Button("a"); Button button2 = new Button("b"); Button button3 = new Button("c"); Button button4 = new Button("d"); Button button5 = new Button("e"); Button button6 = new Button("f"); //Frame设置成表格布局 frame.setLayout(new GridLayout(3,2,10,10)); frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); frame.add(button6); //自动选择最优方式填充,frame不用设置大小 frame.pack(); //Frame可见 frame.setVisible(true);
练习布局:
public class LayoutExer {
public static void main(String[] args) {
//创建Frame,设置坐标大小背景
Frame frame = new Frame("练习布局");
frame.setBounds(200,200,400,300);
frame.setLayout(new GridLayout(2,1));
Panel panel1 = new Panel(new BorderLayout());//边界布局
Panel panel2 = new Panel(new GridLayout(2,1));//表格布局
Panel panel3 = new Panel(new BorderLayout());//边界布局
Panel panel4 = new Panel(new GridLayout(2,2));//表格布局
panel1.add(new Button("east1"),BorderLayout.EAST);
panel1.add(new Button("west1"),BorderLayout.WEST);
panel2.add(new Button("p2but1"));
panel2.add(new Button("p2but2"));
panel1.add(panel2,BorderLayout.CENTER);
panel3.add(new Button("east2"),BorderLayout.EAST);
panel3.add(new Button("west2"),BorderLayout.WEST);
for(int i=0;i<4;i++){
panel4.add(new Button("p4but"+i));
}
panel3.add(panel4,BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel3);
//Frame可见
frame.setVisible(true);
//添加窗口关闭监听
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
(4)事件监听
- 按钮监听
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button("pass");
Button b1 = new Button("b-start");
Button b2 = new Button("b-spot");
frame.setLayout(new FlowLayout());
frame.add(button);
frame.add(b1);
frame.add(b2);
button.addActionListener(new MyAction());
b1.addActionListener(new MyAction());
b2.addActionListener(new MyAction());
frame.pack();
frame.setVisible(true);
closeWindow(frame);
}
public static void closeWindow(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按了");
}
}
- 文本框监听
class MyFrame1 extends Frame{
public MyFrame1(){
TextField textField = new TextField();//单行文本
add(textField);
//监听文本框,按enter ,获得文本框信息
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获得资源,返回给对象
TextField f = (TextField) e.getSource();
//提取出文本框里的信息
System.out.println(f.getText());
//回车后,设置文本框清空
f.setText("");
}
});
textField.setEchoChar('*');//设置替换显示的信息
pack();
setVisible(true);
}
}
(5)简易计算器
- 原始代码
//简易计算器一代
public class TestCalculator1 {
public static void main(String[] args) {
new Calculator1();
}
}
//计算器类
class Calculator1 extends Frame{
public Calculator1(){
/*
3个文本框,一个按钮,一个标签*/
TextField t1 = new TextField(10);
TextField t2 = new TextField(10);
TextField t3 = new TextField(15);
Button button = new Button("=");
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(t1);
add(label);
add(t2);
add(button);
add(t3);
button.addActionListener(new MyCalculatorListener1(t1, t2, t3));
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener1 implements ActionListener{
//获取变量
public TextField t1,t2,t3;
public MyCalculatorListener1(TextField t1,TextField t2,TextField t3){
this.t1 = t1;
this.t2 = t2;
this.t3 = t3;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得两个加数
//getText()获取到的是String类型,通过包装类Intger转换
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
//运算,并将值放到第三个文本框
t3.setText(""+(n1+n2));
//清空
t1.setText("");
t2.setText("");
}
}
- 代码改进
//简易计算器
public class TestCalculator2 {
public static void main(String[] args) {
new Calculator2().loadFrame();
}
}
//计算器类
class Calculator2 extends Frame{
TextField t1,t2,t3;
public void loadFrame(){
/*3个文本框,一个按钮,一个标签*/
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(15);
Button button = new Button("=");
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(t1);
add(label);
add(t2);
add(button);
add(t3);
//添加监听
button.addActionListener(new MyCalculatorListener2(this));
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener2 implements ActionListener{
//在一个类中,组合另外一个类,获得计算器的对象
Calculator2 calculator2 = null;
public MyCalculatorListener2(Calculator2 calculator2){
this.calculator2 = calculator2;
}
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(calculator2.t1.getText());
int n2 = Integer.parseInt(calculator2.t2.getText());
calculator2.t3.setText(""+(n1+n2));
calculator2.t1.setText("");
calculator2.t2.setText("");
}
}
- 代码再改进,完全面向对象,内部类
//计算器类
class Calculator3 extends Frame{
TextField t1,t2,t3;
public void loadFrame(){
/*3个文本框,一个按钮,一个标签*/
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(15);
Button button = new Button("=");
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(t1);
add(label);
add(t2);
add(button);
add(t3);
//添加监听
button.addActionListener(new MyCalculatorListener3());
pack();
setVisible(true);
}
//监听器类
/*
内部类最大的好处,可以畅通无阻的访问外部的属性和方法。
内部类可以简化代码,公司里重复做的就是简化代码。
*/
private class MyCalculatorListener3 implements ActionListener{
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
t3.setText(""+(n1+n2));
t1.setText("");
t2.setText("");
}
}
}
(6)鼠标监听
//鼠标监听事件,用鼠标画点
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame3("画板");
}
}
class MyFrame3 extends Frame{
ArrayList points = null;
public MyFrame3(String title){
super(title);
setBounds(100,100,500,400);
//在这个窗口,添加鼠标监听器
addMouseListener(new MyMouseListener());
//用创建集合,用来存鼠标点击的点
points = new ArrayList<>();
setVisible(true);
}
@Override
public void paint(Graphics g) {
//画画
//用迭代器将ArrayList里的点迭代出来
Iterator it = points.iterator();
//当存在点时,就画下来
while(it.hasNext()){
Point p = (Point) it.next();//将指针指向下一个,返回给当前的点
g.setColor(Color.red);//设置点颜色
//填充一个圆形
g.fillOval(p.x, p.y, 10,10);
}
}
//点添加到集合
public void addPaint(Point pp){
points.add(pp);
}
//适配器模式
private class MyMouseListener extends MouseAdapter{
//鼠标---按下,弹起,按住不放,监听鼠标事件
@Override
public void mousePressed(MouseEvent e) {
//getSource()获取当前窗口的对象
MyFrame3 fr = (MyFrame3) e.getSource();
/*点击时,在界面上产生一个点
这个点是鼠标的点,监听鼠标当前位置,获得点坐标,将点实例化成对象
添加到集合*/
fr.addPaint(new Point(e.getX(), e.getY()));
//每次点击鼠标都需要重新画一遍,刷新
fr.repaint();
}
}
}
(7)窗口监听
public class TestWindowListener {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame(){
setBounds(100,100,400,350);
setVisible(true);
//使用匿名内部类
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("激活了");
}
});
}
}
(8)键盘监听
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(100,100,400,350);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//按下键盘
@Override
public void keyPressed(KeyEvent e) {
//e.getKeyCode()获取键盘码
if(e.getKeyCode() == KeyEvent.VK_UP){
System.out.println("输入了上键");
}
}
});
}
}
2、Swing
2.1第一个窗口
public class JFrameDemo {
public static void main(String[] args) {
//实例化, 对象.方法
new MyJFrame().init();
}
}
class MyJFrame extends JFrame{
//init();初始化
public void init(){
//顶级窗口
this.setTitle("swing窗口");
this.setBounds(100,100,400,300);
this.setBackground(new Color(156, 219, 233));
this.setVisible(true);
//swing 的组件比awt多个“J”
JLabel la = new JLabel("让我居中");
this.add(la);
la.setHorizontalAlignment(SwingConstants.CENTER);
//窗口关闭事件
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
2.2标签
- JLabel
- Icon
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
setBounds(150,100,400,300);
setVisible(true);
//获取当前类同级资源的地址,返回给url
JLabel label = new JLabel("tubiao");
URL url = ImageIconDemo.class.getResource("lyf.jpg");
ImageIcon imageIcon = new ImageIcon(url);
//图标可以放在按钮或标签上
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
2.3面板
- 面板布局
public class JPanelDemo extends JFrame {
public JPanelDemo(){
this.setBounds(150,100,400,300);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = getContentPane();
container.setLayout(new GridLayout(2,2,10,10));
JPanel panel1 = new JPanel(new GridLayout(1,3,2,2));
JPanel panel2 = new JPanel(new GridLayout(2,2,2,2));
JPanel panel3 = new JPanel(new GridLayout(2,1,2,2));
JPanel panel4 = new JPanel(new GridLayout(2,3,2,2));
panel1.add(new Button("1.1"));
panel1.add(new Button("1.2"));
panel1.add(new Button("1.3"));
panel2.add(new Button("2.1"));
panel2.add(new Button("2.2"));
panel2.add(new Button("2.3"));
panel2.add(new Button("2.4"));
panel3.add(new Button("3.1"));
panel3.add(new Button("3.2"));
panel4.add(new Button("4.1"));
panel4.add(new Button("4.2"));
panel4.add(new Button("4.3"));
panel4.add(new Button("4.4"));
panel4.add(new Button("4.5"));
panel4.add(new Button("4.6"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
- 文本域
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea("input", 20,50);
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setBounds(150,100,400,300);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
2.4按钮
- 图标按钮
public class JButtonImageDemo extends JFrame {
public JButtonImageDemo() {
Container container = this.getContentPane();
//将图片变成图标,获取图片资源,将对象赋给图标
URL resource = JButtonImageDemo.class.getResource("lyf.jpg");
Icon icon = new ImageIcon(resource);
//在按钮上设置图标
JButton button = new JButton();
button.setIcon(icon);
container.add(button);
setBounds(100,200,400,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonImageDemo();
}
}
- 单选按钮与多选按钮
public class JButtonSingleMore extends JFrame {
public JButtonSingleMore() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2, 1, 10, 10));
JPanel panel1 = new JPanel(new GridLayout(3,1,3,3));
JPanel panel2 = new JPanel(new GridLayout(1,3,3,3));
//单选按钮JRadioButton
JRadioButton radioButton1 = new JRadioButton("1.1");
JRadioButton radioButton2 = new JRadioButton("1.2");
JRadioButton radioButton3 = new JRadioButton("1.3");
//在单选框中只能选一个,使用按钮分组
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
panel1.add(radioButton1);
panel1.add(radioButton2);
panel1.add(radioButton3);
container.add(panel1);
//多选按钮
JCheckBox checkBox1 = new JCheckBox("2.1");
JCheckBox checkBox2 = new JCheckBox("2.2");
JCheckBox checkBox3 = new JCheckBox("2.3");
panel2.add(checkBox1);
panel2.add(checkBox2);
panel2.add(checkBox3);
container.add(panel2);
setBounds(100,200,400,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
2.5列表
public DownBoxDemo() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2, 1, 10, 10));
JPanel panel1 = new JPanel(new GridLayout(3,1,3,3));
JPanel panel2 = new JPanel(new GridLayout(3,2,3,3));
//下拉框
JComboBox box = new JComboBox();
box.addItem("AAA");//第一个可以是null
box.addItem("BBB");
box.addItem("CCC");
box.addItem("SSS");
panel1.add(box);
container.add(panel1);
//列表框
//动态数组生成列表的内容
Vector vector = new Vector();
vector.add("aaaaa");
vector.add("bbbbb");
vector.add("ccccc");
//将vector放入列表JList
JList jList = new JList(vector);
container.add(jList);
setBounds(100,200,400,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
2.6文本框
public TextBoxDemo() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(3, 1, 10, 10));
JPanel panel1 = new JPanel(new GridLayout(2,2,3,3));
JPanel panel2 = new JPanel(new GridLayout(2,2,3,3));
JPanel panel3 = new JPanel(new GridLayout(2,2,3,3));
//文本框
JTextField textField = new JTextField("lalalalala");
panel1.add(textField);
//文本域
JTextArea textArea = new JTextArea("input", 20,50);
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
panel2.add(scrollPane);
//密码框
JPasswordField passwordField = new JPasswordField();
panel3.add(passwordField);
container.add(panel1);
container.add(panel2);
container.add(panel3);
setBounds(100,200,400,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}