note1


JFrame

http://docs.oracle.com/javase/8/docs/api/

常用属性

    •  this.setAlwaysOnTop(true);
    •  this.setAutoRequestFocus(true);
    •  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    •  this.setResizable(true);
    •  this.setEnabled(true);
    •  this.setSize(100,200);
    • this.setOpacity((float)1.0);// 当前窗体被系统修饰以后,只能opacity给1.0
      this.setUndecorated(true);//当前窗体不被系统装饰, 最小化,最大化,关闭按钮都没有
                                  //要自己添加按钮进行关闭等操作
    • this.setType(Type.POPUP);
      //        this.setType(Type.NORMAL);
      //        this.setType(Type.UTILITY);  //根据type,系统修饰的方式略有不同
    • setVisible(true)
    • JPanel panel1 = new JPanel();
      this.setContentPane(panel1); 要给JFrame设置一个contentPane
      

JPanel

Container中可以放控件,或者其他容器。

常用属性

  • JPanel panel1 = new JPanel();
  • panel1.setBackground(Color.BLUE);
  • panel1.setLayout(new FlowLayout());  //JPanel default layout ==> borderlayout
  • panel1.setBorder(new TitledBorder("panel1"));

其他Containter


JLabel

常用属性

  • JLabel lbl1 = new JLabel();
  •  lbl1.setFont(new Font("MS Mincho", Font.BOLD, 14));
  •  lbl1.setText("lbl1");
  •  lbl1.setForeground(Color.BLACK);
  •  lbl1.setIcon(new ImageIcon(this.getClass().getResource("icon1.png")));
  • note1_第1张图片
    

  •  lbl1.setToolTipText("tool tip");
  • 文本对齐方式:
  •  lbl1.setHorizontalAlignment(SwingConstants.CENTER);
  •  lbl1.setVerticalAlignment(SwingConstants.TOP);

JButton

常用属性

JButton btn1 = new JButton("btn1");
        btn1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("1");
            }
        });

JTextField 单行文本框

常用属性

TextField jtf1 = new JTextField();
        jtf1.setEnabled(true);
        jtf1.setEditable(false);
        jtf1.setText("du text");

JTextArea 多行文本框

常用属性
        JTextArea ta = new JTextArea();
        ta.setRows(5);
        ta.setColumns(10);
        ta.setLineWrap(true);// default : false
 

JScrollPane

把ta放在JScrollPane中的viewportview中,然后把JScrollPane放在panel中

JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(ta);
        panel1.add(scrollPane);

JRadioButton,JCheckBox

把rb加到buttongroup中,但是jpanel中加的是rb

        JRadioButton rb1 = new JRadioButton("rb1");
        rb1.setSelected(true);
        System.out.println(rb1.isSelected());
        JRadioButton rb2 = new JRadioButton("rb2");
        JRadioButton rb3 = new JRadioButton();
        rb3.setText("rb3");
        ButtonGroup btnGr = new ButtonGroup();
        btnGr.add(rb1);
        btnGr.add(rb2);
        btnGr.add(rb3);
        panel1.add(rb1);
        panel1.add(rb2);
        panel1.add(rb3);

JList

给List设置model数据

        lstName = new JList<String>();
        lstName.setModel(new AbstractListModel<String>() {
            private String[] values = new String[]{"java", "c++", "c#"};
            @Override
            public int getSize() {
                return values.length;
            }
            @Override
            public String getElementAt(int index) {
                return values[index];
            }
        });

常用方法:

        lstName.getSelectedValue();
        lstName.getSelectedIndex();
        lstName.getSelectedIndices();
        lstName.getSelectedValuesList();
        lstName.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  • 如果不选择任何项,selectedIndex是-1.
  • 默认selectionmode是可选多行

JComboBox

        String[] values = new String[]{"java", "c++", "c#"};
        comboCours = new JComboBox<String>(values);
        comboCours.addItemListener(new ItemListener() {
            @Override
            public void <span style="background-color: rgb(255, 255, 102);">itemStateChanged</span>(ItemEvent e) {
                System.out.println(comboCours.getSelectedItem());
            }
        });
        panel1.add(comboCours);

JProgressBar

        progressBar = new JProgressBar();
        progressBar.setMinimum(0);
        progressBar.setMaximum(100);

用swingworker多线程控制JprogressBar:

panel1.add(progressBar);
        JButton btnPB = new JButton("add");
        btnPB.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new SwingWorker<String,Integer>(){

                    @Override
                    protected String doInBackground() throws Exception {
                        for (int i = 0; i <=10; i++ ) {
                            Thread.sleep(1000);//sleep for 1s
                            publish(i*10);
                        }
                        return null;
                    }

                    @Override
                    protected void process(List<Integer> chunks) {
                        for(Integer integer : chunks){
                            progressBar.setValue(integer);
                        }
                    }
                }.execute();
            }
        });
        panel1.add(btnPB);

JSpinner数字调节控件

model是数字:

        JSpinner spinner1 = new JSpinner();
        spinner1.setModel(new SpinnerNumberModel(60,0,100,1));
                            //int value, int minimum, int maximum, int stepSize
                            //double, double,double,double
        panel1.add(spinner1);

        int num = Integer.parseInt(spinner1.getValue().toString());
        System.out.println(num);

model是日期:

        JSpinner spinner2 = new JSpinner();
        spinner2.setModel(new SpinnerDateModel());
        panel1.add(spinner2);














你可能感兴趣的:(note1)