<pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

          <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

          <p id="bbfd9"></p>

          <p id="bbfd9"><cite id="bbfd9"></cite></p>

            <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

            <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
            <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
            java語言

            Java使用方法

            時間:2025-05-25 04:04:12 java語言 我要投稿

            Java使用方法集錦

              很多初學者在用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);

              }

              }

            【Java使用方法】相關文章:

            Java重載使用方法08-22

            Java重載的使用方法09-13

            java函數使用方法07-18

            java的hash Code的使用方法09-18

            java語言super使用方法09-28

            Java for循環語句的使用方法10-20

            Java語言相關使用方法08-27

            java File類的基本使用方法09-15

            Java語言Math類的使用方法09-08

                    <pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

                    <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

                    <p id="bbfd9"></p>

                    <p id="bbfd9"><cite id="bbfd9"></cite></p>

                      <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

                      <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
                      <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
                      飘沙影院