java图形用户界面编程
Posted 落叶、心悲凉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java图形用户界面编程相关的知识,希望对你有一定的参考价值。
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.JCheckBox;
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;
public class MyFrame extends JFrame{
private Container contentP;//内容面板
private JLabel usernameLab;//标签
private JLabel imgLab;//标签
private JButton okBtn;//按钮
private JTextField inputTxt;//文本框
private JPasswordField passwordTxt;//密码框
private JComboBox classComb;//下拉框
private JRadioButton maleRad;//男性单选框
private JRadioButton femaleRad;//女性单选框
private JCheckBox swimCheck;//复选框
private JTextArea msgTxtArea;//文本域
public MyFrame(){
Toolkit tk = Toolkit.getDefaultToolkit();//得到工具类
this.setSize(500, 400);//设置大小
this.setLocation((int)(tk.getScreenSize().getWidth()-500)/2,
(int)(tk.getScreenSize().getHeight()-400)/2);//设置位置
this.setTitle("我的第一个窗体");//设置标题
this.setIconImage(tk.createImage("img/wtp_icon_x16.gif"));//设置图标文件
this.setResizable(false);//窗体大小不可变
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);//设置窗体关闭即程序结束
this.addContent();
this.setVisible(true);//设置窗体可见---写在最后
}
//本方法专门完成对内容面板及其里面组件的操作
private void addContent(){
this.contentP = this.getContentPane();//得到内容面板
this.contentP.setBackground(Color.WHITE);//设置内容面板的背景色
this.contentP.setLayout(null);//设置内容面板的布局管理器为空,这样我们给的组件位置、大小才会起作用
//组件的使用
//文字标签
this.usernameLab = new JLabel();//产生组件对象
this.usernameLab.setText("用户名:");//设置标签文本
this.usernameLab.setFont(new Font("隶书", Font.BOLD, 24));//设置文本字体
this.usernameLab.setForeground(new Color(203,86,220));//设置文本颜色
this.usernameLab.setSize(120, 50);
this.usernameLab.setLocation(100, 0);
//调试标签大小位置的特有代码--调完以后注释掉
// this.usernameLab.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.contentP.add(this.usernameLab);//代表把标签放入它所在的容器中
//图片标签
this.imgLab = new JLabel();
this.imgLab.setIcon(new ImageIcon("img/mypic.JPG"));//设置图片
this.imgLab.setSize(161,34);
this.imgLab.setLocation(230, 5);
this.contentP.add(this.imgLab);
//按钮
this.okBtn = new JButton();
this.okBtn.setText("确定");
this.okBtn.setEnabled(false);//设置组件不可用
this.okBtn.setFont(new Font("微软雅黑",Font.PLAIN,18));
this.okBtn.setForeground(new Color(203,86,220));
this.okBtn.setBounds(0,60,100,40);
this.contentP.add(this.okBtn);
//文本框
this.inputTxt = new JTextField();
this.inputTxt.setFont(new Font("微软雅黑",Font.PLAIN,16));
this.inputTxt.setForeground(new Color(203,86,220));
this.inputTxt.setBounds(110, 60, 120, 30);
this.contentP.add(this.inputTxt);
//密码框
this.passwordTxt = new JPasswordField();
this.passwordTxt.setEchoChar(‘*‘);
this.passwordTxt.setFont(new Font("微软雅黑",Font.PLAIN,16));
this.passwordTxt.setForeground(new Color(203,86,220));
this.passwordTxt.setBounds(240, 60, 120, 30);
this.contentP.add(this.passwordTxt);
//下拉框
this.classComb = new JComboBox();
this.classComb.addItem("J130");
this.classComb.addItem("J131");
this.classComb.addItem("J132");
this.classComb.setSelectedIndex(2);
this.classComb.setEditable(true);//设置可编辑
this.classComb.setBounds(2, 110, 120, 30);
this.contentP.add(this.classComb);
//单选框
this.maleRad = new JRadioButton("男");
this.femaleRad = new JRadioButton("女");
this.maleRad.setSelected(true);//设置单选框被默认选中
this.maleRad.setBackground(Color.WHITE);
this.femaleRad.setBackground(Color.WHITE);
this.maleRad.setBounds(125, 110, 50, 30);
this.femaleRad.setBounds(180, 110, 50, 30);
this.contentP.add(this.maleRad);
this.contentP.add(this.femaleRad);
ButtonGroup bg = new ButtonGroup();
bg.add(this.maleRad);
bg.add(this.femaleRad);
//复选框
this.swimCheck = new JCheckBox("游泳");
this.swimCheck.setSelected(true);
this.swimCheck.setBounds(240, 110, 80, 30);
this.contentP.add(this.swimCheck);
//文本域--默认情况下没有外边框
this.msgTxtArea = new JTextArea();
this.msgTxtArea.setFont(new Font("微软雅黑",Font.PLAIN,16));
JScrollPane sp = new JScrollPane(this.msgTxtArea);
sp.setBounds(5, 150, 200, 200);
this.contentP.add(sp);
}
}
以上是关于java图形用户界面编程的主要内容,如果未能解决你的问题,请参考以下文章