2021-04-29 【Java 第31天】GUI(3)

3-3 用label展示图片

public ImageIconDemo() {
  //显示ImageIcon在label上
  JLabel jlabel = new JLabel("ImageIcon");
  URL url = getSource("D://JavaTest//test.png");
  ImageIcon imageIcon = new ImageIcon(url);
  label.setIcon(imageIcon);
  label.setAlignment(SwingConstants.CENTER);
  this.setVisible(true);
  
  Container container = this.getContentPane();
  container.add(label);
}

3-4 文本域,JSCroll面板

public JScrollDemo {
  Container container = this.getContentPane();
  
  //文本域
  JTextArea textArea = new JTextArea(20,30); //设定x大小为20,y大小为30
  textArea.setText("Arthur");

  //scroll面板
  JScrollPane scrollPane = new JScrollPane(textArea);
  container.add(scrollPane);
}

3-5 单选框&多选框
单选框:

JRadioButton radioButton1 = new JRadioButton();
JRadioButton radioButton2 = new JRadioButton();
//存放单选button
ButtonGroup buttongroup = new ButtonGroup();
bg.add(radioButton1);
bg.add(radioButton2);

多选框

JCheckBox checkBox01 = new JCheckBox("CheckBox01");
JCheckBox checkBox02 = new JCheckBox("CheckBox02");

3-6 下拉框、列表框
下拉框

JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("已下架");
status.addItem("正在上映");

列表框

//生成列表的内容
String[] contents = {"1", "2", "3"};
//列表中需要放入内容
JList jlist = new JList(contents);

3-7 文本框、密码框、文本域
文本框

JTextField textfield1 = new JTextField("hello");
JTextField textfield2 = new JTextField("world", 30); //30为最多输入字符的长度

密码框

JPasswordField passwordfield = new JPasswordField();
passwordfield.setEchoChar('a');

文本域

JTextArea textArea = new JTextArea(20,50);//设置row为20,col为50
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 界面是软件与用户交互的最直接的层,界面的好坏决定用户对软件的第一印象。而且设计良好的界面能够引导用户自己完成...
    A梦想才让心跳存在阅读 1,065评论 0 4
  • 一、文本框为字符型 必填项非空校验: 1、必填项未输入--程序应提示错误; 2、必填项只输入若干个空格,未输入其它...
    许小小晴阅读 4,669评论 0 2
  • OGNL表达式----UI标签:1.表单标签: 属性:Action/Method/namespacejavascr...
    任任任任师艳阅读 675评论 0 1
  • Menu 第1章 认识jQuery 1.3 jQuery代码的编写 1.4 jQuery对象和DOM对象 1.4....
    ft207741阅读 732评论 0 0
  • 表单(form) 网站怎样与用户进行交互?答案是使用HTML表单(form)。表单可以把浏览者输入的数据传送到服务...
    远山黛子阅读 313评论 0 0