java Border/BorderLayout 實現GroupBox

來源:互聯網
上載者:User

在java中沒有找到類似於C++中的groupbox,搜了一下才知道是要用borderlayout和border實現的。

做筆記如下:

Code:
  1. import java.awt.*;   
  2. import javax.swing.border.*;   
  3. import java.awt.event.*;   
  4. import javax.swing.*;   
  5.   
  6. public class Test5 {   
  7.     public static void main(String[] args) {   
  8.         SmallFrame sf = new SmallFrame();   
  9.         sf.setVisible(true);   
  10.     }   
  11. }   
  12.   
  13. class SmallFrame extends JFrame {   
  14.     Container p;   
  15.     JLabel tip = new JLabel("請輸入學號:");   
  16.     JTextField stuNoText = new JTextField();   
  17.     JPanel viewPane = new JPanel();   
  18.     JButton ok = new JButton("確定");   
  19.     JButton reset = new JButton("重設");   
  20.     public SmallFrame() {   
  21.         super("按學號查詢");   
  22.         setBounds(200, 100, 350, 280);   
  23.         setResizable(false);   
  24.         setDefaultCloseOperation(EXIT_ON_CLOSE);   
  25.         p = getContentPane();   
  26.         p.setLayout(null);   
  27.         tip.setBounds(70, 20, 100, 20);   
  28.         tip.setForeground(Color.red);   
  29.         stuNoText.setBounds(155, 20, 100, 20);   
  30.         p.add(tip);   
  31.         p.add(stuNoText);   
  32.         ok.setBounds(90, 60, 60, 20);   
  33.         ok.setForeground(Color.green);   
  34.         p.add(ok);   
  35.         reset.setBounds(180, 60, 60, 20);   
  36.         reset.setForeground(Color.green);   
  37.         p.add(reset);   
  38.         //關鍵代碼塊   
  39.         TitledBorder tb = new TitledBorder(BorderFactory.createLineBorder(new Color(255, 0, 0)),"查詢結果");   
  40.         tb.setTitleColor(Color.blue);   
  41.         viewPane.setBorder(tb);   
  42.         viewPane.setLayout(null);   
  43.         JLabel stuNoLabe = new JLabel("學號:");   
  44.         stuNoLabe.setBounds(80, 130, 80, 20);   
  45.         stuNoLabe.setForeground(new Color(34, 139, 34));   
  46.         p.add(stuNoLabe);   
  47.         JTextField stuNoTextf = new JTextField();   
  48.         stuNoTextf.setBounds(150, 130, 90, 20);   
  49.         p.add(stuNoTextf);   
  50.         JLabel stuNameLabe = new JLabel("姓名:");   
  51.         stuNameLabe.setBounds(80, 160, 80, 20);   
  52.         stuNameLabe.setForeground(new Color(34, 139, 34));   
  53.         p.add(stuNameLabe);   
  54.         JTextField nameTextf = new JTextField();   
  55.         nameTextf.setBounds(150, 160, 90, 20);   
  56.         p.add(nameTextf);   
  57.         JLabel stuAgeLabe = new JLabel("年齡:");   
  58.         stuAgeLabe.setBounds(80, 190, 80, 20);   
  59.         stuAgeLabe.setForeground(new Color(34, 139, 34));   
  60.         p.add(stuAgeLabe);   
  61.         JTextField ageTextf = new JTextField();   
  62.         ageTextf.setBounds(150, 190, 90, 20);   
  63.         p.add(ageTextf);   
  64.            
  65.         viewPane.add(new JButton("HKJJ"));   
  66.         viewPane.setBounds(18, 100, 310, 141);   
  67.         p.add(viewPane);   
  68.     }   
  69. }  

上面程式的:

                                 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.