有两种
一:swing包内的图形界面类;二:awt包内图形界面类
14.1 swing包
14.1.1 窗口JFrame类
JFrame类就是窗口类,所有的面板JPanel、按钮JButton、文本JTextArea、标签JLabel等等都可以对象.add()进该JFrame类对象中;也可以通过extends JFrame来使当前类成为窗口,通过this.add()加入到窗口。两者都需要.setVisible(true)来设置可视。另外,还需要setLayout()设置JFrame布局,哪怕是设置null都可以。
//窗口关闭监听
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
14.1.2 面板JPanel类
JPanel面板类,所有的面板JPanel、按钮JButton、文本JTextArea、标签JLabel等等都可以对象.add()进该JPanel类对象中,然后通过将整个面板add进JFrame中。另外,还需要设置JFrame布局,哪怕是设置null都可以。
14.1.3 按钮JButton类
//两个按钮,一个提交,一个重置
JButton submit = new JButton("提交");
JButton reset = new JButton("重置");
//设置按钮的(x,y,width,height)
submit.setBounds(60, 120, 50, 30);
reset.setBounds(150, 120, 50, 30);
//提交按钮submit事件
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//判断账号密码正确与否都弹出相应的 提示框
if( user.getText().equals(USERNAME) && pass.getText().equals(PASSWORD)){
JOptionPane.showMessageDialog(jf, "登录成功");
}else{
JOptionPane.showMessageDialog(jf, "登录失败,请重新登录");
}
}
});
//重置按钮reset事件
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
user.setText("");
pass.setText("");
}
});
jf.add(submit);//将两个按钮直接加入JFrame中
jf.add(reset);
14.1.4 文本框JTextField 与文本域JTextArea
(1)文本框JTextField
//两个文本框
JTextField user = new JTextField();
JTextField pass = new JTextField();
//设置按钮的(x,y,width,height)
user.setBounds(60, 50, 150, 30);
pass.setBounds(60, 80, 150, 30);
//将两个文本框直接加入JFrame中
jf.add(user);
jf.add(pass);
(2)文本域JTextArea
//显示面板组件
JTextArea displayJTA = new JTextArea( 20, 30 );
//JTextArea 加入滚动组件
JScrollPane displayJSP = new JScrollPane(displayJTA);
ScrollBar displayBar = displayJSP.getVerticalScrollBar();//获取滚动条;
//将组件装入显示面板
displayJTA.setEditable(false);//设置只读
displayJTA.setLineWrap(true); //设置JTextArea中文本自动换行
displayJSP.doLayout();
displayJP.setLayout(new BorderLayout());
displayJP.add(displayJSP);
//不断将滚动条移动到最下
displayBar.setValue(displayBar.getMaximum());
14.1.5 标签JLabel类
//两个标签
JLabel userLabel = new JLabel("user:");
JLabel passLabel = new JLabel("pass:");
//设置按钮的(x,y,width,height)
userLabel.setBounds(20, 53, 35, 20);
passLabel.setBounds(20, 83, 35, 20);
jf.add(userLabel);
jf.add(passLabel);
14.1.6 菜单栏JMenuBar、菜单JMenu、菜单选项JMenuItem
//创建菜单栏
JMenuBar mb = new JMenuBar();
//创建菜单
JMenu mn= new JMenu("文件(F)");
//创建菜单选项,加入多个菜单选项
JMenuItem[] mt = new JMenuItem[]{
new JMenuItem("新建(N) Ctrl+N"),
new JMenuItem("打开(O)... Ctrl+O"),
new JMenuItem("保存(S) Ctrl+S"),
new JMenuItem("另存为(A)... Ctrl+A")
};
//将以上菜单选项加入菜单
for(JMenuItem m : mt){
mn.add(m);
}
//横线加入菜单
mn.addSeparator();
//菜单加入菜单栏
mb.add(mn);
//菜单栏加入JFrame窗口
jf.add(mb)
14.2 简易计算器
/**
* 模仿电脑的计算器
*/
public class Calculator extends JFrame{
public static void main(String[] args) {
new Calculator();
}
public Calculator(){
//创建CalculatorPanel面板,并加入到JFrame中
CalculatorPanel c = new CalculatorPanel();
this.add(c);
this.setTitle("简易计算器");
this.setSize(220, 280);
this.setVisible(true);
//窗口关闭监听
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
/***
* 1、面板内包括一个显示文本框,和一个装按钮的面板
* 2、按钮面板自定义了一个添加按钮方法,该方法传入按钮名字,监听事件
* 方法内实现按钮的创建,并加入按钮面板
* 3、自定义两种按钮点击事件:数字按钮点击事件、符号按钮点击事件
*/
class CalculatorPanel extends JPanel{
String command;
boolean start;
double result;
//创建一个显示文本框,和一个装按钮的面板
JTextField text = new JTextField();
JPanel panel = new JPanel();
public CalculatorPanel(){
//第一个输入的数字的符号
command = "+";
//第一个输入的不能为符号按钮,必须是数字按钮
start = true;
//开始时,总数为0
result = 0;
//显示文本框设置为只读
text.setEditable(false);
//从右边开始显示
text.setHorizontalAlignment(JTextField.RIGHT);
//4行4列按钮布局
panel.setLayout(new GridLayout(5, 4));
MyListenerOne m1 = new MyListenerOne();
MyListenerTwo m2 = new MyListenerTwo();
//清除按钮
addButton("C",new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//第一个输入的数字的符号
command = "+";
//第一个输入的不能为符号按钮,必须是数字按钮
start = true;
//开始时,总数为0
result = 0;
text.setText("");
}
});
//回删按钮
addButton("←", new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
char[] ch = text.getText().toCharArray();
int a = 0;
int leng = ch.length-1;
StringBuffer sb = new StringBuffer();
while( (leng--)>0 ){
sb.append(ch[a++]);
}
text.setText(sb.toString());
}
});
//^2求平方按钮
addButton("^2",m2);
//√求开方按钮
addButton("√",m2);
addButton("7", m1);
addButton("8", m1);
addButton("9", m1);
addButton("+", m2);
addButton("4", m1);
addButton("5", m1);
addButton("6", m1);
addButton("-", m2);
addButton("1", m1);
addButton("2", m1);
addButton("3", m1);
addButton("*", m2);
addButton("0", m1);
addButton(".", m1);
addButton("=", m2);
addButton("/", m2);
this.setLayout(new BorderLayout());
this.add(BorderLayout.NORTH,text);
this.add(BorderLayout.CENTER,panel);
this.setSize(200, 250);
this.setVisible(true);
}
/**
* 增加按钮,并为每个按钮增加监听
* @param s 按钮名字
* @param al 监听事件
*/
public void addButton(String s, ActionListener al){
JButton jb = new JButton(s);
jb.addActionListener(al);
panel.add(jb); //每个按钮都加入按钮面板中
}
/**
* 数字按钮点击事件:
*/
class MyListenerOne implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//★获取当前点击按钮的动作命令(名字)
String input = e.getActionCommand();
//当显示文本中有数据,我们就先清空,否则进行数字拼接
if( start ){
text.setText("");
start = false;
}
//判断,如果该字符串存在"."则无法再输入"."
if(input.equals(".")){
char[] ch = text.getText().toCharArray();
for(char c : ch){
if( c == 46 ){
return;
}
}
}//数字拼接,如:1253546
text.setText(text.getText()+input);
}
}
/**
* 符号按钮点击事件:
*/
class MyListenerTwo implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获取当前点击按钮的动作命令(名字)
String s = e.getActionCommand();
if(start){
//★当第一个按下的是符号按钮是,如-,表示负数
command = s;
}else{
//先判断是否为平方或者开方,不是则执行下面的运算
if(s.equals("^2")){
result = Double.parseDouble(text.getText())*Double.parseDouble(text.getText());
}else if(s.equals("√")){
result = Math.sqrt(Double.parseDouble(text.getText()));
}else{
//★本次使用的command是上次记录的command,如果是第一次进来该点击事件
//★那么command就是初始化时候定义的"+",初始化时候的"+"也表示数字为正数
if( command.equals("+") ){
result += Double.parseDouble(text.getText());
}else if( command.equals("-") ){
result -= Double.parseDouble(text.getText());
}else if( command.equals("*") ){
result *= Double.parseDouble(text.getText());
}else if( command.equals("/") ){
result /= Double.parseDouble(text.getText());
}else if( command.equals("=") ){
result = Double.parseDouble(text.getText());
}
}
//每次执行一次符号点击事件都进行一下操作:
//1、将总数显示在显示文本
text.setText(String.valueOf(result));
//2、将start置true,不能连续输入多个符号
start = true;
//★3、记住当次进入该点击事件的command,下次使用
command = s;
}
}
}
}