ºìÁªLinuxÃÅ»§
Linux°ïÖú

Java²¼¾Ö¹ÜÀíÆ÷ʹÓ÷½·¨Ì½ÌÖ

·¢²¼Ê±¼ä:2006-12-02 11:27:51À´Ô´:ºìÁª×÷Õß:smallwl
¡¡¡¡ºÜ¶à³õѧÕßÔÚÓÃJava²¼¾ÖÆ÷×Ô¶¯²¼¾Ö»­½çÃæʱ£¬¾­³£Óö¼û²»ÖªµÀÈçºÎ¶¨ÒåÇøÓò´óС»ò°´Å¥Ö®¼äµÄ¾àÀëµÈÎÊÌâ¡£ÎÒд¹ýһƪ¡¶ÊµÏÖJAVAÊÖ¶¯²¼¾ÖÖи÷¸ö×é¼þÄÜËæ´°¿Ú±ä»¯µÄ·½·¨¡·µÄÎÄÕ£¬ÓжÁÕß·´Ó³Ëã×ø±ê²»ºÃË㣬ÎÊÄܲ»ÄÜÓò¼¾ÖÆ÷ʵÏÖÎÄÕÂÖеĽçÃæ¡£Æäʵ×Ô¶¯²¼¾ÖÒ²¿ÉÒÔ½â¾ö¶¨ÒåÇøÓò´óС»ò°´Å¥Ö®¼äµÄ¾àÀëµÈÎÊÌ⣬ֻÊÇûÓÐÊÖ¶¯²¼¾ÖÄÇôÁé»î¡£ÏÂÃæÎҾ;ÙÒ»¸öÀý×Ó¡£

¡¡¡¡Ê×ÏÈ£¬½¨Ò»¸öframeÎļþ£¨ApplicationÓ¦ÓóÌÐò£©£¬ÔÚDesignÖн«thisÖеÄlayoutÉèÖÃΪBorderLayout¡£

¡¡¡¡µÚ¶þ£¬ÔÚ×é¼þÅÌÄÚµãÑ¡Swing Containerҳǩ£¬Ñ¡È¡Jpanelͼ±ê£¬ÔÚthisÖÐÉÏ·½ÍÏקһ¿éÇøÓò£¬²¼¾ÖÆ÷»á×Ô¶¯µ÷ÕûλÖÃÓë´óС£»Í¬ÑùµÄ·½·¨ÔÚÖÐÏ·½Ò²ÍÏקһ¿éÇøÓò£»ÔÚSwing Containerҳǩ£¬Ñ¡È¡jScrollPaneͼ±ê£¬½«jScrollPaneÔÚÖмäÍÏקһ¿éÇøÓò¡£ÍÏקµÄ˳ÐòÒ»¶¨ÒªÏÈÉϺóÏÂÔÙÖм䡣ΪÁË·½±ãÇø·Ö£¬ÔÚPropertiesµÄbackgroundÖУ¬½«ÉÏ·½µÄJpanel1ÇøÓòÉèÖÃΪºìÉ«£¬Ï·½µÄJpanel2ÇøÓòÉèÖÃΪ³ÈÉ«£¬ÖмäµÄjScrollPane1Ϊ·ÛºìÉ«¡£½«Jpanel1ºÍJpanel2µÄlayoutÉèÖÃΪflowLayout£¨±ØÐëÒªÊÖ¶¯ÉèÖ㬲»Òª²ÉÓÃĬÈÏÖµ£©¡£

¡¡¡¡µÚÈý£¬ÔÚJpanelÖзÅÈëÒ»¸öJlable±êÌâÀ¸,JTextField1Îı¾¿òºÍJbutton°´Å¥£¬ÔÚ×é¼þÅÌÄÚµãÑ¡Swing ҳǩ£¬Ñ¡È¡JLableͼ±êÔÚJpanel1µÄÖл­Ò»¸ö±êÌâÀ¸£¬½«text¸ÄΪ¡°ÇëÊäÈë²éѯÌõ¼þ¡±£¬ÔÙÑ¡È¡JtextFieldÔÚJpanel1Öл­Ò»¸öÎı¾¿ò£¬½«text¸ÄΪ¿Õ£¬×îºóÑ¡È¡JbuttonÔÚJpanel1ÖÐÔÙ»­Ò»¸ö°´Å¥½«text¸ÄΪ¡°²éѯ¡±¡£»­ÍêºóËûÃǶ¼ÊÇÔÚÖм䣬¶øÇÒ´óС¹Ì¶¨£¬ÕâʱµãÑ¡JpanelµÄflowLayout1½«ÓÒ±ßPropertiesÖеÄalignmentÉèÖÃΪLEFT£¬ÕâʱJpanel1ÖеÄ×é¼ü¾Í»áÏò×óÅÅÁС£Ñ¡ÖÐÆäÖÐÒ»¸ö×é¼ü£¬ÔÚPropertiesÖеÄpreferredSize¿ÉÒÔÉèÖÃ×é¼üµÄ¿íºÍ¸ß¡£Í¬ÑùµÄ·½·¨ÔÚJpanel2Öл­Èý¸öJbutton°´Å¥£¬½«text·Ö±ðÉèΪ¡°Ôö¼Ó¡±¡¢¡°É¾³ý¡±¡¢¡°Ð޸ġ±¡£µãÑ¡Jpane2µÄflowLayout2½«ÓÒ±ßPropertiesÖеÄhgapÉèÖÃΪ30£¨°´Å¥µÄ¼ä¾à£¬¿É¸ù¾Ý×Ô¼ºµÄÐèÒªµ÷ÕûÊýÖµ´óС£©, ÕâÑù¾Íµ÷ÕûÁËÈý¸ö°´Å¥Ö®¼äµÄ¾àÀ룬ÉèÖÃvgap»¹¿ÉÒԸıäJpane2ÇøÓòµÄ¸ß¶È¡£

¡¡¡¡µÚËÄ£¬ÔÚjScrollPane1Öн¨Ò»¸ö±í¸ñÓÃÀ´ÏÔʾÊý¾Ý¿âÊý¾ÝµÄÄÚÈÝ£¬ÔÚ×é¼þÅÌÄÚµãÑ¡Swing ҳǩ£¬Ñ¡È¡JTableͼ±ê£¬½«Jtable¼ÓÈëµ½jScrollPane1ÖС£

¡¡¡¡×îºó£¬½«thisÖеÄdefaultCloseOperation¸ÄΪEXIT_ON_CLOSE,ÕâÑùÔڹرմ°¿Úʱ³ÌÐò»á×Ô¶¯Í˳ö¡£

¡¡¡¡³ÌÐòÔ´´úÂëÈçÏ£¨³ýÖÐÎÄ×¢ÊͲ¿·ÖµÄÁ½¾äÊÇ×Ô¼º¼ÓÉÏÈ¥£¬ÆäÓàÊÇ×Ô¶¯Éú³É£©£º

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
public class Frame1
extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
FlowLayout flowLayout1 = new FlowLayout();
FlowLayout flowLayout2 = new FlowLayout();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
GridLayout gridLayout1 = new GridLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable();

public Frame1() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Frame1 frame1 = new Frame1();
frame1.setSize(new Dimension(400, 350));
frame1.show();

}

private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
jPanel1.setBackground(Color.red);
jPanel1.setLayout(flowLayout1);
jPanel2.setBackground(Color.red);
jPanel2.setLayout(flowLayout2);
jPanel3.setBackground(Color.pink);
jPanel3.setLayout(gridLayout1);
jLabel1.setPreferredSize(new Dimension(100, 16));
jLabel1.setText("ÇëÊäÈë²éѯÌõ¼þ");
jTextField1.setPreferredSize(new Dimension(140, 22));
jTextField1.setText("");
jButton1.setText("²éѯ");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
flowLayout1.setAlignment(FlowLayout.LEFT);
flowLayout1.setHgap(5);
flowLayout1.setVgap(10);
jButton2.setText("Ôö¼Ó");
jButton3.setText("ɾ³ý");
jButton4.setText("ÐÞ¸Ä");
flowLayout2.setHgap(30);
flowLayout2.setVgap(5);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().add(jPanel1, BorderLayout.NORTH);
jPanel1.add(jLabel1, null);
jPanel1.add(jTextField1, null);
jPanel1.add(jButton1, null);
this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
jPanel2.add(jButton2, null);
jPanel2.add(jButton3, null);
jPanel2.add(jButton4, null);
this.getContentPane().add(jPanel3, BorderLayout.CENTER);
jPanel3.add(jScrollPane1, null);
jScrollPane1.getViewport().add(jTable1, null);
}

//Ä£Äâ²éѯÊý¾Ý¿â
void jButton1_actionPerformed(ActionEvent e) {
try { //ÖÆ×÷±í
Vector vcol = new Vector(); //ÁÐÃû
Vector vrow = new Vector(); //ÄÚÈÝ
for (int col = 1; col < 31; col++) {
vcol.addElement("ÁÐ" + col);
}
for (int row = 1; row < 101; row++) {
Vector vr1 = new Vector();
for (int col = 1; col < 31; col++) {
vr1.addElement(row + "/" + col);
}
vrow.addElement(vr1);
}
DefaultTableModel dtm = new DefaultTableModel(vrow, vcol);
jTable1 = new JTable(vrow, vcol);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //¹ö¶¯ÌõÉèÖÃ×óÓÒ¹ö
this.jScrollPane1.getViewport().add(jTable1, null); //ÔÚ¹ö¶¯ÌõÖзÅÈë±í
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}

}
}

class Frame1_jButton1_actionAdapter
implements java.awt.event.ActionListener {
Frame1 adaptee;

Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. 190.7.14.* ÓÚ 2007-07-06 00:42:51·¢±í:

    http://84247e780d7de5cd5b99fd72e3ae0ded-t.lwgmrw.org 84247e780d7de5cd5b99fd72e3ae0ded http://84247e780d7de5cd5b99fd72e3ae0ded-b1.lwgmrw.org 84247e780d7de5cd5b99fd72e3ae0ded http://84247e780d7de5cd5b99fd72e3ae0ded-b3.lwgmrw.org 7323937625928ec2c2b389a5c949efe8