Day 7 狂神说Java基础笔记(GUI18-22)

18-喝了一口酒.图片按钮,单选框,多选框

import java.awt.Container;
import java.net.URL;
import javax.swing.*;
//主窗口
public class Hello{
 public static void main(String[] args ) {
  new JButtonDemo();
  }
 }
class JButtonDemo extends JFrame{
 public JButtonDemo() {
  // TODO Auto-generated constructor stub
  Container container = this.getContentPane();
  URL url = Hello.class.getResource("zr.jpg");
  ImageIcon imageIcon= new ImageIcon(url);
  JButton jButton =new JButton();
  jButton.setIcon(imageIcon);
  jButton.setToolTipText("sb");
  container.add(jButton);
  this.setVisible(true);
  this.setSize(700, 700);
  this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 }
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.net.URL;
import javax.swing.*;
//主窗口
public class Hello{
 public static void main(String[] args ) {
  new JButtonDemo();
  }
 }
class JButtonDemo extends JFrame{
 public JButtonDemo() {
  // TODO Auto-generated constructor stub
  Container container = this.getContentPane();
  //单选框
  JRadioButton radioButton1=new JRadioButton("1");
  JRadioButton radioButton2=new JRadioButton("2");
  JRadioButton radioButton3=new JRadioButton("3");
  //由于单选框只能选择一个,需要分组
  ButtonGroup group=new ButtonGroup();
  group.add(radioButton1);
  group.add(radioButton2);
  group.add(radioButton3);    
  container.add(radioButton1);
  container.add(radioButton2);
  container.add(radioButton3);  
  //多选框  JCheckBox
  JCheckBox jCheckBox1 =new JCheckBox("1");
  JCheckBox jCheckBox2 =new JCheckBox("2");
  JCheckBox jCheckBox3 =new JCheckBox("3");
  container.add(jCheckBox1);
  container.add(jCheckBox2);
  container.add(jCheckBox3);
  container.setLayout(new GridLayout(2,3));
  this.setVisible(true);
  this.setSize(700, 700);
  this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 }
}

19-下拉框、列表框

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.net.URL;
import javax.swing.*;
//主窗口
public class Hello{
 public static void main(String[] args ) {
  new JButtonDemo();
  }
 }
class JButtonDemo extends JFrame{
 public JButtonDemo() {
  // TODO Auto-generated constructor stub
  Container container = this.getContentPane();
  //下拉框
  JComboBox jComboBox=new JComboBox();
  jComboBox.addItem("y");
  jComboBox.addItem("y");
  jComboBox.addItem("i");
  container.add(jComboBox);
  //列表框
  String[] contentStrings= {"1","2","3"};
  JList jList =new JList(contentStrings);
  container.add(jList);
  container.setLayout(new FlowLayout());
  this.setVisible(true);
  this.setSize(700, 700);
  this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 }
}

vector:动态数据

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.net.URL;
import java.util.Vector;
import javax.swing.*;
//主窗口
public class Hello{
 public static void main(String[] args ) {
  new JButtonDemo();
  }
 }
class JButtonDemo extends JFrame{
 public JButtonDemo() {
  // TODO Auto-generated constructor stub
  Container container = this.getContentPane();
  //String[] contentStrings= {"1","2","3"};
  Vector contentVector =new Vector();
  contentVector.add("1");
  contentVector.add("1");
  contentVector.add("1");
  contentVector.add("1");
  JList jList =new JList(contentVector);
  container.add(jList);
  container.setLayout(new FlowLayout());
  this.setVisible(true);
  this.setSize(700, 700);
  this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 }
}
  • 下拉框,选择地区或者一些单个选项,如果只有两个,可以选择单选框按钮,节省布局
  • 列表,展示信息,一般动态扩容

20-文本框密码框文本域

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.net.URL;
import java.util.Vector;
import javax.swing.*;
//主窗口
public class Hello{
 public static void main(String[] args ) {
  new JButtonDemo();
  }
 }
class JButtonDemo extends JFrame{
 public JButtonDemo() {
  // TODO Auto-generated constructor stub
  Container container = this.getContentPane();
  //文本框
  JTextField textField1=new JTextField("heo");
  JTextField textField2=new JTextField("wo",20);
  container.add(textField2);
  container.add(textField1);
  //密码框
  JPasswordField jPasswordField=new JPasswordField(20);
  jPasswordField.setEchoChar('*');
  container.add(jPasswordField);
  //文本域
  //JTextArea
  container.setLayout(new FlowLayout());
  this.setVisible(true);
  this.setSize(700, 700);
  this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 }
}

####21-贪吃蛇之界面绘制

####22-绘制静态小蛇

你可能感兴趣的:(狂神学习)