阅读 110

Swing弹窗、组件、标签、面板、下拉框,文本框JFrame

Swing

JFrame窗口

package gui.lesson01.lesson03;
?
import javax.swing.*;
import java.awt.*;
?
public class TextJFrame{
   public static void main(String[] args) {
       new JFrame01().init();
  }
}
class JFrame01 extends JFrame {
   public void init(){
       this.setBounds(100,100,300,300);
       this.setVisible(true);
?
       JLabel jlabel = new JLabel("欢迎来到JFrame窗口");
       this.add(jlabel);
       //让文件标签居中,设置水平对齐
       jlabel.setHorizontalAlignment(SwingConstants.CENTER);
       //获得一个容器 (JFrame需要) Swing与AWT不同,
       Container contentPane = this.getContentPane();
       contentPane.setBackground(Color.yellow);   //设置容器的背景颜色
  }
}

JDialog弹窗

JDialog用来被弹出窗体,默认有关闭事件。

package gui.lesson01.lesson03;
?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
   public void init(){
       this.setBounds(100,100,300,300);
       this.setVisible(true);
       //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);   //设置窗口关闭,由于自带窗口
       Container container = this.getContentPane();   //JFrame放东西,容器
       container.setBackground(Color.yellow);
?
       JButton jButton = new JButton("点击会出现弹窗");   //创建按钮
       jButton.setBounds(100,100,100,50);
       //点击这个按钮,会弹出一个弹窗
       jButton.addActionListener(new ActionListener() {  //点击按钮的会发生事件     给按钮添加监听器
           @Override
           public void actionPerformed(ActionEvent e) {
               //出现弹窗,需要在后面写一个弹窗的类,相当于新建一个窗口,简单
               new MyDialog();
          }
      });
       container.add(jButton);  //在容器中添加按钮 (在JFrame中添加按钮时)
  }
?
   public static void main(String[] args) {
       new DialogDemo().init();
  }
}
//弹窗的这个类,弹窗的窗口
class MyDialog extends JDialog{   //继承弹窗JDialog这个类,这个类继承Windows类,就要重写JDialog这个类的方法
   public MyDialog() {
       this.setBounds(30,30,100,100);
       this.setVisible(true);
       //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       //setBackground(Color.RED); //无效,需要在容器中设置
?
       Container container = this.getContentPane();  //拿到container,组件最后会加到container上
       container.setLayout(null);  //布局是绝对定位,坐标是多少是固定的,游戏中常用
       container.add(new Label("学java"));  //容器中加入Lable标签  
  }
}

Image标签

package gui.lesson01.lesson03;
?
import javax.swing.*;
import java.awt.*;
import java.net.URL;
?
public class TextImage extends JFrame {
?
   public TextImage() {
       JLabel jLabel = new JLabel();  //创建一个标签,图片和图标都是放在标签上的
       URL url = TextImage.class.getResource("10.PNG");  //获取图片地址,获取当前这个class类下面的同级资源(照片放在这个类所处的包内)
?
       ImageIcon imageIcon = new ImageIcon(url);    //通过得到的本地写死的地址,创建一个图片的对象
       jLabel.setIcon(imageIcon);   //在标签上加入创建的图片
       jLabel.setHorizontalAlignment(SwingConstants.CENTER);  //标签放在是水平中间位置
?
       Container container = this.getContentPane();   //创建一个容器,方便把组件添加到容器上
       container.add(jLabel);    //把标签加到容器上
       this.setVisible(true);
       this.setBounds(100,100,300,400);   //对窗口进行设置
  }
?
   public static void main(String[] args) {
       new TextImage();
  }
}

icon标签

package gui.lesson01.lesson03;
?
import javax.swing.*;
import java.awt.*;
?
public class TextIcon extends JFrame implements Icon {
   private int width,height;   //因为要得到用户的输入,引入变量来传递储存
   public TextIcon(){}   //有了有参构造,必须需要有一个无参构造
?
   public TextIcon(int width,int height) {   //获取用户的输入
       this.height = height;
       this.width = width;      //后期可以用写好的模块代替
  }
?
   public void init(){
       TextIcon textIcon = new TextIcon(15, 15);   //new一个图标。
       JLabel jlabel = new JLabel("testIcon",textIcon,SwingConstants.CENTER);  //把图标放在标签上,标签名,对象大小,标签位置
       Container container = this.getContentPane();
       container.add(jlabel);
       setVisible(true);
  }
   public static void main(String[] args) {
       new TextIcon().init();
  }
?
   @Override            //继承这个Icon接口,就要重写它的方法   alt+enter implement methods
   public void paintIcon(Component c, Graphics g, int x, int y) {
       g.fillOval(x,y,width,height);    //创建一个对象后,重写的方法自动运行   重要
  }
?
   @Override
   public int getIconWidth() {
       return this.width;
  }
?
   @Override
   public int getIconHeight() {
       return this.height;
  }
}

面板布局JPanel

package gui.lesson01.lesson03;
?
import javax.swing.*;
import java.awt.*;
?
public class TextJscroll extends JFrame {
   public TextJscroll() {
       TextArea textArea = new TextArea();   //创建文本框
       JPanel jPanel = new JPanel();     //创建面板
       JPanel jPanel1 = new JPanel();    //创建另一个面板
       jPanel1.setLayout(new GridLayout(1,3));
?
       jPanel.add(textArea);     //把文本框放到面板上
       jPanel1.add(new Button("1"));   //在面板上放置按钮
       jPanel1.add(new Button("2"));
       jPanel1.add(new Button("3"));
?
       Container container = this.getContentPane();   //创建一个容器
       container.add(jPanel);   //把面板添加到容器上,三句简写一句,container.add(new JPanel().add(textArea));
       container.add(jPanel1);
       container.setLayout(new GridLayout(2,1,10,10));   //容器的布局模式:表格(列表)布局2行1列,行间距10,列间距10
?
       setVisible(true);
       setBounds(100,100,300,400);
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
?
   public static void main(String[] args) {
       new TextJscroll();
  }
}

JScroll面板(含滚动条)

package gui.lesson01.lesson03;
?
import javax.swing.*;
import java.awt.*;
?
public class ScrollDemo extends JFrame{
   public ScrollDemo() {
       JTextArea jTextArea = new JTextArea(20,50);   //创建可以换行的文本域,宽,高,用行数和每行可以写的字数来代替
       jTextArea.setText("欢迎进入文本域");   //设置文本域(文本框)默认的文本
?
       ScrollPane scrollPane = new ScrollPane();  //创建边框可以包含滚动条,跟着滚动条滚动的面板
       scrollPane.add(jTextArea);   //把文本域加到面板中
?
       Container container = this.getContentPane();
       container.add(scrollPane);   //把面板放到容器上
?
       setVisible(true);
       setBounds(100,100,300,400);
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  //关闭窗口
  }
?
   public static void main(String[] args) {
       new ScrollDemo();
  }
}

引入图片,图片按钮,图片标签

package gui.lesson01.lesson03;
?
import javax.swing.*;
import java.awt.*;
import java.net.URL;
?
public class TextPicture extends JFrame {
   public TextPicture() {
       Container container = this.getContentPane();
?
       URL resource = TextPicture.class.getResource("10.PNG");   //得到一个图片的路径URL
       Icon icon = new ImageIcon(resource);   //url可以变成一个图标,把路径变成一个图标(创建一个图标后面写上路径)
?
       JLabel jLabel = new JLabel();
       jLabel.setIcon(icon);          //把这个图标放在标签上
       jLabel.setToolTipText("图片标签");    //设置鼠标放到标签的时候的出现的提示文本框
       container.add(jLabel);         //在容器上加上标签
       this.setVisible(true);
       this.setBounds(100,100,300,400);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
?
   public static void main(String[] args) {
       new TextPicture();
  }
}

单选框

package gui.lesson01.lesson03;
?
import javax.swing.*;
import java.awt.*;
import java.net.URL;
?
public class TextPic extends JFrame {
   public TextPic() {
       Container container = this.getContentPane();
?
       URL resource = TextPicture.class.getResource("10.PNG");   //得到一个图片的路径URL
       Icon icon = new ImageIcon(resource);   //url可以变成一个图标,把路径变成一个图标
?
       JRadioButton jRadioButton1 = new JRadioButton("button1");    //创建一个单选框
       JRadioButton jRadioButton2 = new JRadioButton("button2");
       JRadioButton jRadioButton3 = new JRadioButton("button3");
?
       ButtonGroup buttonGroup = new ButtonGroup();   //由于单选框要实现只能选择一个,所以要创建一个组(分组),一个组中只能选择一个。
       buttonGroup.add(jRadioButton1);       //把单选框加入到一个组中
       buttonGroup.add(jRadioButton2);
       buttonGroup.add(jRadioButton3);
?
       container.add(jRadioButton1,BorderLayout.CENTER);    //把单选框加入到容器中
       container.add(jRadioButton2,BorderLayout.NORTH);
       container.add(jRadioButton3,BorderLayout.SOUTH);
?
       this.setVisible(true);
       this.setBounds(100,100,300,400);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
?
   public static void main(String[] args) {
       new TextPic();
  }
}

多选框

package gui.lesson01.lesson03;
?
import javax.swing.*;
import java.awt.*;
import java.net.URL;
?
public class TextPic01 extends JFrame {
   public TextPic01() {
       Container container = this.getContentPane();
?
       URL resource = TextPicture.class.getResource("10.PNG");   //得到一个图片的路径URL
       Icon icon = new ImageIcon(resource);   //url可以变成一个图标,把路径变成一个图标
?
       JCheckBox jCheckBox01 = new JCheckBox("jCheckBox01");   //创建多选框,如果单选框不分组,也类似于多选框
       JCheckBox jCheckBox02 = new JCheckBox("jCheckBox02");   //重要
?
       container.add(jCheckBox01,BorderLayout.NORTH);   //把多选框放到容器中,后面写布局方式的位置
       container.add(jCheckBox02,BorderLayout.SOUTH);
?
       this.setVisible(true);
       this.setBounds(100,100,300,400);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
?
   public static void main(String[] args) {
       new TextPic01();
  }
}

列表框

下拉框

package gui.lesson01.lesson04;
?
import javax.swing.*;
import java.awt.*;
?
public class TextCombox extends JFrame {
   public TextCombox() {
       Container container = this.getContentPane();
?
       JComboBox status = new JComboBox();    //创建一个下拉框
       status.addItem(null);    //添加下拉框中的的Item选项,在status中添加各种状态
       status.addItem("正在热映");
       status.addItem("即将上映");
       status.addItem("已下架");
?
       //status.addActionListener();     //可以获取用户选中的值,再用getresource赋给自己的对象变量,再用gettext获取文本值
       
       container.add(status);
?
       this.setVisible(true);
       this.setBounds(100,100,300,400);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
?
   public static void main(String[] args) {
       new TextCombox();
  }
}

列表框

package gui.lesson01.lesson04;
?
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
?
public class TextCombobox01 extends JFrame {
   public TextCombobox01() {
       Container container = this.getContentPane();
?
       //String[] contents = {"1","2","3"};   //创建并赋值一个静态数组
       Vector vector = new Vector();   //new一个集合里的一种数据类型Vector
       JList jList = new JList(vector);   //用数据结构可以动态的添加内容,因为这里面放的是一个引用new JList(Vector),所以可以先放进来,再赋值。
       vector.add("zhangsan");   //把值赋给到Vector的内存中,就可以获得值
       vector.add("lisi");       //往Vector里面动态的添加值,动态操作Vector,重新绘制,就能动态做列表框了
       vector.add("wanger");
       //JList jList = new JList(contents);   //创建一个列表框,并且将数组的值放入列表框中
       //container.add(jList);
?
       container.add(jList);    //把列表框放到容器中
       this.setVisible(true);
       this.setBounds(100,100,300,400);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
?
   public static void main(String[] args) {
       new TextCombobox01();
  }
}

应用场景:1、选择地区,关联性,中国,后面出现中国的城市,下拉框。

2、列表用来展示信息,一般是动态扩容,类似聊天室建房。

原文:https://www.cnblogs.com/zhzred/p/14438242.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐