附加题_创建窗口组件添加

这次要求简单地应用一下java的一些图形用户界面编辑的组件类,布局~

虽然本次设计的结构有点乱,不过也总算把结果做出来了,要对类更加熟悉才能写出更好结构的程序~

 

下面是效果图:

 

下面是源代码:

 

 

// SevenBaseFrame.java

/**
 * 这是一个包含了七个基本组件的可视化界面图。
 * 使用GridLayout布局,然后,再定义七个JPanel变量,每个变量用于接收一个组件的布局;
 * 此外,这七个基本组件包括 检签组件、按钮组件、文本框组件、文本区组件、单选按钮组件、复选框、下拉列表组件。
 **/

import java.awt.*;
import javax.swing.*;
import java.lang.*;
import java.awt.event.*;
import java.util.*;
import java.awt.Container.*;


public class SevenBaseFrame extends JFrame
{
	public JPanel labelp;
	public JPanel buttonp;
	public JPanel textfieldp;
	public JPanel textAreap;
	public JPanel radiop;
	public JPanel checkboxp;
	public JPanel combop;

	public SevenBaseFrame(){
		Container sevenC = getContentPane();
		sevenC.setLayout(new GridLayout(4, 2, 10, 10)); // 设置总布局2行2列,行列间距都为10

		labelp = new JPanel();
		buttonp = new JPanel();
		textfieldp = new JPanel();
		textAreap = new JPanel();
		radiop = new JPanel();
		checkboxp = new JPanel();
		combop = new JPanel();

		JLl jlabel= new JLl(labelp);
		JB  jbutton = new JB(buttonp);
		JT  jtextfield = new JT(textfieldp);
		JTA jtextarea = new JTA(textAreap);
		JR jradio = new JR(radiop);
		Jcb jcheckbox = new Jcb(checkboxp);
		Jlb jcombo = new Jlb(combop);

		sevenC.add(labelp);
		sevenC.add(buttonp);
		sevenC.add(textfieldp);
		sevenC.add(textAreap);
		sevenC.add(radiop);
		sevenC.add(checkboxp);
		sevenC.add(combop);

		setExtendedState(JFrame.MAXIMIZED_BOTH);	// 全屏
		setVisible(true);							// 可见
		this.setTitle("图形用户界面组件");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	public static void main(String[] args){
		SevenBaseFrame test = new SevenBaseFrame();
	}


	public class JLl extends JFrame
	{
		private JLabel label;
		public JLl(JPanel p){
			label = new JLabel("显示文字的标签");	// 使用文本创建一个标签对象
			label.setFont(new Font("Serif", Font.PLAIN, 20));	// 设置标签字体
			label.setToolTipText("这是标签对象");		// 设置标签的工具提示
			p.add(label);					// 将标签对象添加到面板对象p上
		}
	}
	
	public class JB extends JFrame
	{
		private JButton button1, button2;

		public JB(JPanel p){
			button1 = new JButton("按钮 1");		// 创建按钮对象
			button1.setFont(new Font("Serif", Font.PLAIN, 20));
			
			ImageIcon img1 = new ImageIcon("picture1.png");
			ImageIcon img2 = new ImageIcon("picture2.png");
			button2 = new JButton("按钮2", img2);
			button2.setRolloverIcon(img1);
			button2.setFont(new Font("Serif", Font.PLAIN, 14));

			// 为组件注册监听器
			BHandler h = new BHandler();
			button1.addActionListener(h);
			button2.addActionListener(h);

			// 将各种组件添加到内容面板
			p.add(button1);
			p.add(button2);
		}
		private class BHandler implements ActionListener{
			public void actionPerformed(ActionEvent event){
				JOptionPane.showMessageDialog(JB.this, "你按了:" + event.getActionCommand());
			}
		}

	}

	public class JT extends JFrame
	{
		private JTextField t1, t2, t3;
		private JPasswordField k1;

		public JT(JPanel p){
			p.setLayout(new FlowLayout());
			p.setBackground(Color.YELLOW);

			t1 = new JTextField(10);					// 创建文本框对象,宽度为10
			t2 = new JTextField("请输入文本");			// 创建带有初始文本的文本框对象
			t2.setFont(new Font("Serif", Font.PLAIN, 12));
			t3 = new JTextField("不可编辑的文本框", 20);	// 创建带有初始文本的文本框对象,宽度为12
			t3.setFont(new Font("Serif", Font.PLAIN, 12));
			t3.setEditable(false);						// 设置该文本框为不可编辑状态
			k1 = new JPasswordField("口令", 10);		// 创建密码框

			p.add(t1);
			p.add(t2);
			p.add(t3);
			p.add(k1);
		}
	}

	public class JTA extends JFrame
	{
		private JTextArea ta1, ta2, ta3, ta4;

		public JTA(JPanel p){
			p.setLayout(new GridLayout(4, 1, 5, 5));
			ta1 = new JTextArea();		// 利用不同的构造方法创建文本区对象
			ta2 = new JTextArea(2, 8);
			ta3 = new JTextArea("3");
			ta4 = new JTextArea("4", 5, 10);

			ta1.setFont(new Font("Serif", Font.PLAIN, 12));
			ta2.setFont(new Font("Serif", Font.PLAIN, 12));
			ta3.setFont(new Font("Serif", Font.PLAIN, 12));
			ta4.setFont(new Font("Serif", Font.PLAIN, 12));

			ta1.setText("JTextArea1");			// setText()方法会将原来的内容清除
			ta2.append("JTextArea2");			// 将设置的字符串接在原来文本区文字之后
			ta4.setTabSize(10);					// 设置字体大小
			ta4.setLineWrap(true);				// 设置自动换行

			p.add(new JScrollPane(ta1));
			p.add(new JScrollPane(ta2));
			p.add(new JScrollPane(ta3));
			p.add(new JScrollPane(ta4));
		}
	}

	public class JR extends JFrame
	{
		private JPanel p1, p2;
		private JRadioButton red, green, blue;
		private ButtonGroup buttonGroup;

		public JR(JPanel p){
			Container c = getContentPane();
			p.add(c);
			p1 = new JPanel();		// 创建一个用来显示颜色的面板对角
			p1.setBackground(Color.RED);
			c.add(p1, BorderLayout.CENTER);

			buttonGroup = new ButtonGroup();	// 创建单选按钮选项
			red = new JRadioButton("红色", true);	// 创建单选按钮选项
			green = new JRadioButton("绿色");
			blue = new JRadioButton("蓝色");
			red.setFont(new Font("Serif", Font.PLAIN, 14));	// 设置字体
			green.setFont(new Font("Serif", Font.PLAIN, 14));	
			blue.setFont(new Font("Serif", Font.PLAIN, 14));	
			buttonGroup.add(red);
			buttonGroup.add(green);
			buttonGroup.add(blue);

			rHandler h = new rHandler();		// 创建监听器
			red.addItemListener(h);
			blue.addItemListener(h);
			green.addItemListener(h);

			p2 = new JPanel();
			p2.add(red);
			p2.add(green);
			p2.add(blue);
			c.add(p2, BorderLayout.SOUTH);
		}

		private class rHandler implements ItemListener{		// 类中类,类的嵌套
			public void itemStateChanged(ItemEvent event){
				if(red.isSelected()){
					p1.setBackground(Color.red);
				}
				else if(blue.isSelected()){
					p1.setBackground(Color.blue);
				}
				else{
					p1.setBackground(Color.green);
				}
			}
		}
	}


	public class Jcb extends JFrame
	{
		private JLabel label;
		private JCheckBox b, i;

		public Jcb(JPanel f){
			Container c = getContentPane();
			c.setLayout(new FlowLayout());
			c.setBackground(Color.YELLOW);
			f.add(c);
			label = new JLabel("请注意观察宋体文字的变化");
			label.setFont(new Font("Serif", Font.PLAIN, 20));
			c.add(label);

			b = new JCheckBox("粗体");		// 创建复选框
			i = new JCheckBox("斜体");
			b.setFont(new Font("Serif", Font.PLAIN, 16));
			i.setFont(new Font("Serif", Font.PLAIN, 16));
			b.setBackground(Color.YELLOW);
			i.setBackground(Color.YELLOW);

			CBHandler h = new CBHandler();		// 创建监听器
			b.addItemListener(h);				// 注册监听器
			i.addItemListener(h);
			c.add(b);
			c.add(i);
		}

		private class CBHandler implements ItemListener
		{
			private int vb = Font.PLAIN;
			private int vi = Font.PLAIN;
			
			public void itemStateChanged(ItemEvent e){
				if(e.getSource() == b) vb = b.isSelected() ? Font.BOLD: Font.PLAIN;
				if(e.getSource() == i) vi = i.isSelected() ? Font.ITALIC: Font.PLAIN;
				label.setFont(new Font("Serif", vb + vi, 20));
			}
		}
	}

	public class Jlb extends JFrame{
		private JComboBox lbk;
		private JLabel label;
		private String names[] = {"华南理工大学", "清华大学", "复旦大学"};

		public Jlb(JPanel f){
			Container c = getContentPane();
			c.setBackground(Color.YELLOW);
			f.add(c);
			lbk = new JComboBox(names);		// 创建下拉列表对象
			lbk.setMaximumRowCount(4);		// 设置下拉列表所能显示的列表项的最大数目
			lbk.setSelectedIndex(0);		// 设置默认的选择项
			lbk.setFont(new Font("Serif", Font.PLAIN, 14));	// 设置字体
			lbk.addItemListener(new lbHandler());			// 注册监听器

			label = new JLabel("你选择了:华南理工大学");
			label.setFont(new Font("Serif", Font.PLAIN, 14));
			c.add(lbk, BorderLayout.NORTH);
			c.add(label, BorderLayout.CENTER);
		}

		private class lbHandler implements ItemListener
		{
			public void itemStateChanged(ItemEvent e){
				if(e.getStateChange() == e.SELECTED){
					label.setText("你选择了:" + names[lbk.getSelectedIndex()]);
				}
			}
		}
	}


}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值