2014年4月16日 星期三

Swing:元件:JCheckBox

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

class Frame9 extends JFrame{
 private JPanel contentPane;

 Frame9(){
  
  contentPane = new JPanel();
  add(contentPane);
  contentPane.setBounds(10, 20, 100, 120);
  Border line = BorderFactory.createLineBorder(Color.black);
  contentPane.setBorder(BorderFactory.createTitledBorder(line,"性別"));
  contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
  
  JCheckBox[] checkBox = new JCheckBox[2];
  checkBox[0] = new JCheckBox("男",true);
  checkBox[1] = new JCheckBox("女",false);
  
  for(int i=0; i < checkBox.length; i++)
   contentPane.add(checkBox[i]);
  
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setLayout(null);
  setBounds(0,0,450,300);
  setTitle("Swing JFrame 視窗");
  setVisible(true);
 }
}
public class FrameJCheckBox {

 public static void main(String[] args) {
  Frame9 frame = new Frame9();
 }

}

執行結果

沒有留言:

張貼留言