Java(图形用户界面)GUI

╰+哭是因爲堅強的太久メ 2024-03-29 14:07 189阅读 0赞

目录

基本了解

容器和组件

首层容器

常用组件

1、组件与容器的关系

2、组件的类型有哪些常用的类型

所有组件创建的基本步骤:

API


基本了解

GUI发展阶段分两个:

  • “awt”技术,第一阶段的技术都放在了java.awt包中。主要包含了:
  1. 带有外观的容器和组件类;比如:窗体、按钮、文本框….
  2. 帮助实现特效的工具类;比如:颜色类、字体类、图片图像类….
  3. 对于各种动作事件的处理类;比如:点击事件、鼠标移动、键盘事件….

但是,awt的问题出在了带外观的容器和组件类上。它的外观容器和组件的实现方式是偷懒了的。比如:产生一个按钮对象,那么它会直接到当前操作系统的图形库里面找操作系统定义的按钮设计,然后直接使用。所以出现了一个问题:不同操作系统下的图形库,同样的容器或组件在设计的时候标准是不同的。这就导致同样的一段JavaGUI的代码在不同的操作系统下可能会出现不同的展示效果。这与Java设计的跨平台特性冲突了。

  • “swing”技术,在此阶段,Java重新设计了带有外观的容器和组件类,这些类放在了javax.swing包当中。(eXtend拓展)

所以在jdk当中同一个容器或组件就有两个类,比如按钮;

java.awt.Button

javax.swing.JButton

类名的区别就是swing当中容器或组件类 在类型前面有一个J

“swt”就是两个技术一起用


容器和组件

容器:装东西的 组件:装在里面的东西

首层容器就是GUI界面最外层的容器

中间容器是它所属的外部容器的组件,但是它里面又可以放子容器或组件。在设计复杂界面的时候,这种容器嵌套关系,可以帮助我们把复杂问题划分为一个个的简单问题。

组件就不能再放东西了,它自己就是最底层的一个部件。比如:按钮、文本框…


首层容器

JFrame;常用!他提供了我们常见的窗体样式。如:标题栏、最大化、最小化、关闭按钮…

JDialoge;较常用,弹窗

JWindow;提供空白窗口

JApplet;被淘汰的历史遗留技术;


常用组件

1、组件与容器的关系

所有的子容器和组件,与父容器的关系都应该是has-a关系;也就是说的子容器和组件都应该是父容器的属性。

2、组件的类型有哪些常用的类型

标签 — JLable

注意:主要是放置文字或图片。标签还可以 作为 中间容器用。

文本框 — JTextField

密码框 — JPasswordField

按钮 — JButton

单选框 — JRadionButton

复选框 — JCheckBox

下拉列表 — JComboBox

文本域 — JTextArea

所有组件创建的基本步骤:

s1、new 出组件对象;— 通用

s2、设置组件的内容和样式;

s3、设置组件的大小和位置;— 空布局下,通用setBounds()

s4、把组件放入容器当中。— 通用容器.add(组件)

API

  1. private Container contentP; //内容面板
  2. private JLabel 变量名; // 文字/图片 标签
  3. private JTextField nameTxt; //文本框
  4. private JPasswordField pwdTxt; //密码框
  5. private JButton 变量名; // 文字/图片按钮 带图标的文字按钮
  6. private JRadioButton 变量名; //单选框
  7. private JCheckBox 变量名; //复选框
  8. private JComboBox<String> 变量名;// 下拉列表
  9. private JTextArea 变量名; //消息文本域
  10. 窗体 用的内容面板
  11. 无参构造(){
  12. this.setSize(300,500);//大小
  13. this.setLocationRelativeTo(null);//窗口位置为屏幕中央 (必须先设置size
  14. this.setTitle("标题");//设置标题栏
  15. this.setIconImage(Toolkit.getDefaultToolkit().createImage("img/hp.JPG"));//设置图标
  16. this.setResizable(false);//窗体大小 是否可变
  17. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭窗体即退出程序
  18. this.addContent();//调用方法(增加内容)
  19. this.setVisible(true);//显示
  20. }
  21. //获取真正放置组件
  22. private void addContent(){
  23. this.contentP = this.getContentPane();//获取窗体的内容面板
  24. this.contentP.setLayout(null);//设置空布局,即采用绝对定位的方式
  25. this.contentP.setBackground(Color.WHITE);//设置内容面板背景色
  26. //文字标签
  27. this.变量名 = new JLabel();
  28. this.变量名.setText("用户名:");//内容
  29. this.变量名.setFont(new Font("宋体",Font.PLAIN,24));//字体
  30. this.变量名.setForeground(new Color(20, 138, 180));//字体颜色
  31. this.变量名.setBorder(BorderFactory.createLineBorder(Color.BLACK));//边线
  32. this.变量名.setBounds(20,30,100,30);//位置
  33. this.contentP.add(this.变量名);//增加进内容面板
  34. //图片标签
  35. this.变量名 = new JLabel();
  36. ImageIcon image = new ImageIcon("img/aden.JPG");//设置图片自适应大小
  37. ImageIcon newImage = new ImageIcon(image.getImage().getScaledInstance(宽,高,Image.SCALE_DEFAULT));
  38. this.imgLab.setIcon(newImage);
  39. this.imgLab.setBounds(位置x,y,宽,高);
  40. this.contentP.add(this.imgLab);
  41. //文本框
  42. this.nameTxt = new JTextField();
  43. this.nameTxt.setFont(new Font("宋体",Font.ITALIC,16));
  44. this.nameTxt.setForeground(Color.RED);
  45. this.nameTxt.setBounds(230,30,180,40);
  46. this.contentP.add(this.nameTxt);
  47. //密码框
  48. this.pwdTxt = new JPasswordField();
  49. this.pwdTxt.setFont(new Font("宋体",Font.ITALIC,16));
  50. this.pwdTxt.setForeground(Color.RED);
  51. this.pwdTxt.setEchoChar('?');//密码框特有的设置显示字符
  52. this.pwdTxt.setBounds(230,80,180,40);
  53. this.contentP.add(this.pwdTxt);
  54. //文字按钮
  55. this.txtBtn = new JButton("确定");
  56. this.txtBtn.setFont(new Font("宋体",Font.ITALIC,16));
  57. this.txtBtn.setForeground(Color.RED);
  58. this.txtBtn.setBounds(30,130,80,30);
  59. this.contentP.add(this.txtBtn);
  60. //带图标的文字按钮
  61. this.txtImgBtn = new JButton("登陆");
  62. this.txtImgBtn.setIcon(new ImageIcon("img/hp.JPG"));//设置按钮默认图标
  63. this.txtImgBtn.setRolloverIcon(new ImageIcon("img/logo.gif"));//设置鼠标进入按钮后的图标
  64. this.txtImgBtn.setBounds(120,130,80,30);
  65. this.contentP.add(this.txtImgBtn);
  66. //图片按钮
  67. this.imgBtn = new JButton();
  68. this.imgBtn.setIcon(new ImageIcon("img/buttonClear.jpg"));
  69. this.imgBtn.setBounds(210,130,140,50);
  70. this.contentP.add(this.imgBtn);
  71. //单选框
  72. this.maleRad = new JRadioButton("男");
  73. this.femaleRad = new JRadioButton("女");
  74. //对同组的单选按钮进行逻辑分组
  75. //ButtonGroup是没有外观的。
  76. ButtonGroup genderGroup = new ButtonGroup();
  77. genderGroup.add(this.maleRad);
  78. genderGroup.add(this.femaleRad);
  79. this.maleRad.setSelected(true);//设置某个单选按钮默认被选中
  80. this.maleRad.setBounds(20,190,60,30);
  81. this.femaleRad.setBounds(90,190,60,30);
  82. this.contentP.add(this.maleRad);
  83. this.contentP.add(this.femaleRad);
  84. //复选框
  85. this.swimChk = new JCheckBox("游泳");
  86. this.footballChk = new JCheckBox("足球");
  87. this.basketballChk = new JCheckBox("篮球");
  88. this.footballChk.setSelected(true);
  89. this.basketballChk.setSelected(true);
  90. this.swimChk.setBounds(160,190,60,30);
  91. this.footballChk.setBounds(230,190,60,30);
  92. this.basketballChk.setBounds(300,190,60,30);
  93. this.contentP.add(this.swimChk);
  94. this.contentP.add(this.footballChk);
  95. this.contentP.add(this.basketballChk);
  96. //下拉列表
  97. this.classComb = new JComboBox<>(new String[]{"J190","J191","J192"});
  98. this.classComb.addItem("J193");
  99. this.classComb.addItem("J194");
  100. this.classComb.setSelectedIndex(3);//根据下标设置默认被选中的选项
  101. this.classComb.setSelectedItem("J192");//根据对象设置默认被选中的选项
  102. this.classComb.setEditable(false);//设置下拉列表可编辑
  103. this.classComb.setEnabled(false);//设置下拉列表不可用
  104. this.classComb.setBounds(20,240,100,30);
  105. this.contentP.add(this.classComb);
  106. //文本域
  107. this.msgArea = new JTextArea();
  108. this.msgArea.setFont(new Font("宋体",Font.BOLD,16));
  109. this.msgArea.setForeground(Color.BLUE);
  110. JScrollPane scrollPane = new JScrollPane(this.msgArea);//滚动面板
  111. scrollPane.setBounds(20,300,460,150);
  112. this.contentP.add(scrollPane);
  113. }
  114. //消息框
  115. JOptionPane.showMessageDialog(null,"文本");
  116. //输入框
  117. String inputName = JOptionPane.showInputDialog(null,"文本");
  118. //输入的字符串转int
  119. int c1=Integer.parseInt(inputName );
  120. if(c1==1){
  121. }else{
  122. }
  123. System.out.println(inputName);
  124. //确认框 -- 返回 0、1、2对应 Yes、No、取消
  125. int choice = JOptionPane.showConfirmDialog(null,"文本",“标题”
  126. JOptionPane.YES_NO_OPTION);
  127. 0 1 2取消 -1 x
  128. System.out.println(choice);

发表评论

表情:
评论列表 (有 0 条评论,189人围观)

还没有评论,来说两句吧...

相关阅读