java gui 更换图标,Java GUI/图形用户界面(续一)

Checkbox类

●Checkbox类用来建立单选按钮和多选按钮(也叫复选框)。

●创建多选按钮,只需要使用构造函数:

( label, boolean state)

●创建单选按钮,需要使用构造函数:

( label, boolean state,  group)

●单选按钮和多选按钮的语义事件为ItemEvent,对应的监听器接口为ItemListener,该接口中只有一个itemStateChanged方法

●编程实例:创建一个多选按钮和两个属于同一组的单选按钮,并对每个按钮的选中情况进行处理

packagecheckbox;

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;;

publicclassTestCheckboxextendsJFrame

{

privatestaticfinallongserialVersionUID= 1L;

Checkboxcheckbox=newCheckbox("上海市",true);

JCheckBoxjcheckbox1=newJCheckBox("北京市");

JCheckBoxjcheckbox2=newJCheckBox("南京市",true);

Imageimg=this.getToolkit().getImage("..""ico.jpg");

ImageIconimgicon=newImageIcon(img);

//Icon icon=imgicon;

CheckboxGroupcbg=newCheckboxGroup();

Checkboxcheckbox1=newCheckbox("18岁",true,cbg);

Checkboxcheckbox2=newCheckbox("20岁",false,cbg);

Choicecho=newChoice();

classchoiceItemListenerimplementsItemListener

{

publicvoiditemStateChanged(ItemEvent e)

{

if(e.getItem().equals("男孩"))

{

System.out.println("我是男孩");

}

else

{

System.out.println("我是女孩");

}

}

}

classcheckboxItemListenerimplementsItemListener

{

publicvoiditemStateChanged(ItemEvent e)

{

Checkbox cb=newCheckbox();

cb=(Checkbox)e.getItemSelectable();

/*if(cb.getLabel().equals("18岁"))

{

System.out.println("18岁啦");

}

else

{

System.out.println("20岁啦");

}

if(cb.getState()==true)

{

System.out.println(cb.getLabel());

}*/

if(cb==checkbox1)

{

System.out.println("18岁啦");

}

else

{

System.out.println("20岁啦");

}

}

}

publicTestCheckbox()

{

//用于产生流式部局管理器

FlowLayout fl=newFlowLayout();

this.setLayout(fl);

cho.add("男孩");

cho.add("女孩");

this.add(cho);

this.add(checkbox);

this.add(jcheckbox1);

this.add(jcheckbox2);

this.add(checkbox1);

this.add(checkbox2);

cho.addItemListener(newchoiceItemListener());

checkboxItemListener cbi=newcheckboxItemListener();

checkbox1.addItemListener(cbi);

checkbox2.addItemListener(cbi);

addWindowListener(newWindowAdapter()

{

publicvoidwindowClosing(WindowEvent e)

{

e.getWindow().dispose();

}

});

}

publicstaticvoidmain(String[] args)

{

//TODOAuto-generated method stub

TestCheckbox mainFrame=newTestCheckbox();

mainFrame.setSize(400,400);

mainFrame.setTitle("TestCheckbox");

mainFrame.setVisible(true);

}

}

Panel与ScrollPane类

●Panel类是一个容器类,用于产生一种特殊的空白面板,可以容纳其他的组件,但不能独立存在

Panel的两个构造函数:

public Panel()

使用默认的布局管理器创建新面板。所有面板的默认布局管理器都是 FlowLayout类

public Panel(LayoutManager layout)

创建具有指定布局管理器的新面板。

参数:

layout - 此面板的布局管理器。

●ScrollPane类是一种容器类,用于产生滚动窗口,通过滚动条在一个较小的窗口中显示较大的子部件。在ScrollPane容器内只能增加一个组件。ScrollPanel的两个构造函数:public ScrollPane()

创建一个具有滚动条策略 "as needed" 的新滚动窗格容器。public ScrollPane(int scrollbarDisplayPolicy)

创建新的滚动窗格容器

滚动条的显示策略可以设置如下:

as needed: 创建滚动条,且只在滚动窗格需要时显示

always: 创建滚动条,且滚动窗格总是显示滚动条

never: 滚动窗格永远不创建或显示滚动条

编程举例:如何使用ScrollPane 如图:

packageTestArea;

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

publicclassTestTextAreaextendsJFrame

{

privatestaticfinallongserialVersionUID= 1L;

publicTestTextArea()

{

//关闭窗体

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

JTextArea jta=newJTextArea(30,20);

ScrollPane sp=newScrollPane();

sp.add(jta);

//把容器添加到窗体

//this.add(sp);

this.getContentPane().add(sp);

addWindowListener(newWindowAdapter()

{

publicvoidwindowClosing(WindowEvent e)

{

//e.getWindow().dispose();

}

});

}

publicstaticvoidmain(String[] args)

{

//TODOAuto-generated method stub

TestTextArea mainFrame =newTestTextArea();

mainFrame.setTitle("TestTextArea");

mainFrame.setSize(200,200);

mainFrame.setVisible(true);

}

}

布局管理器

●一个容器中的各个组件之间的位置和大小关系就称之为布局

●Java语言提供了布局管理器来管理组件在容器中的布局,而不是直接使用位置坐标来设置各个组件的位置和大小

●AWT中的布局管理器类:

◆BorderLayout

◆FlowLayout

◆GridLayout

◆CardLayout

◆GridBagLayout

BorderLayout编程实例

如图:

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

publicclassTestBorderLayoutextendsJFrame

{

privatestaticfinallongserialVersionUID= 1L;

publicTestTextArea()

{

//关闭窗体

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.add(newJButton("North"),"North");

this.add(newJButton("South"),"South");

this.add(newJButton("East"),"East");

this.add(newJButton("West"),"West");

this.add(newJButton("Center"),"Center");

addWindowListener(newWindowAdapter()

{

publicvoidwindowClosing(WindowEvent e)

{

//e.getWindow().dispose();

}

});

}

publicstaticvoidmain(String[] args)

{

//TODOAuto-generated method stub

TestBorderLayoutmainFrame =newTestBorderLayout();

mainFrame.setTitle("TestBorderLayout");

mainFrame.setSize(300,200);

mainFrame.setVisible(true);

}

}

FlowLayout编程实例

packageTestArea;

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

publicclassTestTextAreaextendsJFrame

{

privatestaticfinallongserialVersionUID= 1L;

publicTestTextArea()

{

//关闭窗体

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setLayout(newFlowLayout());

this.add(newJButton("East"));

this.add(newJButton("South"));

this.add(newJButton("West"));

this.add(newJButton("North"));

this.add(newJButton("Center"),"Center");

addWindowListener(newWindowAdapter()

{

publicvoidwindowClosing(WindowEvent e)

{

//e.getWindow().dispose();

}

});

}

publicstaticvoidmain(String[] args)

{

//TODOAuto-generated method stub

TestFlowLayoutmainFrame =newTestFlowLayout();

mainFrame.setTitle("TestFlowLayout");

mainFrame.setSize(300,200);

mainFrame.setVisible(true);

}

}

GridLayout编程实例

●GridLayout布局管理器将容器划分成若干行列的网格,在容器上添加组件时它们会按从左到右、从上到下的顺序在风格中排列

●在GridLayout的构造方法中,需要指定在容器上划分的网格的行、列数

CardLayout

●CardLayout布局管理器能够实现将多个组件放在同一容器区域内的交替显示,相当于多张卡片摞在一起,在任何时候都只有最上面的一个可见

编程实例

创建两个Panel对象,每个Panel上都能拥有一个布局管理器,左边的Panel使用GridLayout布局器放置3个按钮,右边的Panel上使用CardLayout布局管理器来放置卡片,最后在窗口上使用BorderLayout放置这两个Panel面板。右边的Panel中带有5张卡片(用5个按钮模拟),按下左边的Panel中的prev按钮,依次向前显示,按下next按钮,依次向后显示,按下three按钮,显示第三张卡片

packagecardlayout;

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

publicclassTestImageextendsJFrame

{

privatestaticfinallongserialVersionUID= 1L;

JPanelplCard=newJPanel();

CardLayoutcl=newCardLayout();

publicTestImage()

{

this.setLayout(newBorderLayout());

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

JPanel pl=newJPanel();

pl.setLayout(newGridLayout(2,1));

JButton btPrev=newJButton(/*"前一个"*/);

JButton btNext=newJButton(/*"下一个"*/);

ImageIcon icon1=newImageIcon("image""back.gif");

ImageIcon icon2=newImageIcon("image""next.gif");

btPrev.setIcon(icon1);

btNext.setIcon(icon2);

pl.add(btPrev);

pl.add(btNext);

this.add(pl,"West");

plCard.setLayout(cl);

ImageIcon icon3=newImageIcon("image""001.gif");

ImageIcon icon4=newImageIcon("image""002.gif");

ImageIcon icon5=newImageIcon("image""003.gif");

ImageIcon icon6=newImageIcon("image""004.gif");

ImageIcon icon7=newImageIcon("image""005.gif");

ImageIcon icon8=newImageIcon("image""006.gif");

JLabel lb1=newJLabel();

JLabel lb2=newJLabel();

JLabel lb3=newJLabel();

JLabel lb4=newJLabel();

JLabel lb5=newJLabel();

JLabel lb6=newJLabel();

lb1.setIcon(icon3);

lb2.setIcon(icon4);

lb3.setIcon(icon5);

lb4.setIcon(icon6);

lb5.setIcon(icon7);

lb6.setIcon(icon8);

plCard.add(lb1,"1");

plCard.add(lb2,"2");

plCard.add(lb3,"3");

plCard.add(lb4,"4");

plCard.add(lb5,"5");

plCard.add(lb6,"6");

this.add(plCard);

MyActionListener my=newMyActionListener();

btPrev.addActionListener(my);

btNext.addActionListener(my);

}

classMyActionListenerimplementsActionListener

{

publicvoidactionPerformed(ActionEvent e)

{

//if(e.getActionCommand().equals("前一个"))

if(e.getID()==e.COMPONENT_EVENT_MASK)

{

cl.previous(plCard);

}

else

{

cl.next(plCard);

}

}

}

publicstaticvoidmain(String[] args)

{

//TODOAuto-generated method stub

TestImage mainFrame=newTestImage();

mainFrame.setTitle("显示图片");

mainFrame.setBounds(100,100,400,300);

mainFrame.setVisible(true);

}

}

package cardlayout;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TestCardLayout extends JFrame

{

private static final long serialVersionUID = 1L;

CardLayout cl=new CardLayout();

Panel plCard=new Panel();

public TestCardLayout()

{

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

Panel pl=new Panel();

GridLayout gl=new GridLayout(5,1);

pl.setLayout(gl);

JButton btFirst=new JButton("First");

JButton btPrev=new JButton("Prev");

JButton btNext=new JButton("Next");

JButton btThree=new JButton("Three");

JButton btLast=new JButton("Last");

pl.add(btFirst);

pl.add(btPrev);

pl.add(btNext);

pl.add(btThree);

pl.add(btLast);

this.add(pl,"West");

plCard.setLayout(cl);

//images文件夹和bin目录在同一目录下

ImageIcon icon1=new ImageIcon("images""gcz01.jpg");

ImageIcon icon2=new ImageIcon("images""gcz02.jpg");

ImageIcon icon3=new ImageIcon("images""gcz03.jpg");

ImageIcon icon4=new ImageIcon("images""gcz04.jpg");

ImageIcon icon5=new ImageIcon("images""gcz05.jpg");

JButton bt1=new JButton("One",icon1);

JButton bt2=new JButton("Two",icon2);

JButton bt3=new JButton("Three",icon3);

JButton bt4=new JButton(icon4);

JButton bt5=new JButton();

bt5.setIcon(icon5);

plCard.add(bt1,"1");

plCard.add(bt2,"2");

plCard.add(bt3,"3");

plCard.add(bt4,"4");

plCard.add(bt5,"5");

this.add(plCard);

MyActionListener ml=new MyActionListener();

btPrev.addActionListener(ml);

btNext.addActionListener(ml);

btThree.addActionListener(ml);

btFirst.addActionListener(ml);

btLast.addActionListener(ml);

}

class MyActionListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

if(e.getActionCommand().equals("Prev"))

{

cl.previous(plCard);

}

else if(e.getActionCommand().equals("First"))

{

cl.first(plCard);

}

else if(e.getActionCommand().equals("Last"))

{

cl.last(plCard);

}

else if(e.getActionCommand().equals("Next"))

{

cl.next(plCard);

}

else

{

cl.show(plCard,"3");

}

}

}

public static void main(String[] args)

{

// TODO Auto-generated method stub

TestCardLayout mainFrame=new TestCardLayout();

mainFrame.setBounds(100,100,400,300);

mainFrame.setTitle("TestCardLayout");

mainFrame.setVisible(true);

}

}

取消布局管理器

●调用Container.setLayout(null)方法取消布局管理器设置,在这种情况下,可以调用Component.setBounds方法来用绝对坐标设置容器上的每个组件的大小和位置。

●不使用布局管理器将会给程序带来一个潜在的问题,当容器大小改变时,所有组件仍保持原来的位置和大小,交导致整个程序界面比较难看。

Swing和JFC

●所有的Swing组件,位于java.swing包中,它们是构筑在AWT上层的GUI组件,Swing组件是JComponent类的子类,JComponent又是java.awt.Containt的子类

●为保证可移植性,Swing完全用Java语言编写

●Swing提供了比AWT更多的组件库,例如,Jtable,Jtree,JcomboBox

●Swing也增加了AWT中原有组件的功能,例如,与AWT中的Button对应的Swing组件是Jbutton

●JFC(java Foundation Class)是指Sun对早期的JDK进行扩展的部分,集合了Swing组件和其他能简化开发的API类,包括Swing,java2D,accessibility,internationalization

编程实例:从AWT到Swing

JFrame

●JFrame是与AWT中的Frame相对应的Swing组件

●JFrame上面只能有一个唯一的组件,这个组件为JRootPane,调用JFrame.getContentPane()方法可获得JFrame中内置的JrootPane对象。

●应用程序不能直接在JFrame实例对象上增加组件和设置布局管理器,而应该在JRootPane对象上增加子组件和设置布局管理器。

●调用Jframer setDefaultCloseOperation方法,可以设置单击窗口上的关闭按钮时的事件处理方式,例如:当设置值为JFrame.EXIT_ON_CLOSE时,单击JFrame窗口上的关闭按钮,将直接关闭JFrame框架窗口并结束程序运行。

编程实例:使用JFrame来创建程序的主框架窗口

importjava.awt.GridLayout;

importjavax.swing.*;

publicclassTestJFrameextendsJFrame

{

privatestaticfinallongserialVersionUID= 1L;

publicTestJFrame()

{

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton bt=newJButton("OK");

JButton bt1=newJButton("EXIT");

//this.getContentPane().add(bt);

JPanel pl=newJPanel();

pl.setLayout(newGridLayout(2,1));

pl.add(bt);

pl.add(bt1);

this.getContentPane().add(pl);

}

publicstaticvoidmain(String[] args)

{

//TODOAuto-generated method stub

TestJFrame mainFrame=newTestJFrame();

mainFrame.setSize(300,200);

mainFrame.setVisible(true);

}

}

JScrollPane

●JScrollPane是与AWT中的ScrollPane相对应的Swing组件

●最基本的JScrollPane由水平和垂直方向上的JScrollBar、以及一个JViewport组件

●调用JScrollPane.getViewport方法,可以获得代表滚动窗口中的视图区域的JViewport对象。

●调用JViewport.setViesport方法,可以将滚动窗口中要显示的内容作为子组件增加到JViewport上。

●编程实例:使用JScrollPane创建滚动窗口

importjavax.swing.*;

publicclassTestJFrameextendsJFrame

{

privatestaticfinallongserialVersionUID= 1L;

publicTestJFrame()

{

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JScrollPane jsp=newJScrollPane();

JTextArea jta=newJTextArea(20,30);

jsp.getViewport().add(jta);

this.getContentPane().add(jsp);

}

publicstaticvoidmain(String[] args)

{

//TODOAuto-generated method stub

TestJFrame mainFrame=newTestJFrame();

mainFrame.setSize(300,200);

mainFrame.setVisible(true);

}

}

JScrollPane

●JOptionPane类提供了若干了showXxxDialog静态方法,可用来产生简单的标准对话框

方法名

描述

showConfirmDialog

询问一个确认问题,如 yes/no/cancel。

showInputDialog

提示要求某些输入。

showMessageDialog

告知用户某事已发生。

showOptionDialog

上述三项的大统一 (Grand Unification)。public static int showConfirmDialog( parentComponent,message,title,int optionType,int messageType)throws

调用一个由 optionType参数确定其中选项数的对话框,messageType参数确定要显示的图标。messageType参数主要用于提供来自外观的默认图标。

参数:

parentComponent - 确定在其中显示对话框的 Frame;如果为 null或者 parentComponent不具有 Frame,则使用默认的 Frame。

message - 要显示的 Object

title - 对话框的标题字符串

optionType - 指定可用于对话框的选项的整数:YES_NO_OPTION、YES_NO_CANCEL_OPTION或 OK_CANCEL_OPTION

messageType - 指定此消息种类的整数;主要用于确定来自可插入外观的图标:ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE或 PLAIN_MESSAGE

返回:

指示用户所选选项的整数

示例:

显示一个错误对话框,该对话框显示的 message 为 'alert':

JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);

显示一个内部信息对话框,其 message 为 'information':

JOptionPane.showInternalMessageDialog(frame, "information",

"information", JOptionPane.INFORMATION_MESSAGE);

显示一个信息面板,其 options 为 "yes/no",message 为 'choose one':

JOptionPane.showConfirmDialog(null,

"choose one", "choose one", JOptionPane.YES_NO_OPTION);

显示一个内部信息对话框,其 options 为 "yes/no/cancel",message 为 'please choose one',并具有 title 信息:

JOptionPane.showInternalConfirmDialog(frame,

"please choose one", "information",

JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

显示一个警告对话框,其 options 为 OK、CANCEL,title 为 'Warning',message 为 'Click OK to continue':

Object[] options = { "OK", "CANCEL" };

JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",

JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,

null, options, options[0]);

显示一个要求用户键入 String 的对话框:

String inputValue = JOptionPane.showInputDialog("Please input a value");

显示一个要求用户选择 String 的对话框:

Object[] possibleValues = { "First", "Second", "Third" };

Object selectedValue = JOptionPane.showInputDialog(null,

"Choose one", "Input",

JOptionPane.INFORMATION_MESSAGE, null,

possibleValues, possibleValues[0]);

编程举例:使用JOptionPane类,在程序开始运行时,弹出一个对话框提示用户,在主框架窗口的关闭按钮被单击时,弹出一个对话框询问用户是否真的要结束程序的运行。

importjava.awt.event.*;

importjavax.swing.*;

publicclassTestJFrameextendsJFrame

{

privatestaticfinallongserialVersionUID= 1L;

publicTestJFrame()

{

this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

ImageIcon iconMessage=newImageIcon("images""message.gif");

JOptionPane.showMessageDialog(this,"程序已运行...","Running",

JOptionPane.INFORMATION_MESSAGE,iconMessage);

JScrollPane jsp=newJScrollPane();

JTextArea jta=newJTextArea(20,30);

jsp.getViewport().add(jta);

this.getContentPane().add(jsp);

addWindowListener(newWindowAdapter()

{

publicvoidwindowClosing(WindowEvent e)

{

ImageIcon iconExit=newImageIcon("images""exit.jpg");

if(JOptionPane.showConfirmDialog(TestJFrame.this,"确认要退出吗?",

"退出", JOptionPane.OK_CANCEL_OPTION,

JOptionPane.QUESTION_MESSAGE,iconExit)==JOptionPane.YES_OPTION)

{

e.getWindow().dispose();

}

}

});

}

publicstaticvoidmain(String[] args)

{

//TODOAuto-generated method stub

TestJFrame mainFrame=newTestJFrame();

mainFrame.setTitle("TestJOptionPane");

mainFrame.setSize(300,200);

mainFrame.setVisible(true);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值