swing 中的关闭事件系统自带 可以直接调用
Component.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Component可以是JFrame也可以是Jpanel
需要构造一个容器
Container container = this.getContentPane();
窗口,面板的背景颜色需要调用容器的setBackground(Color)方法设置
this.setBounds()方法用于设置窗口,面板…的位置和大小
this.setVisible(true); 设置可见性
这些方法基本上在所有需要使用swing语句的类中都要使用
窗口
JFrame
创建窗口类 需要 extends JFrame
初始化方法
init(){}
创建一个窗口的构造方法
new JFrame.var
设置文字Lable(窗口左上角的lable)
JLabel jLabel = new JLabel("String ");
class Myframe02 extends JFrame{
public void init(){
this.setVisible(true);
this.setBounds(100,100,400,400);
JLabel label = new JLabel("Swing");
this.add(label);
//让文本标签居中
label.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.CYAN);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
面板
Jpanel
窗口只是最外层的框架,基本上所有的操作都是在面板中进行的,
需要构造窗口 再用container.add 将Jpanel添加到JFrame中去
虽然用this.add也可以 但是如果窗口中的组件多了用this.add会使得组件不那么协调
也可以在contianer的setlayout方法中设置组件的添加形式
设置有滚动条的面板
JScrollPane jScrollPane = new JScrollPane();
public class JScrolPanelDemo extends JFrame {
public JScrolPanelDemo(){
Container container = this.getContentPane();
//文本框
TextArea textArea = new TextArea(20,50);//行,列
textArea.setText("文本框");
JScrollPane jScrollPane = new JScrollPane(textArea);//设置有滚动条的面板
container.add(jScrollPane);
this.setVisible(true);
this.setSize(400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrolPanelDemo();
}
}
按钮
JButton
获得图片图标
类名.class.getResource(“路径”)
URL resource = JButtonDemo01.class.getResource(“1910300210 19软工1班 严昌敬 创新作业.jpg”);//只能是相对路径,所以图片要放在该类所在的文件夹下
Icon icon = new ImageIcon(resource); 将图片转化为图标
JButton button=new JButton(icon);
鼠标放上去的悬浮提示
button.setToolTipText(“图片图标”);
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
URL resource = JButtonDemo01.class.getResource("1910300210 19软工1班 严昌敬 创新作业.jpg");
Icon icon = new ImageIcon(resource);
JButton button=new JButton(icon);
button.setToolTipText("图片图标");//鼠标放上去的悬浮提示
container.add(button);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,300);
this.setVisible(true);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选按钮
new JRadioButton();先创建按钮
ButtonGroup group = new ButtonGroup(); 在创建一个ButtonGroup 一个Group中只能选择一个按钮从而达到单选的目的
然后把每个JRadioButton添加到 group中去
group.add(JRadioButton)
然后再将每个JRadioButton放到container中
public class JButtonDemo02 extends JFrame{
public JButtonDemo02() {
Container container = this.getContentPane();
//设置单选按钮
JRadioButton radioButton1 = new JRadioButton("radioButton1");
JRadioButton radioButton2 = new JRadioButton("radioButton2");
JRadioButton radioButton3 = new JRadioButton("radioButton3");
//要实现单选 就要把多个选项放在group里面 一个group只能选一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1,BorderLayout.CENTER);
container.add(radioButton2,BorderLayout.NORTH);
container.add(radioButton3,BorderLayout.SOUTH);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,300);
this.setVisible(true);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
多选按钮
new JCheckBox()
然后把创造的多个CheckBox放到container中去
public class JButtonDemo03 extends JFrame {
public JButtonDemo03() {
Container container = this.getContentPane();
//设置多选按钮
JCheckBox jCheckBox01 = new JCheckBox("choose01");
JCheckBox jCheckBox02 = new JCheckBox("choose02");
JCheckBox jCheckBox03 = new JCheckBox("choose03");
container.setLayout(new FlowLayout());
container.add(jCheckBox01);
container.add(jCheckBox02);
container.add(jCheckBox03);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,300);
this.setVisible(true);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}