在java中没有找到类似于C++中的groupbox,搜了一下才知道是要用borderlayout和border实现的。
做笔记如下:
- import java.awt.*;
- import javax.swing.border.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Test5 {
- public static void main(String[] args) {
- SmallFrame sf = new SmallFrame();
- sf.setVisible(true);
- }
- }
- class SmallFrame extends JFrame {
- Container p;
- JLabel tip = new JLabel("请输入学号:");
- JTextField stuNoText = new JTextField();
- JPanel viewPane = new JPanel();
- JButton ok = new JButton("确定");
- JButton reset = new JButton("重置");
- public SmallFrame() {
- super("按学号查询");
- setBounds(200, 100, 350, 280);
- setResizable(false);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- p = getContentPane();
- p.setLayout(null);
- tip.setBounds(70, 20, 100, 20);
- tip.setForeground(Color.red);
- stuNoText.setBounds(155, 20, 100, 20);
- p.add(tip);
- p.add(stuNoText);
- ok.setBounds(90, 60, 60, 20);
- ok.setForeground(Color.green);
- p.add(ok);
- reset.setBounds(180, 60, 60, 20);
- reset.setForeground(Color.green);
- p.add(reset);
- //关键代码块
- TitledBorder tb = new TitledBorder(BorderFactory.createLineBorder(new Color(255, 0, 0)),"查询结果");
- tb.setTitleColor(Color.blue);
- viewPane.setBorder(tb);
- viewPane.setLayout(null);
- JLabel stuNoLabe = new JLabel("学号:");
- stuNoLabe.setBounds(80, 130, 80, 20);
- stuNoLabe.setForeground(new Color(34, 139, 34));
- p.add(stuNoLabe);
- JTextField stuNoTextf = new JTextField();
- stuNoTextf.setBounds(150, 130, 90, 20);
- p.add(stuNoTextf);
- JLabel stuNameLabe = new JLabel("姓名:");
- stuNameLabe.setBounds(80, 160, 80, 20);
- stuNameLabe.setForeground(new Color(34, 139, 34));
- p.add(stuNameLabe);
- JTextField nameTextf = new JTextField();
- nameTextf.setBounds(150, 160, 90, 20);
- p.add(nameTextf);
- JLabel stuAgeLabe = new JLabel("年龄:");
- stuAgeLabe.setBounds(80, 190, 80, 20);
- stuAgeLabe.setForeground(new Color(34, 139, 34));
- p.add(stuAgeLabe);
- JTextField ageTextf = new JTextField();
- ageTextf.setBounds(150, 190, 90, 20);
- p.add(ageTextf);
- viewPane.add(new JButton("HKJJ"));
- viewPane.setBounds(18, 100, 310, 141);
- p.add(viewPane);
- }
- }
上面程序的效果图: