天天看點

java圖形化界面開發_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);

}

}