【JAVA语言程序设计基础篇】--图形用户界面基础--练习



exercise12_1 练习FLowLayout


package chapter12;

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class exercise12_1 extends JFrame{
	public exercise12_1(){
		setLayout(new FlowLayout(FlowLayout.LEFT,20,10));
		JPanel p1 = new JPanel ();
		p1.add(new JButton("button1"));
		p1.add(new JButton("button2"));
		p1.add(new JButton("button3"));
		
		JPanel p2 = new JPanel ();
		p2.add(new JButton("button4"));
		p2.add(new JButton("button5"));
		p2.add(new JButton("button6"));
		add(p1);
		add(p2);
	}
	public static void main(String[] args) {
		exercise12_1 frame = new exercise12_1();
		frame.setTitle("exercise12_1");
		frame.setSize(600, 100);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}



exercise12_2 练习BorderLayout

【JAVA语言程序设计基础篇】--图形用户界面基础--练习_第1张图片

package chapter12;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

@SuppressWarnings("serial")
public class exercise12_2 extends JFrame{
	public exercise12_2(){
		setLayout(new BorderLayout(50,50));
		JPanel p1 = new JPanel();
		p1.add(new JButton("button1"));
		p1.add(new JButton("button2"));
		p1.add(new JButton("button3"));
		
		JPanel p2 = new JPanel();
		p2.add(new JButton("button4"));
		p2.add(new JButton("button5"));
		p2.add(new JButton("button6"));
		
		add(p1,BorderLayout.CENTER);
		add(p2,BorderLayout.SOUTH);
	}
	public static void main(String[] args) {
		exercise12_2 frame = new exercise12_2();
		frame.setTitle("exercise12_2");
		frame.setSize(500, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
}


exercise12_3 练习GridLayout

【JAVA语言程序设计基础篇】--图形用户界面基础--练习_第2张图片

package chapter12;

import java.awt.GridLayout;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class exercise12_3 extends JFrame{
	public exercise12_3(){
		setLayout(new GridLayout(2,3,10,10));
		JPanel p1 = new JPanel();
		p1.add(new JButton("button1"));
		p1.add(new JButton("button2"));
		p1.add(new JButton("button3"));
		
		JPanel p2 = new JPanel();
		p2.add(new JButton("button4"));
		p2.add(new JButton("button5"));
		p2.add(new JButton("button6"));
		
		add(p1);
		add(p2);

	}
	public static void main(String[] args) {
		exercise12_3 frame = new exercise12_3();
		frame.setSize(600,200);
		frame.setTitle("diamond");
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

	}

}

erercise12_4

package chapter12;


import java.awt.GridLayout;
import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;

@SuppressWarnings("serial")
public class exercise12_4 extends JFrame {
	
	  public static void main(String[] args) {
		  exercise12_4 frame = new exercise12_4();
		  
		  frame.pack();//系统自动分配窗口size
		  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		  frame.setTitle("query");
		  frame.setLocationRelativeTo(null);
		  frame.setVisible(true);
	  }
	  public exercise12_4(){
		  setLayout(new BorderLayout(10,10));
		  JPanel p1 = new newPanel("button1","button2","button3");
		  JPanel p2 = new newPanel("button4","button5","button6");
		  setLayout(new BorderLayout());
		  add(p1,BorderLayout.NORTH);//BorderLayout使用记得加上方位
		  add(p2,BorderLayout.SOUTH);
	  }
}
@SuppressWarnings("serial")
class newPanel extends JPanel{
	public newPanel (String str1,String str2,String str3){
		setLayout(new GridLayout(2,2));
		add(new JButton(str1));
		add(new JButton(str2));
		add(new JButton(str3));
	}
}

【JAVA语言程序设计基础篇】--图形用户界面基础--练习_第3张图片


package chapter12;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

@SuppressWarnings("serial")
public class exercise12_5 extends JFrame{
	public exercise12_5(){
		JLabel str1 = new JLabel("department of computer science");
		JLabel str2 = new JLabel("SCHOOL OF computing");
		JLabel str3 = new JLabel("armstrong atlantic state univer sity");
		JLabel str4 = new JLabel("tel:920-64442");
		
		str1.setBorder(new LineBorder(Color.blue,5));
		str2.setBorder(new LineBorder(Color.BLACK,10));
		str3.setBorder(new LineBorder(Color.orange));
		str4.setBorder(new TitledBorder("aaa"));
		
		setLayout(new GridLayout(4,1,5,5));
		add(str1);
		add(str2);
		add(str3);
		add(str4);
		
	}
	public static void main(String[] args) {
		exercise12_5 frame = new exercise12_5();
		frame.pack();
		frame.setTitle("TAG");
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}

}

【JAVA语言程序设计基础篇】--图形用户界面基础--练习_第4张图片


Swing通用特性

package chapter12;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

@SuppressWarnings("serial")
public class exercise12_8 extends JFrame{
	public exercise12_8 (){
		JLabel jbt1 = new JLabel("black");
		JLabel jbt2 = new JLabel("blue");
		JLabel jbt3 = new JLabel("green");
		JLabel jbt4 = new JLabel("orange");
		JLabel jbt5 = new JLabel("magenta");
		JLabel jbt6 = new JLabel("yellow");
		
		jbt1.setForeground(Color.black);
		jbt2.setForeground(Color.blue);
		jbt3.setForeground(Color.green);
		jbt4.setForeground(Color.orange);
		jbt5.setForeground(Color.magenta);
		jbt6.setForeground(Color.yellow);
		
		jbt1.setBorder(new LineBorder(Color.yellow));
		jbt2.setBorder(new LineBorder(Color.yellow));
		jbt3.setBorder(new LineBorder(Color.yellow));
		jbt4.setBorder(new LineBorder(Color.yellow));
		jbt5.setBorder(new LineBorder(Color.yellow));
		jbt6.setBorder(new LineBorder(Color.yellow));
		
		Font font = new Font ("TimesRoman",Font.BOLD,20);
		
		jbt1.setToolTipText("black");
		jbt1.setFont(font);
		setLayout(new GridLayout(3,2,5,5));
		add(jbt1);
		add(jbt2);
		add(jbt3);
		add(jbt4);
		add(jbt5);
		add(jbt6);
	}
	public static void main(String[] args) {
		exercise12_8 frame = new exercise12_8();
		frame.pack();
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("aaa");
	}

}


【JAVA语言程序设计基础篇】--图形用户界面基础--练习_第5张图片

你可能感兴趣的:(Java_学习笔记)