javaswing的动态增加控件:
class dynmaicBtnListener implements java.awt.event.ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("new button clicked.");
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
CenterViewPanel.setLayout(new BoxLayout(CenterViewPanel, BoxLayout.Y_AXIS));
JButton newbtn = new JButton("MyDynamicButton");
newbtn.addActionListener(new dynmaicBtnListener ());
CenterViewPanel.add(newbtn);
// CenterViewPanel.validate();
// CenterViewPanel.repaint();
CenterViewPanel.revalidate();
}
在动态增加控件的时候,必须在add之前调用layout设置方法。不然没有效果。
CenterViewPanel.setLayout(new BoxLayout(CenterViewPanel, BoxLayout.Y_AXIS));
在add之后要调用
CenterViewPanel.validate();
CenterViewPanel.repaint();
或
CenterViewPanel.revalidate();
去刷新重绘控件。
javaswing的控件属性:
了解各种用户界面组件:
JButton、JLabel、JTextField、JTextArea、JComboBox、 Jlist、JCheckBox、JRadioButton、JMenuBar、JMenu、JMenuItem、JCheckBoxMenuItem、JRadioButtonMenuItem、JScrollBar、JScrollPane、JTabbedPane等
一、JButton
按钮是一种点击时触发行为事件的组件。
按钮的四个构造方法:
public Jbutton()创建一个空按钮
public JButton(String text) 创建一个标有指定文字的按钮
public JButton(Icon icon) 创建一个标有指定图标的按钮
public JButton(String text,Icon icon) 创建一个标有指定文字和图标的按钮
图标:是一个固定大小的图片,典型的图标体形较小,用于装饰组件。利用类ImageIcon可以从图像文件中得到图标,如:Icon icon=new ImageIcon(“photo.gif”);
JButton 的属性
text:按钮上的标签,例如可用jbt.setText(“OK”)给按钮jbt设置标签。
icon:按钮上的图标,例如可用jbt.setTextIcon(new ImageIcon(“a.gif”))。
mnemonic:指定热键。同时按下ALT键和指定热键相当于按下该按钮。例如使用jbt.setMnemonic(‘O’)可将O设置为按钮jbt的热键。
horizontalAlignment:此属性只有三个值SwingConstants.LEFT,SwingConstants.CENTER, SwingConstants.RIGHT。它指定按钮上标签的水平对齐方式。默认值为居中。
verticalAlignment:此属性也取三个值SwingConstants.TOP, SwingConstants.CENTER和SwingConstants.BOTTOM。它指定按钮上标签的垂直对齐方式。默认值为居中。
horizontalTextPosition:此属性有三个值SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT。它指定文本相对于图标的水平位置,默认为SwingConstants.RIGHT。对应方法setHorizontalTextPosition。
verticalTextPosition:此属性有三个值SwingConstants.TOP, SwingConstants.CENTER, SwingConstants.BOTTOM。它指定文字相对图标的垂直位置,默认值为SwingConstants.CENTER。对应方法setVerticalTextPosition。
二、JLabel
标签是显示一小段文字、一幅图片或者二者皆有的区域。
它的六个构造方法如下:
public JLabel() 创建一个空标签。
public JLabel(String text,int horizontalAlignment) 创建一个指定内容字符串和水平对齐方式的标签。其中水平对齐方式可取值SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT。
public JLabel(String text) 创建一个指定文字的标签。
public JLabel(Icon icon) 创建一个指定图标的标签。
public JLabel(Icon icon,int horizontalAlignment) 创建一个指定图标和水平对齐方式的标签。
public JLabel(String text,Icon icon,int horizontalAlignment) 创建一个指定文本、图标和水平对齐方式的标签。
例如,下面语句创建一个文本内容为“Interest Rate”的标签:
JLabel myLabel = new JLabel(“Interest Rate”);
下面的语句创建一个标签,它使用文件“images/map.gif”中的图像作图标:
JLabel mapLabel = new JLabel(new ImageIcon(“images/map.gif”);
JLabel 的属性
JLabel继承了类Jcomponent的所有属性,并具有JButton类的许多属性,如:
text
icon
horizontalAlignment
verticalAlignment
三、JTextField
文本域是一个用户可以输入字符的输入区。允许用户输入各种数据,如姓名和描述文字。
JTextField的四个构造方法:
public JTextField() 创建一个空文本域。
public JTextField(int columns) 创建一个指定列数的空文本域。
public JTextField(String text) 用指定初始文字创建一个文本域。
public JTextField(String text,int columns) 创建一个文本域,并用指定文字和列数初始化。
JTextField 属性
除了text、horizontalAlignment等属性外,JTextField还有如下属性:
editable 布尔型属性,表明用户能否修改文本域。
columns 文本域的宽度。
JTextField 方法
getText() 从文本域返回字符串。
setText(String text) 将给定字符串写入文本域当中
setEditable(boolean editable) 使文本域变为可编辑的,默认为true。
setColumns(int) 设置文本与的列数,文本域的长度可变。
四、JTextArea
如想让用户输入多行文本,只能通过创建多个JTextField实例来实现,解决问题的更好办法是使用JTextField,它允许用户输入多行文字。
JTextArea的三个构造方法:
public JTextArea() 创建一个空的文本区。
JTextArea(int rows, int columns) 创建一个指定行数和列数的文本区。
JTextArea(String s, int rows, int columns) 创建一个指定文本、行数和列数的文本区。
JTextArea 属性
除了text、editable、columns外,JTextArea还有以下属性:
lineWrap
wrapStyleWord
rows
lineCount
tabSize
JTextArea 的方法
以下方法用于插入、追加和替换文本:
public void inser(String s,int pos) 将字符串s插入到文本区的指定位置pos。
public void append(String s) 将字符串s添加到文本的末尾。
public void replaceRange(String s,int start,int end) 用字符串s替换文本中从位置start到end的文字。
五、JComboBox
组和框是一些项目的简单列表,用户能够从中进行选择。
JComboBox的两个构造方法:
public JComboBox()默认构造方法。
public JComboBox(Object[] stringItems) 带有字符串列表的构造方法,其中stringItems是一个字符串数祖。
JComboBox的属性
JComboBox的有用的属性:
selectedIndex int值,表示组合框中选定项的序号。
selectedItem Object类型,表示选定项。
JComboBox 的方法
有用的方法:
public void addItem(Object item)在组和框中添加一个选项,它可以是任何对象。
public Object getItemAt(int index) 得到组合框中指定序号的项。
public void removeItem(Object anObject)删除指定的项。
public void removeAllItems() 删除列表中所有项。
六、JList
列表框的作用和组合框的作用基本相同,但它允许用户同时选择多项。
JList的两个构造方法:
JList()
创建一个空的列表框
JList(Object[] stringItems)
创建一个有初始项的列表框
JList不会自动滚动。给列表框加滚动条的方法与文本区相同,只需创建一个滚动窗格并将列表框加入其中即可。
Jlist的 属性
selectedIndexd
selectedIndices
selectedValue
selectedValues
selectionMode
visibleRowCount
七、JCheckBox
复选框是一种用户能够打开、关闭选项的组件,如同电灯开关一般。
JCheckBox的七个构造方法:
JCheckBox()
JCheckBox(String text)
JCheckBox(String text, boolean selected)
JCheckBox(Icon icon)
JCheckBox(Icon icon, boolean selected)
JCheckBox(String text, Icon icon)
JCheckBox(String text, Icon icon, boolean selected)
JCheckBox 的属性
JCheckBox 除了具有JButton的所有属性如text、icon、mnemonic、verticalAlignment、horizontalAlignment、horizontalTextPosition和verticalTextPosition 外,还有selected属性,该属性指明复选框是否被选中。
八、JRadioButton
单选按钮,或叫选择按钮,让用户从一组组件中选择唯一的一个选项。
JRadioButton的七个构造方法:
JRadioButton()
JRadioButton(String text)
JRadioButton(String text, boolean selected)
JRadioButton(Icon icon)
JRadioButton(Icon icon, boolean selected)
JRadioButton(String text, Icon icon)
JRadioButton(String text, Icon icon, boolean selected)
JRadioButton 的属性
JRadioButton 除具有JButton的所有属性如text、icon、mnemonic、verticalAlignment、 horizontalAlignment、horizontalTextPosition、verticalTextPosition外,还具有 selected属性,该属性指明单选按钮是否被选中。
将单选钮组合成组
单选按钮可以象按钮一样添加到容器中。要将单选按钮分组,需要创建java.swing.ButtonGroup的一个实例,并用add方法把单选按钮添加到该实例中,如:
ButtonGroup btg = new ButtonGroup();
btg.add(jrb1);
btg.add(jrb2);
上述代码创建了一个单选按钮组,这样就不能同时选择jrb1和jrb2。
九、消息对话框
对话框通常用作临时窗口,用来接受用户的附加信息或提示用户发生了某事件。
消息对话框是一种简单却频繁使用的对话框,用来显示消息提醒用户。
消息对话框是模式的,即消息对话框消失前其他窗口均不可用。
创建消息对话框
使用 JOptionPane类中的静态方法:
showMessageDialog(Component parentComponent, Object message, String title, int messageType)
parentComponet是对话框的父组件,对话框都是由它派生而来的。message是要显示的对象,它通常是一个字符串。title是对话框的标题。messageType决定了所显示消息的类型。
showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)
除PLAIN_MESSAGE外,每种消息都有相应的图标,以上方法可支持自制图标。
javaswing的注册事件:
Swing 是目前Java中不可缺少的窗口工具组,是建立图形化用户界面(GUI)程序的强大工具。Java Swing组件自动产生各种事件来响应用户行为。Java将事件封装成事件类,并且为每个事件类定义了一个事件监听器。一个组件注册事件监听器方法,表明该组件要响应指定事件。也就是说我们可以通过注册监听器,监听事件源产生的事件,从而在事件处理程序中处理我们所需要处理的用户行为。
Java Swing中处理各组件事件的一般步骤是:
1. 新建一个组件。
2. 将该组件添加到相应的面板。
3. 注册监听器以监听事件源产生的事件
4. 定义处理事件的方法。
注册事件我们一般采用两种方式:一是:利用一个监听器以及多个if语句来决定是哪个组件产生的事件;二是使用多个内部类来响应不同组件产生的各种事件,它又分两种方式,一种是采用匿名内部类,一种是采用一般内部类。
下面我们采用以上三种方式来注册事件。来说明以上三种方式是如何实现事件的处理方法。
一、采用一个监听器多个if语句来实现
在这种方式下:我们要继承ActionListener接口,并且要实现actionPerformed方法。通过getActionCommand()方法来获取事件的事件源。
[java] view plain copy
public class Test_01 extends JFrame implements ActionListener {
Test_01() {
JPanel panel = new JPanel();
JButton button1 = new JButton("按钮一");
JButton button2 = new JButton("按钮二");
panel.add(button1);
panel.add(button2);
this.getContentPane().add(panel);
this.setVisible(true);
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String source = e.getActionCommand();
if (source.equals("按钮一")) {
System.out.println("你按了按钮一");
}
if (source.equals("按钮二")) {
System.out.println("你按了按钮二");
}
}
public static void main(String args[]) {
new Test_01();
}
}
利用一个监听器来处理事件的缺点是:其实当处理的事件比较少的时候,这种方式还是一种比较好的方式,它简单。当程序比较复杂时,需要一大串的if语句来实现。程序的代码比较难阅读和维护。
一、利用匿名内部类来是实现
[java] view plain copy
public class Test_02 extends JFrame{
Test_02(){
JPanel panel = new JPanel();
JButton button1 = new JButton("按钮一");
JButton button2 = new JButton("按钮二");
panel.add(button1);
panel.add(button2);
this.getContentPane().add(panel);
this.setVisible(true);
button1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("你按了按钮一");
}
});
button2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("你按了按钮二");
}
});
}
public static void main(String args[]){
new Test_02();
}
}
使用匿名内部类来实现可以解决使用if来获取事件源带来的麻烦。但是使用匿名内部类同样存在着一些问题。由于它是和事件组一起的。根据事件组在代码中的位置不同,类的定义以及处理事件,同样不便于阅读。如果事件处理程序比较复杂,内部类中的代码就会变的很长。
三、利用一般内部类来实现
[java] view plain copy
public class Test_03 extends JFrame{
Test_03(){
JPanel panel = new JPanel();
JButton button1 = new JButton("按钮一");
JButton button2 = new JButton("按钮二");
panel.add(button1);
panel.add(button2);
this.getContentPane().add(panel);
this.setVisible(true);
button1.addActionListener(new Button1ActionListener());
button2.addActionListener(new Button2ActionListener());
}
private class Button1ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("你按了按钮一");
}
}
private class Button2ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("你按了按钮二");
}
}
public static void main(String[] args) {
new Test_03();
}
}
利用一般内部类我们可以解决很多的问题,该方法避免了第二种方法中由于使用匿名内部类而导致的代码混乱。它把所有的事件处理方法都集中在一块,并且都具有有意义的名称,程序非常容易阅读与维护。单个的事件处理程序也可以被工具栏、菜单栏等重复使用。
基于上面的总结,我们一般采用第三种方法来注册事件