李兴华java开发实战经典---图形界面01

1.第一个JFrame窗体
public class JFrameDemo {

	public static void main(String[] args) {
		JFrame frame=new JFrame("第一个swing窗体");
		Dimension dimension=new Dimension();
		Point point=new Point(500,600);
		dimension.setSize(330,180);
		frame.setSize(dimension);
		frame.setLocation(point);
		frame.setBackground(Color.WHITE);
		frame.setVisible(true);
	}
}

结果
在这里插入图片描述

2.创建JLabel并设置字体,大小,图片
public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		Dimension dimension=new Dimension();
		Point point=new Point(500,600);
		dimension.setSize(330,180);
		frame.setSize(dimension);
		frame.setLocation(point);
		frame.setBackground(Color.WHITE);
		frame.setVisible(true);
		
		GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
		String fontName[]=ge.getAvailableFontFamilyNames();//获取系统可用字体
		
		for(int i=0;i<fontName.length;i++) {
			System.out.println(fontName[i]);//读出所有能用的字体
		}
		
		String picPath="d:"+File.separator+"sniper.jpg";
		File file=new File(picPath);
		InputStream input=null;
		byte[] bytes=new byte[(int)file.length()];
		try {
			input=new FileInputStream(file);
			input.read(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Icon icon=new ImageIcon(bytes);
		
		
		JLabel lab=new JLabel("我的label",icon,JLabel.CENTER);
		Font font=new Font("Serief",Font.ITALIC+Font.BOLD,28);
		
		lab.setFont(font);
		frame.add(lab);
		input.close();

	}

}

结果:
Yu Gothic Medium
Yu Gothic UI
Yu Gothic UI Light
Yu Gothic UI Semibold
Yu Gothic UI Semilight
ZWAdobeF
仿宋
华光中圆_CNKI
华光中楷_CNKI
华光中等线_CNKI
华光中长宋_CNKI
以上部分结果
在这里插入图片描述

3.创建JButton
public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		Dimension dimension=new Dimension();
		Point point=new Point(500,600);
		dimension.setSize(330,580);
		frame.setSize(dimension);
		frame.setLocation(point);
		frame.setBackground(Color.WHITE);
		frame.setVisible(true);
		
		String picPath="d:"+File.separator+"sniper.jpg";
		File file=new File(picPath);
		InputStream input=null;
		byte[] bytes=new byte[(int)file.length()];
		try {
			input=new FileInputStream(file);
			input.read(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Icon icon=new ImageIcon(bytes);

		JButton jButton=new JButton(icon);
		Font font=new Font("Serief",Font.BOLD,28);
		jButton.setFont(font);
		dimension.setSize(200,50);
		jButton.setSize(dimension);
		
		frame.add(jButton);

	}

}
4.创建布局管理器

流式布局管理器

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		frame.setLayout(new FlowLayout(FlowLayout.CENTER,3,3));
		
		JButton jButton=null;
		for (int i = 0; i < 9; i++) {
			jButton=new JButton("按钮-"+i);
			frame.add(jButton);
		}
		
		frame.setSize(300,380);
		frame.setVisible(true);

	}
}

结果
在这里插入图片描述
borderLayout布局管理器

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		frame.setLayout(new BorderLayout(3,3));
		
		frame.add(new JButton("东 east"),BorderLayout.EAST);
		frame.add(new JButton("西 west"),BorderLayout.WEST);
		frame.add(new JButton("南 south"),BorderLayout.SOUTH);
		frame.add(new JButton("北 north"),BorderLayout.NORTH);
		frame.add(new JButton("中 center"),BorderLayout.CENTER);
				
		frame.setSize(300,180);
		frame.setVisible(true);

	}
}

结果:
在这里插入图片描述
GridLayout布局管理器

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		frame.setLayout(new GridLayout(3,5,3,3));
		
		JButton jButton=null;
		for (int i = 0; i < 13; i++) {
			jButton=new JButton("按钮-"+i);
			frame.add(jButton);
		}
				
		frame.setSize(400,280);
		frame.setVisible(true);

	}
}

结果:
在这里插入图片描述
CardLayout布局管理器

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		CardLayout cardLayout=null;
		cardLayout=new CardLayout();
		frame.setLayout(cardLayout);
		
		Container container=frame.getContentPane();
		container.add(new JLabel("标签-A",JLabel.CENTER),"first");
		container.add(new JLabel("标签-B",JLabel.CENTER),"second");
		container.add(new JLabel("标签-C",JLabel.CENTER),"third");
		container.add(new JLabel("标签-D",JLabel.CENTER),"fourth");
		container.add(new JLabel("标签-E",JLabel.CENTER),"fifth");
		cardLayout.show(container, "third");
		frame.pack();
		
		frame.setSize(400,280);
		frame.setVisible(true);
		for (int i = 0; i < 4; i++) {
			try {
				Thread.sleep(3000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			cardLayout.next(container);
		}

	}
}

结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
绝对布局管理器

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		
		frame.setLayout(null);
		JLabel title=new JLabel("www.sina.com");
		JButton enter=new JButton("进入");
		JButton help=new JButton("帮助");
		
		title.setBounds(45,5,150,20);
		enter.setBounds(10,30,80,20);
		help.setBounds(100,30,80,20);

		frame.setSize(400,280);
		frame.add(title);
		frame.add(enter);
		frame.add(help);
		
		frame.setVisible(true);
	}
}

结果:
在这里插入图片描述

5.其他容器

JPanel容器

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		
		
		JPanel panel=new JPanel();
		panel.add(new JLabel("标签-A"));
		panel.add(new JLabel("标签-B"));
		panel.add(new JLabel("标签-C"));
		panel.add(new JButton("按钮-A"));
		panel.add(new JButton("按钮-B"));
		panel.add(new JButton("按钮-C"));
		
		frame.add(panel);
		frame.setSize(200,280);
		frame.pack();//设置组件自动调整大小
		frame.setLocation(300,200);
		frame.setVisible(true);
	}
}

结果:
在这里插入图片描述
JSplitPanel容器

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		Container container=frame.getContentPane();
		
		JPanel panel=new JPanel();
		JSplitPane lfsplit=null;//左右分割
		JSplitPane tpsplit=null;//上下分割
		
		lfsplit=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JLabel("左标签"), new JLabel("右标签"));
		lfsplit.setDividerSize(3);//设置分隔条大小
		tpsplit=new JSplitPane(JSplitPane.VERTICAL_SPLIT,lfsplit, new JLabel("下标签"));
		tpsplit.setDividerSize(3);//设置分隔条大小
		frame.pack();//设置组件自动调整大小

		container.add(tpsplit);
		
		frame.setSize(200,280);
		frame.pack();//设置组件自动调整大小
		frame.setLocation(300,200);
		frame.setVisible(true);
	}
}

结果:
在这里插入图片描述

JTablePanel容器

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		Container container=frame.getContentPane();
		
		JTabbedPane jTabbedPane=null;
		jTabbedPane=new JTabbedPane(JTabbedPane.TOP);
		JPanel panel1=new JPanel();
		JPanel panel2=new JPanel();
		
		JButton jButton=new JButton("按钮");
		JLabel label=new JLabel("标签");
		
		panel1.add(jButton);
		panel2.add(label);

		
		String picPath="d:"+File.separator+"sniper.jpg";
		
		jTabbedPane.addTab("图片选项", new ImageIcon(picPath), panel1,"图像");
		jTabbedPane.addTab("文字选项",panel2);
		container.add(jTabbedPane);
		
		
		frame.setSize(200,280);
		frame.pack();//设置组件自动调整大小
		frame.setLocation(300,200);
		frame.setVisible(true);
	}
}

结果:
在这里插入图片描述
JScollPane
利用其进行滚动条的设计

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		Container container=frame.getContentPane();
		String picPath="d:"+File.separator+"sniper.jpg";
		Icon icon=new ImageIcon(picPath);
		JPanel panel=new JPanel();
		JLabel label=new JLabel(icon);
		
		JScrollPane jScrollPane=null;
		//设置水平及垂直
		jScrollPane=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		
		panel.add(label);
		container.add(jScrollPane);
	
		frame.setSize(100,180);
		frame.pack();//设置组件自动调整大小
		frame.setLocation(300,200);
		frame.setVisible(true);
	}
}

结果:
在这里插入图片描述
内部窗体

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		Container container=frame.getContentPane();
		
		JDesktopPane jDesktopPane=null;
		jDesktopPane=new JDesktopPane();
		container.add(jDesktopPane,BorderLayout.CENTER);//设置显示样式
		container.add(new JLabel("内部窗体"),BorderLayout.SOUTH);
		
		//创建内部窗体
		JInternalFrame jInternalFrame=null;
		
		String picPath="d:"+File.separator+"sniper.jpg";
		Icon icon=new ImageIcon(picPath);

		JPanel jPanel=null;
		for (int i = 0; i < 3; i++) {
			jInternalFrame=new JInternalFrame("烽锐-"+i,true,true,true,true);
			jPanel=new JPanel();
			jPanel.add(new JLabel(icon));
			jInternalFrame.setLocation(35-i*10,35-i*10);
			jInternalFrame.add(jPanel);
			jInternalFrame.pack();
			jInternalFrame.setVisible(true);
			jDesktopPane.add(jInternalFrame);
			
		}
		
			
		frame.setSize(100,180);
		frame.pack();//设置组件自动调整大小
		frame.setLocation(300,200);
		frame.setVisible(true);
	}
}

结果:
在这里插入图片描述
JToggleButton

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		JToggleButton button1=new JToggleButton("已选中",true);
		JToggleButton button2=new JToggleButton("未选中");
		JToggleButton button3=new JToggleButton("按我");
		
		frame.setLayout(new GridLayout(3,1));
		frame.add(button1);
		frame.add(button2);
		frame.add(button3);
		
		frame.setSize(100,180);
		frame.pack();//设置组件自动调整大小
		frame.setLocation(300,200);
		frame.setVisible(true);

	}
}

在这里插入图片描述
JTextField

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		JTextField name=new JTextField(30);
		JTextField noed=new JTextField("hello",10);
		
		JLabel nameLabel=new JLabel("输入用户姓名:");
		JLabel noedLabel=new JLabel("不可编辑文本:");
		
		noedLabel.enable(false);//表示不可编辑
		
		frame.setLayout(new GridLayout(2,2));
		frame.add(noedLabel);
		frame.add(noed);
		
		frame.setSize(100,180);
		frame.pack();//设置组件自动调整大小
		frame.setLocation(300,200);
		frame.setVisible(true);

	}
}

结果:
在这里插入图片描述
密码框JPasswordField

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");
		JPasswordField jpf1=new JPasswordField();
		JPasswordField jpf2=new JPasswordField();

		jpf2.setEchoChar('#');//设置回显符号
		JLabel label1=new JLabel("默认的回显");
		JLabel label2=new JLabel("强制的回显");
		
		label1.setBounds(10,10,100,20);
		label2.setBounds(10,40,100,20);
		jpf1.setBounds(110,10,80,20);
		jpf2.setBounds(110,40,80,20);
		
		frame.setLayout(null);
		frame.add(label1);
		frame.add(jpf1);
		frame.add(label2);
		frame.add(jpf2);
		
		frame.setSize(100,180);
		frame.pack();//设置组件自动调整大小
		frame.setLocation(300,200);
		frame.setVisible(true);

	}
}

结果:
在这里插入图片描述
多行文本域:JTestArea
public class JFrameDemo {

public static void main(String[] args) throws IOException {
	JFrame frame=new JFrame("第一个swing窗体");

	JTextArea jta=new JTextArea(3,10);
	JLabel lab=new JLabel("多行文本域");
	lab.setBounds(10,10,120,20);
	jta.setBounds(130,10,150,100);
	
	frame.setLayout(null);
	frame.add(lab);
	frame.add(jta);
	
	frame.setSize(200,180);
	frame.pack();//设置组件自动调整大小
	frame.setLocation(300,200);
	frame.setVisible(true);

}

}
在这里插入图片描述
改进带滚动条的多行文本域

public class JFrameDemo {

	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("第一个swing窗体");

		JTextArea jta=new JTextArea(3,10);
		JLabel lab=new JLabel("多行文本域");
		JScrollPane src=new JScrollPane(jta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		
		
		
		frame.setLayout(new GridLayout(2,1));
		//src.add(jta);
		frame.add(lab);
		frame.add(src);
		
		frame.setSize(200,180);
		frame.pack();//设置组件自动调整大小
		frame.setLocation(300,200);
		frame.setVisible(true);

	}
}

在这里插入图片描述

发布了61 篇原创文章 · 获赞 1 · 访问量 913

猜你喜欢

转载自blog.csdn.net/weixin_43745804/article/details/104720621