第8章 标签与按钮(二)

简介:   8.2.7 JLabel类总结   例8-6 创建JLabel实例 import java.net.URL; import java.

 

8.2.7 JLabel类总结

 

8-6 创建JLabel实例

import java.net.URL;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

 

public class Test extends JApplet implements SwingConstants {

        public void init() {

               Container contentPane = getContentPane();

               JComboBox iconTextGap = new JComboBox();

               JPanel controlPanel = new JPanel();

               ImageIcon icon = new ImageIcon(this.getClass().getResource("ladybug.gif"));

 

               final JLabel label = new JLabel("Lady Bug", icon, CENTER);

 

               label.setFont(new Font("Times-Roman", Font.ITALIC, 20));

 

               iconTextGap.addItem("4");

               iconTextGap.addItem("10");

               iconTextGap.addItem("15");

               iconTextGap.addItem("20");

               iconTextGap.addItem("25");

 

               controlPanel.add(new JLabel("Icon/Text Gap:"));

               controlPanel.add(iconTextGap);

 

               contentPane.setLayout(new BorderLayout());

               contentPane.add(controlPanel, "North");

               contentPane.add(label, "Center");

 

               iconTextGap.addItemListener(new ItemListener() {

                       public void itemStateChanged(ItemEvent event) {

                               JComboBox b = (JComboBox)event.getSource();

                               String    s = (String)b.getSelectedItem();

                               int       gap = Integer.parseInt(s);

                              

                               label.setIconTextGap(gap);

                       }

               });

        }

}

8.3 按钮

 

8.4 JButtion

 

8-7 一个按钮简单例子

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

 

public class Test extends JApplet {

        JButton button = new JButton("button ...",

                                                             new ImageIcon(this.getClass().getResource("exclaim.gif")));

        int actCnt = 0;

 

        public void init() {

               Container contentPane = getContentPane();

 

               contentPane.setLayout(new FlowLayout());

               contentPane.add(button);

 

               button.addActionListener(new ActionListener() {

                       public void actionPerformed(ActionEvent event) {

                               showStatus(event.getActionCommand() +

                                                     " activated " + actCnt + " times");

                               actCnt++;

                       }

               });

        }

}

8.4.1 JButtion属性

 

8.4.2 JButtion事件

 

8-8 处理JButton事件

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

 

public class Test extends JApplet {

        //Icon icon = new ImageIcon("icon.gif");

        JButton button = new JButton("button");

 

        public Test() {

               Container contentPane = getContentPane();

 

               button.setRolloverIcon(new ImageIcon(this.getClass().getResource("punch.gif")));

               button.setIcon(new ImageIcon(this.getClass().getResource("open_hand.gif")));

 

               contentPane.setLayout(new FlowLayout());

               contentPane.add(button);

 

               button.addActionListener(new ActionListener() {

                       public void actionPerformed(ActionEvent e) {

                               System.out.println("action!");

                       }

               });

               button.addChangeListener(new ChangeListener() {

                       public void stateChanged(ChangeEvent e) {

                               System.out.println(getButtonState());

                       }

               });

        }

        private String getButtonState() {

               ButtonModel model = button.getModel();

               String state = "Button State: ";

 

               state += model.isSelected() ? "selected" : "deselected";

               state += model.isPressed() ? ", pressed" :

                                                                      ", not pressed";

               state += model.isArmed() ? ", armed" : ", disarmed";

               state += model.isRollover() ? ", rollover" :

                                                                       ", not rollover";

 

               return state;

        }

}

8.4.3 JButtion类总结

 

8-9 创建JButton实例

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class Test extends JApplet {

        Icon icon = new ImageIcon("icon.gif");

 

        JButton noargButton = new JButton(),

                               textButton = new JButton("text"),

                               textIconButton = new JButton("text", icon),

                               iconButton = new JButton(icon);

 

        public Test() {

               Container contentPane = getContentPane();

 

               contentPane.setLayout(new FlowLayout());

               contentPane.add(noargButton);

               contentPane.add(textButton);

               contentPane.add(iconButton);

               contentPane.add(textIconButton);

        }

}

8-10 把一个按钮指定为缺省的按钮

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class Test extends JApplet {

        Icon icon = new ImageIcon("icon.gif");

 

        JButton noargButton = new JButton(),

                               textButton = new JButton("text"),

                               textIconButton = new JButton("text", icon),

                               iconButton = new JButton(icon);

 

        public Test() {

               Container contentPane = getContentPane();

 

               contentPane.setLayout(new FlowLayout());

               contentPane.add(noargButton);

               contentPane.add(textButton);

               contentPane.add(iconButton);

               contentPane.add(textIconButton);

        }

}

 

8-11 程序方式单击一个按钮

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class Test extends JApplet {

        int clickDuration = 68;

 

        public Test() {

               Container contentPane = getContentPane();

               JPanel controlPanel = new JPanel();

               JPanel buttonPanel = new JPanel();

 

               JButton doClick = new JButton("do click");

               final JButton clickMe = new JButton("click me");

 

               final JComboBox comboBox = new JComboBox(new Object[] {

                               "68", "250", "500", "750", "1000"

               });

 

               controlPanel.add(new JLabel("Click Duration:"));

               controlPanel.add(comboBox);

 

               buttonPanel.add(doClick);

               buttonPanel.add(clickMe);

 

               contentPane.add(controlPanel, BorderLayout.NORTH);

               contentPane.add(buttonPanel, BorderLayout.CENTER);

 

               getRootPane().setDefaultButton(doClick);

 

               doClick.addActionListener(new ActionListener() {

                       public void actionPerformed(ActionEvent e) {

                               clickMe.doClick(clickDuration);

                       }

               });

 

               comboBox.addItemListener(new ItemListener() {

                       public void itemStateChanged(ItemEvent e) {

                               if(e.getStateChange() == ItemEvent.SELECTED) {

                                      clickDuration = Integer.parseInt((String)

                                                                     comboBox.getSelectedItem());

                               }

                       }

               });

        }

}

8-12 JButton图标

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.plaf.basic.*;

import java.awt.*;

import java.awt.event.*;

 

public class Test extends JApplet {

        public void init() {

               Container contentPane = getContentPane();

               Icon icon = new StringIcon("icon for JButton"),

                        rolloverIcon = new StringIcon("rollover"),

                        pressedIcon = new StringIcon("pressed"),

                        disabledIcon = new StringIcon("disabled"),

                        selectedIcon = new StringIcon("selected"),

                        rolloverSelectedIcon =

                                      new StringIcon("rollover selected"),

                        disabledSelectedIcon =

                                      new StringIcon("disabled selected");

 

               final JButton button = new JButton();

 

               button.setRolloverEnabled(true);

 

               button.setIcon(icon);

               button.setRolloverIcon(rolloverIcon);

               button.setRolloverSelectedIcon(rolloverSelectedIcon);

               button.setSelectedIcon(selectedIcon);

               button.setPressedIcon(pressedIcon);

               button.setDisabledIcon(disabledIcon);

               button.setDisabledSelectedIcon(disabledSelectedIcon);

 

               JComboBox cb = new JComboBox();

               cb.addItem("enabled");

               cb.addItem("disabled");

 

               cb.addItemListener(new ItemListener() {

                       public void itemStateChanged(ItemEvent e) {

                               if(e.getStateChange() == ItemEvent.SELECTED) {

                                      String item = (String)e.getItem();

 

                                      if(item.equals("enabled")) {

                                              button.setEnabled(true);

                                      }

                                      else {

                                              button.setEnabled(false);

                                      }

                               }

                       }

               });

               contentPane.setLayout(new FlowLayout());

               contentPane.add(cb);

               contentPane.add(button);

        }

}

class StringIcon implements Icon {

        private String s;

 

        public StringIcon(String s) {

               this.s = s;

        }

        public int getIconWidth() { return 100; }

        public int getIconHeight() { return 100; }

 

        public void paintIcon(Component c, Graphics g, int x, int y) {

               FontMetrics fm = g.getFontMetrics();

               g.setColor(c.getForeground());

               g.drawString(s, 10, fm.getHeight());

        }

}

 

8-13 设置按钮边距

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class Test extends JApplet {

        public Test() {

               Container contentPane = getContentPane();

               JButton button = new JButton("button");

 

               button.setMargin(new Insets(50,25,10,5));

               contentPane.setLayout(new FlowLayout());

               contentPane.add(button);

        }

}

8-14 按钮助记符

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class Test extends JApplet {

        public Test() {

               Container contentPane = getContentPane();

               JButton button = new JButton("button With Mnemonic");

 

               button.setMnemonic('M');

 

               contentPane.setLayout(new FlowLayout());

               contentPane.add(button);

        }

}

 

8.4.4 AWT兼容

 

8.5 本章回顾

 

 

目录
相关文章
|
5月前
textarea文本框默认显示文本鼠标点击时清空
textarea文本框默认显示文本鼠标点击时清空
|
3月前
点击div显示下拉框,然后下拉框中的点击事件不生效。
点击div显示下拉框,然后下拉框中的点击事件不生效。
|
5月前
|
前端开发
uniapp checkbox样式失效,选中框选中按钮不显示
uniapp checkbox样式失效,选中框选中按钮不显示
84 0
|
9月前
|
JavaScript
原生js实现复选框选全部框与取消全选框
原生js实现复选框选全部框与取消全选框
39 0
|
前端开发 JavaScript
两种方式实现css取消页面鼠标双击选中文字或单击拖动选中文字的效果
两种方式实现css取消页面鼠标双击选中文字或单击拖动选中文字的效果
400 0
【Layui】关于单选框的选中状态,下拉框默认显示
【Layui】关于单选框的选中状态,下拉框默认显示
598 0
【Layui】关于单选框的选中状态,下拉框默认显示
|
存储 JavaScript
行内表单 在统一行显示搜索框 下拉框 按钮
行内表单 在统一行显示搜索框 下拉框 按钮
行内表单 在统一行显示搜索框 下拉框 按钮
点击element-ui表格中的图标,上方显示具体的文字描述
点击element-ui表格中的图标,上方显示具体的文字描述
点击element-ui表格中的图标,上方显示具体的文字描述
按钮与标签组件
按钮与标签组件
89 0