GUI图形界面


package com.lovo.frame;


import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Toolkit;


import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


import com.lovo.bean.ClassBean;


public class MyFrame extends JFrame{
//内容面板---找窗体获取,不能new
private Container contentP;

//标签---JLabel---两种:放文字、放图像
private JLabel usernameLab;
private JLabel imgLab;

//文本框---JTextField
private JTextField inputTxt;

//密码框---JPasswordField
private JPasswordField pwdTxt;

//按钮
private JButton confirmBtn;
private JButton imageBtn;
private JButton txtImgeBtn;

//单选框---JRadionButton
//复选框---JCheckBox---可以自学
private JRadioButton maleBtn;
private JRadioButton femaleBtn;


//下拉列表---JComboBox
private JComboBox<String> comBox;
private JComboBox<ClassBean> classBox;

//文本域---JTextArea配合滚动条JScollBar
private JScrollPane jsp;
private JTextArea intrArea;


public MyFrame(){
this.setSize(500, 400);//设置窗体大小
Toolkit tk = Toolkit.getDefaultToolkit();//工具类
int screenW = (int)tk.getScreenSize().getWidth();//得到屏幕的宽
int screenH = (int)tk.getScreenSize().getHeight();//得到屏幕的高
this.setLocation((screenW - (int)this.getSize().getWidth())/2, 
(screenH - (int)this.getSize().getHeight()) / 2);//设置窗体的位置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认的关闭操作
this.setResizable(false);//设置不能改变窗体尺寸

this.setTitle("我的第一个窗体");//设置标题
this.setIconImage(tk.createImage("img/xiaoxin.GIF"));//设置标题图标

this.addContent();

this.setVisible(true);//把窗体绘制在屏幕上
}

//添加内容
private void addContent(){
//首先获取到内容面板
this.contentP = this.getContentPane();
//其次把布局管理器设置为空
this.contentP.setLayout(null);
this.contentP.setBackground(Color.WHITE);

this.usernameLab =new JLabel();
this.usernameLab.setText("用户名不能为空");
this.usernameLab.setFont(new Font("微软雅黑", Font.ITALIC, 16));
this.usernameLab.setForeground(new Color(174,159,232));
this.usernameLab.setBorder(BorderFactory.createLineBorder(Color.BLACK));//添加线边框
this.usernameLab.setBounds(20, 20, 120, 25);
this.contentP.add(this.usernameLab);


this.imgLab = new JLabel();
this.imgLab.setIcon(new ImageIcon("img/eye.JPG"));
this.imgLab.setBounds(150, 5, 298, 111);
this.contentP.add(this.imgLab);


this.inputTxt = new JTextField();
this.inputTxt.setText("J129");
this.inputTxt.setEditable(false);//设置是否可编辑
this.inputTxt.setFont(new Font("微软雅黑", Font.ITALIC, 20));
this.inputTxt.setForeground(new Color(174,159,232));
this.inputTxt.setBounds(50, 125, 150, 35);
this.contentP.add(this.inputTxt);


this.pwdTxt = new JPasswordField();
this.pwdTxt.setEchoChar('*');
this.pwdTxt.setFont(new Font("微软雅黑", Font.ITALIC, 20));
this.pwdTxt.setForeground(new Color(174,159,232));
this.pwdTxt.setBounds(250, 125, 150, 35);
this.contentP.add(this.pwdTxt);


this.confirmBtn = new JButton();//产生按钮对象
this.confirmBtn.setEnabled(true);//设置按钮是否被点击
this.confirmBtn.setText("确定");//对按钮对象的属性进行设置
this.confirmBtn.setBounds(50,165, 80, 30);//对按钮的大小位置进行设置
this.contentP.add(this.confirmBtn);//内容面板添加按钮


this.imageBtn = new JButton();
this.imageBtn.setIcon(new ImageIcon("img/button.JPG"));
this.imageBtn.setBounds(150, 165, 74, 21);
this.contentP.add(this.imageBtn);


this.txtImgeBtn = new JButton();
this.txtImgeBtn.setText("确定");
this.txtImgeBtn.setIcon(new ImageIcon("img/xiaoxin.GIF"));
this.txtImgeBtn.setRolloverIcon(new ImageIcon("img/hp.JPG"));
this.txtImgeBtn.setBounds(235, 165, 120, 30);
this.contentP.add(this.txtImgeBtn);

this.maleBtn = new JRadioButton();
this.femaleBtn = new JRadioButton();
ButtonGroup bg = new ButtonGroup();//按钮组--没有外观,其作用是把多个按钮在逻辑上进行分组
bg.add(this.maleBtn);
bg.add(this.femaleBtn);
this.maleBtn.setText("男");
this.maleBtn.setSelected(true);
this.femaleBtn.setText("女");
this.maleBtn.setBounds(360, 165, 50, 30);
this.femaleBtn.setBounds(430, 165, 50, 30);
this.contentP.add(this.maleBtn);
this.contentP.add(this.femaleBtn);


this.comBox = new JComboBox<String>();
this.comBox.addItem("J125");
this.comBox.addItem("J126");
this.comBox.addItem("J127");
this.comBox.addItem("J128");
this.comBox.addItem("J129");
this.comBox.setEditable(true);
this.comBox.setBounds(20, 205, 100, 25);
this.contentP.add(this.comBox);

this.classBox = new JComboBox<ClassBean>();
this.classBox.addItem(new ClassBean("J125",28));
this.classBox.addItem(new ClassBean("J126",30));
this.classBox.addItem(new ClassBean("J127",31));
this.classBox.addItem(new ClassBean("J128",29));
this.classBox.addItem(new ClassBean("J129",32));
this.classBox.setBounds(130, 205, 100, 25);
this.contentP.add(this.classBox);


this.intrArea = new JTextArea();
this.jsp = new JScrollPane(this.intrArea);
this.jsp.setBounds(100, 240, 300, 130);
this.contentP.add(this.jsp);

}

}








测试代码

package com.lovo.test;


import java.awt.Toolkit;
import javax.swing.JFrame;


import com.lovo.frame.MyFrame;
import com.lovo.jpanel.GuessFrame;


public class TestMain {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyFrame frame = new MyFrame();//产生窗体对象

}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java GUI图形界面编程是指使用Java编程语言来创建图形用户界面(Graphical User Interface,GUI)应用程序的过程。Java提供了一组丰富的类库和工具,使得开发人员可以快速、方便地创建交互性强、用户友好的应用程序。 Java GUI图形界面编程的主要组成部分是Swing和JavaFX两个库。Swing是一个基于Java的图形用户界面工具包,提供了丰富的用户界面组件,如按钮、文本框、表格等,可以在应用程序中创建和管理这些组件,实现用户界面的交互。JavaFX是Java平台上的富客户端应用程序框架,提供了更强大、更灵活的界面组件和布局控制,可以创建更加先进、更具吸引力的GUI应用程序。 使用Java GUI图形界面编程进行应用程序开发有许多优点。首先,Java GUI库提供了丰富的组件和控件,开发人员可以根据自己的需求选择合适的组件,快速构建出用户界面。其次,Java GUI库具有良好的跨平台性,可以在不同操作系统上运行,并且具有相同的用户界面。再者,Java提供了强大的事件驱动模型,使得应用程序能够响应用户的操作,实现交互性。 总之,Java GUI图形界面编程是一种灵活、方便、强大的开发方式,使得开发人员能够更加简单地创建出功能强大、用户友好的应用程序。无论是简单的桌面应用还是复杂的企业级应用,都可以通过Java GUI图形界面编程来实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值