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

📄 s19.htm

📁 提供给JAVA编程者图形界面卷
💻 HTM
📖 第 1 页 / 共 5 页
字号:
              TableCellRenderer renderer = <br>              table.getCellRenderer(r,c);</p>            <p> Component comp = <br>              renderer.getTableCellRendererComponent(<br>              table, table.getValueAt(r,c), <br>              false, false, r, c);</p>            <p> width = comp.getPreferredSize().width;<br>              maxw = width &gt; maxw ? width : maxw;<br>              }<br>              return maxw;<br>              }<br>              public static void main(String args[]) {<br>              GJApp.launch(<br>              new Test(),&quot;Setting Column Widths&quot;,300,300,320,140);<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 size="1" noshade>            <p align="center">&nbsp; </p>            <p> 19.4 表格列模型</p>            <p>&nbsp; </p>            <p> 19.4.1 DefaultTableColumnModel类</p>            <p>&nbsp;</p>            <p align="center">&nbsp; </p>            <p align="center">&nbsp; </p>            <p> 19.4.2 列边距</p>            <p>&nbsp;</p>            <p align="center"><b>例19-7 设置列边距</b></p>            <hr noshade size="1">            import java.awt.*;<br>            import java.awt.event.*;<br>            import javax.swing.*;<br>            import javax.swing.event.*;<br>            import javax.swing.table.*;<br>            import java.util.*;            <p>public class Test extends JFrame {<br>              JTable table = new JTable(<br>              new AbstractTableModel() {<br>              public int getRowCount() { return 10; }<br>              public int getColumnCount() { return 10; }</p>            <p> public Object getValueAt(int row, int col) {<br>              return &quot;(&quot; + Integer.toString(row) + &quot;,&quot; +<br>              Integer.toString(col) + &quot;)&quot;;<br>              }<br>              });</p>            <p> public Test() {<br>              Container cp = getContentPane();<br>              cp.add(new JScrollPane(table), BorderLayout.CENTER);<br>              cp.add(new ControlPanel(), BorderLayout.NORTH);<br>              }<br>              class ControlPanel extends JPanel {<br>              private JSlider slider = new JSlider(<br>              JSlider.HORIZONTAL,0,100,<br>              table.getColumnModel().getColumnMargin());</p>            <p> private JLabel label = new JLabel();</p>            <p> public ControlPanel() {<br>              add(new JLabel(&quot;Column Margin:&quot;));<br>              add(slider);<br>              add(label);</p>            <p> label.setText(<br>              Integer.toString(<br>              table.getColumnModel().getColumnMargin()));</p>            <p> slider.addChangeListener(new ChangeListener() {<br>              public void stateChanged(ChangeEvent e) {<br>              table.getColumnModel().setColumnMargin(<br>              slider.getValue());<br>              }<br>              });<br>              table.getColumnModel().addColumnModelListener(<br>              new TableColumnModelListener() {<br>              public void columnMarginChanged(ChangeEvent e) {<br>              TableColumnModel m = table.getColumnModel();<br>              label.setText(<br>              Integer.toString(m.getColumnMargin()));<br>              }</p>            <p> // unfortunately, Swing does not have many<br>              // event adapter classes ...<br>              public void columnAdded(TableColumnModelEvent e) {<br>              }<br>              public void columnMoved(TableColumnModelEvent e) {<br>              }<br>              public void columnRemoved(<br>              TableColumnModelEvent e) {<br>              }<br>              public void columnSelectionChanged(<br>              ListSelectionEvent e) {<br>              }<br>              });<br>              }<br>              }<br>              public static void main(String args[]) {<br>              GJApp.launch(new Test(),<br>              &quot;Column Margins&quot;,150,150,500,200); <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 size="1" noshade>            <p align="center">&nbsp; </p>            <p> 19.4.3 隐藏列</p>            <p>&nbsp;</p>            <p align="center"><b>例19-8 添加和删除列</b></p>            <hr noshade size="1">            import javax.swing.*;<br>            import javax.swing.table.*;<br>            import java.awt.*;<br>            import java.awt.event.*;<br>            import java.util.*;            <p>public class Test extends JFrame {<br>              JTable table = new JTable(<br>              new Object[][] {<br>              {&quot;Mouse&quot;, &quot;Mighty&quot;, &quot;M.&quot; },<br>              {&quot;Mouse&quot;, &quot;Polly&quot;, &quot;A.&quot; },<br>              {&quot;Doright&quot;, &quot;Dudley&quot;, &quot;L.&quot; }<br>              },<br>              new Object[] {<br>              &quot;Last Name&quot;, &quot;First Name&quot;, &quot;Middle Initial&quot;               <br>              }<br>              );<br>              public Test() {<br>              Container cp = getContentPane();</p>            <p> cp.add(new JScrollPane(table), BorderLayout.CENTER);<br>              cp.add(new ControlPanel(), BorderLayout.NORTH);<br>              }<br>              class ControlPanel extends JPanel {<br>              private JCheckBox checkBox = new JCheckBox(<br>              &quot;First Name Column Showing&quot;);<br>              public ControlPanel() {<br>              final TableColumnModel tcm = table.getColumnModel();<br>              final TableColumn firstNameColumn = <br>              table.getColumn(&quot;First Name&quot;);</p>            <p> checkBox.setSelected(true);<br>              add(checkBox);</p>            <p> checkBox.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent event) {<br>              if(checkBox.isSelected()) {<br>              tcm.addColumn(firstNameColumn);<br>              tcm.moveColumn(2,1);<br>              }<br>              else {<br>              tcm.removeColumn(firstNameColumn);<br>              }<br>              table.sizeColumnsToFit(-1);<br>              }<br>              });<br>              }<br>              }<br>              public static void main(String args[]) {<br>              GJApp.launch(<br>              new Test(), &quot;Showing/Hiding Columns&quot;,300,300,450,175);<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>

⌨️ 快捷键说明

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