⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s04.htm

📁 Java2Swingt界面设计
💻 HTM
📖 第 1 页 / 共 4 页
字号:
            <p>&nbsp;</p>            <p align="center"><b>例4-21 用客户属性把一个动态目标分配给一个按钮</b></p>            <hr size="1" noshade>            import javax.swing.*;<br>            import java.util.*;<br>            import java.awt.*;<br>            import java.awt.event.*;<br>            import java.beans.*;            <p>public class ClientPropertiesTest extends JApplet {<br>              JButton button = new JButton(&quot;toggle target color&quot;);<br>              JComboBox targetCombo = new JComboBox();<br>              JPanel[] targets = { new JPanel(),<br>              new JPanel(),<br>              new JPanel() };<br>              public void init() {<br>              Container contentPane = getContentPane();<br>              Dimension targetPreferredSize = new Dimension(100,100);<br>              JPanel targetPanel = new JPanel();</p>            <p> for(int i=0; i &lt; targets.length; ++i) {<br>              targets[i].setBackground(Color.blue);<br>              targets[i].setPreferredSize(targetPreferredSize);<br>              targetPanel.add(targets[i]);</p>            <p> }<br>              targetCombo.addItem(&quot;left&quot;);<br>              targetCombo.addItem(&quot;center&quot;);<br>              targetCombo.addItem(&quot;right&quot;);</p>            <p> contentPane.setLayout(new FlowLayout());<br>              contentPane.add(button);<br>              contentPane.add(targetCombo);<br>              contentPane.add(targetPanel);</p>            <p> button.putClientProperty(&quot;target&quot;, targets[0]);</p>            <p> button.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent e) {<br>              Component c = <br>              (Component)button.getClientProperty(&quot;target&quot;);</p>            <p> if(c != null) {<br>              Color bg = c.getBackground();</p>            <p> c.setBackground(bg == Color.blue ?<br>              Color.red : Color.blue);</p>            <p> c.repaint();<br>              }<br>              }<br>              });<br>              targetCombo.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent e) {<br>              button.putClientProperty(<br>              &quot;target&quot;, <br>              targets[targetCombo.getSelectedIndex()]);<br>              }<br>              });<br>              button.addPropertyChangeListener(<br>              new PropertyChangeListener() {<br>              public void propertyChange(PropertyChangeEvent e) {<br>              if(e.getPropertyName().equals(&quot;target&quot;)) {<br>              showStatus(<br>              (String)targetCombo.getSelectedItem() + <br>              &quot; panel set as target&quot;);<br>              }<br>              }<br>              });<br>              }<br>              public static void main(String args[]) {<br>              final JFrame f = new JFrame();<br>              JApplet applet = new ClientPropertiesTest();</p>            <p> applet.init();</p>            <p> f.setContentPane(applet.getContentPane());<br>              f.setBounds(100,100,300,250);<br>              f.setTitle(&quot;ClientPropertiesTest&quot;);<br>              f.setVisible(true);</p>            <p> f.addWindowListener(new WindowAdapter() {<br>              public void windowClosing(WindowEvent e) {<br>              f.dispose();<br>              System.exit(0);<br>              }<br>              });<br>              }<br>              }</p>            <hr size="1" noshade>            <p> 4.10 焦点管理</p>            <p>&nbsp;</p>            <p> 4.10.1 JComponent的焦点属性</p>            <p>&nbsp;</p>            <p align="center"><b>例4-22 为Swing组件指定焦点属性</b></p>            <hr size="1" noshade>            import javax.swing.*;<br>            import java.awt.*;<br>            import java.awt.event.*;            <p>public class Test extends JApplet {<br>              private JButton button_1 = new NotFocusTraversableButton(),<br>              button_2 = new ButtonThatManagesFocus(),<br>              button_3 = new JButton(&quot;regular button&quot;),<br>              button_4 = new JButton(&quot;regular button&quot;),<br>              button_5 = new JButton(&quot;request focus disabled&quot;),<br>              button_6 = new JButton(<br>              &quot;next focusable component set to Manages Focus button&quot;);</p>            <p> public void init() {<br>              Container contentPane = getContentPane();<br>              FocusCycleRootPanel panel = new FocusCycleRootPanel();</p>            <p> button_5.setRequestFocusEnabled(false);<br>              button_6.setNextFocusableComponent(button_2);</p>            <p> panel.add(button_3);<br>              panel.add(button_4);<br>              panel.add(button_5);</p>            <p> contentPane.setLayout(new FlowLayout());<br>              contentPane.add(button_1);<br>              contentPane.add(button_2);<br>              contentPane.add(panel);<br>              contentPane.add(button_6);<br>              }<br>              }<br>              class ButtonThatManagesFocus extends JButton {<br>              public ButtonThatManagesFocus() {<br>              super(&quot;Manages Focus&quot;); <br>              }<br>              public boolean isManagingFocus() {<br>              return true;<br>              }<br>              public void processComponentKeyEvent(KeyEvent e) {<br>              System.out.println(e);<br>              }<br>              }<br>              class NotFocusTraversableButton extends JButton {<br>              public NotFocusTraversableButton() {<br>              super(&quot;Not Focus Traversable&quot;); <br>              }<br>              public boolean isFocusTraversable() {<br>              return false;<br>              }<br>              }<br>              class FocusCycleRootPanel extends JPanel {<br>              public FocusCycleRootPanel() {<br>              setBorder(BorderFactory.createTitledBorder(<br>              &quot;FocusCycleRoot Panel&quot;));<br>              }<br>              public boolean isFocusCycleRoot() {<br>              return true;<br>              }<br>              }</p>            <hr size="1" noshade>            <p> 4.10.2 焦点管理器</p>            <p>&nbsp;</p>            <p align="center"><b>例4-23 实现一个定制的焦点管理器</b></p>            <hr size="1" noshade>            import javax.swing.*;<br>            import java.awt.*;<br>            import java.awt.event.*;            <p>public class Test extends JApplet {<br>              private JButton button_1 = new JButton(&quot;button one&quot;),<br>              button_2 = new JButton(&quot;button two&quot;),<br>              button_3 = new JButton(&quot;button three&quot;),<br>              button_4 = new JButton(&quot;button four&quot;),<br>              button_5 = new JButton(&quot;button five&quot;),<br>              button_6 = new JButton(&quot;button six&quot;);</p>            <p> public void init() {<br>              Container contentPane = getContentPane();</p>            <p> javax.swing.FocusManager.setCurrentManager(<br>              new CustomFocusManager());</p>            <p> contentPane.setLayout(new FlowLayout());<br>              contentPane.add(button_1);<br>              contentPane.add(button_2);<br>              contentPane.add(button_3);<br>              contentPane.add(button_4);<br>              contentPane.add(button_5);<br>              contentPane.add(button_6);<br>              }<br>              }<br>              class CustomFocusManager extends DefaultFocusManager {<br>              public boolean compareTabOrder(Component a, Component b) {<br>              Point location_a = a.getLocation(),<br>              location_b = b.getLocation();</p>            <p> int ax = location_a.x, ay = location_a.y;<br>              int bx = location_b.x, by = location_b.y;</p>            <p> if(Math.abs(ay - by) &lt; 10) {<br>              return (bx &lt; ax);<br>              }<br>              return (ay &gt; by); <br>              }<br>              }</p>            <hr size="1" noshade>            <p> 4.11 支持可访问性</p>            <p>&nbsp;</p>            <p align="center"><b>例4-24 获得可访问性信息</b></p>            <hr size="1" noshade>            import javax.swing.*;<br>            import javax.accessibility.*;<br>            import java.awt.*;<br>            import java.awt.event.*;            <p>public class Test extends JApplet {<br>              public void init() {<br>              Container contentPane = getContentPane();<br>              JLabel label = new JLabel(&quot;First Name:&quot;);<br>              JButton showButton = new JButton(<br>              &quot;show accessible information&quot;);</p>            <p> final JTextField field = new JTextField(15);</p>            <p> AccessibleContext fieldContext = <br>              field.getAccessibleContext();</p>            <p> fieldContext.setAccessibleName(&quot;First Name&quot;);<br>              fieldContext.setAccessibleDescription(<br>              &quot;Enter your first name&quot;);</p>            <p> contentPane.setLayout(new FlowLayout());<br>              contentPane.add(showButton);<br>              contentPane.add(label);<br>              contentPane.add(field);</p>            <p> showButton.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent e) {<br>              AccessibleContext context;<br>              AccessibleRole role;</p>            <p> context = field.getAccessibleContext();<br>              role = context.getAccessibleRole();</p>            <p> System.out.print(&quot;Accessible Role: &quot;);<br>              System.out.println(<br>              context.getAccessibleRole());</p>            <p> System.out.print(&quot;Accessible Description: &quot;);<br>              System.out.println(<br>              context.getAccessibleDescription());</p>            <p> System.out.print(&quot;Accessible Name: &quot;);<br>              System.out.println(<br>              context.getAccessibleName());<br>              <br>              }<br>              });</p>            <p> }<br>              }</p>            <hr size="1" noshade>            <p> 4.12 本章回顾</p>            <p>&nbsp;</p>            <p>[<a href="index.html" target="_self">目录</a>][<a href="s03.htm">上一页</a>][<a href="s05.htm">下一页</a>](飒龙收藏/2002.5.18)             </p>            </td>          </tr>        </tbody>      </table>    </td>  </tr></tbody></table><script language="javascript">bottomprint()</script></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -