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

📄 s06.htm

📁 Java2Swingt界面设计
💻 HTM
📖 第 1 页 / 共 3 页
字号:
            <p>&nbsp;</p>            <p align="center"><b>例6-8 使用BoxLayout</b></p>            <hr noshade size="1">            import java.awt.*;<br>            import java.awt.event.*;<br>            import javax.swing.*;            <p>public class Test extends JApplet {<br>              public Test() {<br>              Container contentPane = getContentPane();<br>              ContainerWithBoxLayout yaxis = <br>              new ContainerWithBoxLayout(BoxLayout.Y_AXIS);</p>            <p> ContainerWithBoxLayout xaxis = <br>              new ContainerWithBoxLayout(BoxLayout.X_AXIS);</p>            <p> contentPane.setLayout(new FlowLayout());</p>            <p> xaxis.add(new JButton(new ImageIcon(&quot;reach.gif&quot;)));<br>              xaxis.add(new JButton(new ImageIcon(&quot;punch.gif&quot;)));<br>              xaxis.add(new JButton(new ImageIcon(&quot;open_hand.gif&quot;)));</p>            <p> yaxis.add(new JButton(new ImageIcon(&quot;ladybug.gif&quot;)));<br>              yaxis.add(new JButton(new ImageIcon(&quot;crab.gif&quot;)));<br>              yaxis.add(new JButton(new ImageIcon(&quot;frog.gif&quot;)));<br>              yaxis.add(new JButton(new ImageIcon(&quot;snail.gif&quot;)));</p>            <p> contentPane.add(xaxis);<br>              contentPane.add(yaxis);<br>              }<br>              }<br>              class ContainerWithBoxLayout extends JPanel { <br>              public ContainerWithBoxLayout(int orientation) {<br>              setLayout(new BoxLayout(this, orientation));<br>              }<br>              }</p>            <hr noshade size="1">            <p> 6.5.2 Box类</p>            <p>&nbsp;</p>            <p align="center"><b>例6-9 使用水平和垂直的膨胀体</b></p>            <hr noshade size="1">            <hr noshade size="1">            <p> 6.6 进度监视器</p>            <p>&nbsp;</p>            <p> 6.6.1 ProgressMonitor</p>            <p>    <a href="s06_tu05.jar">图6-5</a> 使用一个进度监视器(注:此为一可点击执行的.jar文件,但需要下载到你的电脑上方可点击执行)</p>            <p align="center"><b>例6-10 使用一个进度监视器</b></p>            <hr noshade size="1">            import javax.swing.*;<br>            import java.awt.*;<br>            import java.awt.event.*;<br>            import java.util.*;<br>            import java.io.*;            <p>public class Test extends JFrame {<br>              private JButton readButton = new JButton(&quot;read file&quot;);<br>              private BufferedInputStream in;<br>              private ProgressMonitor pm;<br>              private String fileName = &quot;Test.java&quot;;</p>            <p> public Test() {<br>              final Container contentPane = getContentPane();</p>            <p> contentPane.setLayout(new FlowLayout());<br>              contentPane.add(readButton);</p>            <p> readButton.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent e) {<br>              try {<br>              in = new BufferedInputStream(<br>              new FileInputStream(fileName));</p>            <p> pm = new ProgressMonitor(contentPane,<br>              &quot;Reading File:&quot;,<br>              fileName,<br>              0, in.available());<br>              }<br>              catch(FileNotFoundException fnfx) {<br>              fnfx.printStackTrace();<br>              }<br>              catch(IOException iox) {<br>              iox.printStackTrace();<br>              }</p>            <p> ReadThread t = new ReadThread();<br>              t.start();<br>              }<br>              });<br>              }<br>              class ReadThread extends Thread {<br>              int i, cnt=0;<br>              String s;</p>            <p> public void run() {<br>              try {<br>              readButton.setEnabled(false);</p>            <p> while(!pm.isCanceled() &amp;&amp; (i = in.read()) != -1) {<br>              try {<br>              Thread.currentThread().sleep(25);<br>              }<br>              catch(InterruptedException ex) {<br>              ex.printStackTrace();<br>              }<br>              System.out.print((char)i);</p>            <p> SwingUtilities.invokeLater(new Runnable(){<br>              public void run() {<br>              pm.setProgress(++cnt);<br>              }<br>              });<br>              }<br>              if(pm.isCanceled())<br>              JOptionPane.showMessageDialog(<br>              Test.this,<br>              &quot;Operation Canceled!&quot;,<br>              &quot;Cancellation&quot;,<br>              JOptionPane.ERROR_MESSAGE);<br>              }<br>              catch(IOException ex) {<br>              ex.printStackTrace();<br>              }<br>              finally {<br>              try {<br>              in.close();<br>              }<br>              catch(IOException ex2) {<br>              ex2.printStackTrace();<br>              }<br>              }<br>              readButton.setEnabled(true);<br>              }<br>              }<br>              public static void main(String args[]) {<br>              GJApp.launch(new Test(), <br>              &quot;Using Progress Monitors&quot;,300,300,450,300);<br>              }<br>              }<br>              class GJApp extends WindowAdapter {<br>              static private JPanel statusArea = new JPanel();<br>              static private JLabel status = new JLabel(&quot; &quot;);<br>              static private ResourceBundle resources;</p>            <p> public static void launch(final JFrame f, String title,<br>              final int x, final int y, <br>              final int w, int h) {<br>              launch(f,title,x,y,w,h,null); <br>              }<br>              public static void launch(final JFrame f, String title,<br>              final int x, final int y, <br>              final int w, int h,<br>              String propertiesFilename) {<br>              f.setTitle(title);<br>              f.setBounds(x,y,w,h);<br>              f.setVisible(true);</p>            <p> statusArea.setBorder(BorderFactory.createEtchedBorder());<br>              statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));<br>              statusArea.add(status);<br>              status.setHorizontalAlignment(JLabel.LEFT);</p>            <p> f.setDefaultCloseOperation(<br>              WindowConstants.DISPOSE_ON_CLOSE);</p>            <p> if(propertiesFilename != null) {<br>              resources = ResourceBundle.getBundle(<br>              propertiesFilename, Locale.getDefault());<br>              }</p>            <p> f.addWindowListener(new WindowAdapter() {<br>              public void windowClosed(WindowEvent e) {<br>              System.exit(0);<br>              }<br>              });<br>              }<br>              static public JPanel getStatusArea() {<br>              return statusArea;<br>              }<br>              static public void showStatus(String s) {<br>              status.setText(s);<br>              }<br>              static Object getResource(String key) {<br>              if(resources != null) {<br>              return resources.getString(key);<br>              }<br>              return null;<br>              }<br>              }</p>            <hr noshade size="1">            <p> 6.6.2 PropressMonitorInputStream</p>            <p>&nbsp;</p>            <p align="center"><b>例6-11 使用ProgressMonitorInputStream</b></p>            <hr noshade size="1">            import javax.swing.*;<br>            import java.awt.*;<br>            import java.awt.event.*;<br>            import java.util.*;<br>            import java.io.*;            <p>public class Test extends JFrame {<br>              private ProgressMonitorInputStream in;<br>              private JButton readButton = new JButton(&quot;read file&quot;);</p>            <p> public Test() {<br>              final Container contentPane = getContentPane();<br>              final String fileName = &quot;Test.java&quot;;</p>            <p> contentPane.setLayout(new FlowLayout());<br>              contentPane.add(readButton);</p>            <p> readButton.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent e) {<br>              try {<br>              in = new ProgressMonitorInputStream(<br>              contentPane,<br>              &quot;Reading &quot; + fileName,<br>              new FileInputStream(fileName));<br>              }<br>              catch(FileNotFoundException ex) {<br>              ex.printStackTrace();<br>              }</p>            <p> ReadThread t = new ReadThread();<br>              readButton.setEnabled(false);<br>              t.start();<br>              }<br>              });<br>              }<br>              class ReadThread extends Thread {<br>              public void run() {<br>              int i;</p>            <p> try {<br>              while((i = in.read()) != -1) {<br>              System.out.print((char)i);<br>              try {<br>              Thread.currentThread().sleep(10);<br>              }<br>              catch(Exception ex) {<br>              ex.printStackTrace();<br>              }<br>              }<br>              in.close();<br>              }<br>              catch(IOException ex) {<br>              JOptionPane.showMessageDialog(<br>              Test.this,<br>              &quot;Operation Canceled!&quot;,<br>              &quot;Cancellation&quot;,<br>              JOptionPane.ERROR_MESSAGE);<br>              }<br>              readButton.setEnabled(true);<br>              }<br>              }<br>              public static void main(String args[]) {<br>              GJApp.launch(new Test(), <br>              &quot;Using ProgressMonitorInputStream&quot;,<br>              300,300,450,300);<br>              }<br>              }<br>              class GJApp extends WindowAdapter {<br>              static private JPanel statusArea = new JPanel();<br>              static private JLabel status = new JLabel(&quot; &quot;);<br>              static private ResourceBundle resources;</p>            <p> public static void launch(final JFrame f, String title,<br>              final int x, final int y, <br>              final int w, int h) {<br>              launch(f,title,x,y,w,h,null); <br>              }<br>              public static void launch(final JFrame f, String title,<br>              final int x, final int y, <br>              final int w, int h,<br>              String propertiesFilename) {<br>              f.setTitle(title);<br>              f.setBounds(x,y,w,h);<br>              f.setVisible(true);</p>            <p> statusArea.setBorder(BorderFactory.createEtchedBorder());<br>              statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));<br>              statusArea.add(status);<br>              status.setHorizontalAlignment(JLabel.LEFT);</p>            <p> f.setDefaultCloseOperation(<br>              WindowConstants.DISPOSE_ON_CLOSE);</p>            <p> if(propertiesFilename != null) {<br>              resources = ResourceBundle.getBundle(<br>              propertiesFilename, Locale.getDefault());<br>              }</p>            <p> f.addWindowListener(new WindowAdapter() {<br>              public void windowClosed(WindowEvent e) {<br>              System.exit(0);<br>              }<br>              });<br>              }<br>              static public JPanel getStatusArea() {<br>              return statusArea;<br>              }<br>              static public void showStatus(String s) {<br>              status.setText(s);<br>              }<br>              static Object getResource(String key) {<br>              if(resources != null) {<br>              return resources.getString(key);<br>              }<br>              return null;<br>              }<br>              }</p>            <hr noshade size="1">            <p> 6.7 撤消/重复</p>            <p>&nbsp;</p>            <p> 6.7.1 一个简单的撤消/重复样例</p>            <p>&nbsp;</p>            <p align="center"><b>例6-12 一个简单的撤消/重复样例</b></p>            <hr noshade size="1">            import javax.swing.*;<br>            import javax.swing.undo.*;<br>            import java.awt.*;<br>            import java.awt.event.*;            <p>public class Test extends JApplet {<br>              private JPanel colorPanel = new JPanel();<br>              private BackgroundColorEdit undo = new BackgroundColorEdit();<br>              private Color oldColor;</p>            <p> public void init() {<br>              colorPanel.setBorder(<br>              BorderFactory.createTitledBorder(<br>              &quot;Change color and subsequently undo &quot; +<br>              &quot;from the Edit menu&quot;));</p>            <p> makeMenuBar();<br>              getContentPane().add(colorPanel, BorderLayout.CENTER);<br>              }<br>              private void makeMenuBar() {<br>              JMenuBar menuBar = new JMenuBar();<br>              JMenu editMenu = new JMenu(&quot;Edit&quot;);</p>            <p> editMenu.add(new SetColorAction());<br>              editMenu.add(new UndoAction());</p>            <p> menuBar.add(editMenu);<br>              setJMenuBar(menuBar);<br>              }<br>              class SetColorAction extends AbstractAction {<br>              public SetColorAction() {<br>              super(&quot;Set color ...&quot;);<br>              }<br>              public void actionPerformed(ActionEvent e) {<br>              Color color = JColorChooser.showDialog(<br>              Test.this, // parent component<br>              &quot;Pick A Color&quot;, // dialog title<br>              null); // initial color</p>            <p> if(color != null) { <br>              oldColor = colorPanel.getBackground();<br>              colorPanel.setBackground(color);<br>              }<br>              }<br>              }<br>              class UndoAction extends AbstractAction {<br>              public UndoAction() {<br>              putValue(Action.NAME, undo.getUndoPresentationName());<br>              }<br>              public void actionPerformed(ActionEvent e) {<br>              String name = (String)getValue(Action.NAME);<br>              boolean isUndo = name.equals(<br>              undo.getUndoPresentationName());</p>            <p> if(isUndo) {<br>              undo.undo(); <br>              putValue(Action.NAME,<br>              undo.getRedoPresentationName());<br>              }<br>              else {<br>              undo.redo(); <br>

⌨️ 快捷键说明

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