Java窗体

转载 2019-10-21 07:44

Java图形化界面

创建窗体对象

1
2
3
4
5
6
7
package 窗体;

import java.awt.Color;
import javax.swing.JFrame;
public static void main(String[] args) {
JFrame jf1= new JFrame("窗体1");
}

设置窗体的关闭状态

  1. DO_NOTHING_ON_CLOSE
  2. HIDF_ON_CLOSE
  3. DISPOSE_ON_CLOSE
  4. EXIT_ON_CLOSE
    1
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

设置窗体列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class MenuFrame extends JFrame{
//导航栏
JMenuBar bar;
//菜单
JMenu file,edit;
//菜单项
JMenuItem fnew,fopen,fclose,ecut,ecopy;
public MenuFrame() {
bar = new JMenuBar();
file = new JMenu("File");
fnew = new JMenuItem("new");
edit = new JMenu("Edit");
fopen = new JMenuItem("Fopen");
fclose = new JMenuItem("Fclose");
ecut = new JMenuItem("Ecut");
ecopy = new JMenuItem("Ecopy");
file.add(fnew);
file.add(edit);
file.add(fopen);
edit.add(fclose);
edit.add(ecut);
edit.add(ecopy);
bar.add(file);
bar.add(edit);
this.setJMenuBar(bar);
this.setVisible(true);
this.setBounds(200, 200, 400, 300);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}

给列表选项添加图标

1
file1 = new JMenuItem("销售员资料管理",new ImageIcon("img/salesman.gif"));

设置快捷键

  1. 单个快捷键
    1
    fnew.setAccelerator(KeyStroke.getKeyStroke('A'));
  2. 组合键
    1
    fopen.setAccelerator(KeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));

给窗体添加图片

  1. 创建一个Imgae对象

    1
    Image img;
  2. 获取图片

    1
    img = Toolkit.getDefaultToolkit().getImage("img/elms.jpg");
  3. 生成JPanel类的子类,并重写父类panintChildren()方法

    1
    2
    3
    4
    5
    6
    7
    8
    9

    backImgP = new JPanel() {
    //重写父类panintChildren()方法
    @Override
    protected void paintChildren(Graphics g) {
    g.drawImage(img,0, 0, this);
    super.paintChildren(g);
    }
    };
  4. 设置到界面

    1
    this.getContentPane().add(backImgP);

常用组件

  1. 组件实现形式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    class RegisterFrame extends JFrame{
    //定义一个文本框
    JTextField tfName;

    public RegisterFrame(){
    initComp();
    init();
    }
    窗体组件实现方法
    public void initComp(){
    //给定义的文本框new出空间和设置可输入长度
    tfName=new JTextField(10);
    //给前增加JLabel
    add(new JLabel("登录名:"));
    //传给主页面
    add(tfName);
    }
    //设置窗体大体样式方法
    public void init(){
    this.setLayout(new FlowLayout());
    this.setVisible(true);
    this.setBounds(200, 200, 380, 300);
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
    }
  2. JTextArea文本区:允许用户输入多行文本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
        //定义一个文本框
    JTextField tfName;
    public void initComp(){
    //给定义的文本框new出空间和设置可输入长度
    tfName=new JTextField(10);
    pfPassword=new JPasswordField(10);
    //给前增加JLabel
    add(new JLabel("登录名:"));
    //传给主界面
    add(tfName);
    }
  3. JBotton按钮

    1
    2
    3
    4
    5
    JButton jbConfirm;
    public void initComp(){
    jbConfirm=new JButton("确认");
    add(jbConfirm);
    }
  4. JRadioBotton单选按钮——单选要成组ButttonGroup;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    JRadioButton sex1,sex2,sex3;
    ButtonGroup sex;
    public void initComp(){
    sex=new ButtonGroup();
    sex.add(sex1);
    sex.add(sex2);
    sex.add(sex3);
    add(new JLabel("性别:"));
    add(sex1);
    add(sex2);
    add(sex3);
    }
  5. JComboBox下拉列表(复选框)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    //for循环设置下拉选项
    JComboBox cmbage;
    public void initComp(){
    cmbage=new JComboBox();
    for(int i=19;i<=70;i++)
    cmbage.addItem(i);
    add(new JLabel("年龄:"));
    add(cmbage);
    }

    //数组形式设置下拉选项
    JComboBox cmbaddress;
    String[] address={"烟台","威海","青岛"};
    public void initComp(){
    add(new JLabel("兴趣爱好:"));
    cmbaddress=new JComboBox(address);
    }
  6. JPasswordField密码框

    1
    2
    3
    4
    5
    6
    JPasswordField pfPassword;
    public void initComp(){
    pfPassword=new JPasswordField(10);
    add(new JLabel("密码:"));
    add(pfPassword);
    }

    常用容器

  7. JPanel面板
    JPanel默认布局是FlowLayout

  8. JTabbedPane选项卡
    JTabbedPane是可以作为中间容器,当用户该容器内添加一个容器时,它就会自动的为该组件选择相对应的选项卡。

  9. JScrollPane滚动窗口

  10. JSplitPane拆分方格

  11. JLayeredPane分层窗格

    常用布局

  12. FlowLayout布局

  13. BorderLayout布局

  14. CayrdLayout布局

  15. GridLayout布局

  16. null布局

  17. BoxLayout布局

    窗体实例 计算器界面

  18. 创建数组把标识存入数组

    1
    2
    3
    ·. String[] nOp = {"7","8","9","/","sqrt","4","5""6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
    String[] mOp = {"MC","MR","MS","M+"};
    String[] rOp = {"Back","CE","C"};
  19. 创建一个 JTextField

    1
    JTextField text = null;
  20. 对其进行初始化

    1
    2
    3
    4
    this.setTitle("计算器");  
    this.setSize(500, 400);
    this.setLocation(200, 200);
    this.setVisible(true);
  21. 创建底层面板并把 Text组件放在NORTH的位置

    1
    2
    3
    4
    JPanel panel = new JPanel();  
    panel.setLayout(new BorderLayout(5,5));
    text=new JTextField();
    panel.add(text, BorderLayout.NORTH);
  22. 编写侧栏

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    JPanel panel1 = new JPanel();  
    //设置面板布局方式
    panel1.setLayout(new GridLayout(5,1,0,10));
    //定义按钮数组 把定义好的数组把标识传给定义的按钮数组
    JButton[] mopButtons=new JButton[mOp.length+1];
    //new 出空间
    mopButtons[0]=new JButton();
    //添加到侧面面板
    panel1.add(mopButtons[0]);
    for(int i=0;i<mOp.length;i++){
    mopButtons[i+1]=new JButton();
    mopButtons[i+1].setText(mOp[i]);
    mopButtons[i+1].setForeground(Color.red);
    panel1.add(mopButtons[i+1]);
    }

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×