其他分享
首页 > 其他分享> > Swing

Swing

作者:互联网

Swing

界面

package com.ar.chapter03;
import javax.swing.*;


import java.awt.*;

public class JFrameDemo02 {
  public static void main(String[] args) {
      new MyJFrame02().init();
  }
}
class MyJFrame02 extends JFrame{
  public void init(){
      this.setBounds(100,100,400,200);
      this.setVisible(true);
      JLabel jLabel=new JLabel("阿娆");
      this.add(jLabel);
      //文本居中
      jLabel.setHorizontalAlignment(SwingConstants.CENTER);
      Container container= this.getContentPane();
      container.setBackground(Color.red);
  }
}
package com.ar.chapter03;

import javax.swing.*;
import java.awt.*;
public class JFrameDome {
   //初始化
   public void init(){
       JFrame jFrame=new JFrame("Swing窗口");
       jFrame.setVisible(true);
       jFrame.setBounds(100,100,400,200);
       //设置文字
       JLabel jLabel=new JLabel("ar");

       jFrame.add(jLabel);
       //容器实例化
       jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
   public static void main(String[] args) {
       new JFrameDome().init();
  }
}

弹窗

image-20211204171354176

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DialongDemo extends JFrame {
  //
  public DialongDemo(){
      this.setVisible(true);
      this.setSize(700,1500);
      this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      //放入新窗口
      Container container = this.getContentPane();
      //绝对布局
      container.setLayout(null);
      //按钮
      JButton button = new JButton("弹出窗口");
      button.setBounds(30,30,200,50);
      //监听事件,点击是弹出一个弹窗
      button.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              //弹窗
              new MyDialogDemo();
          }
      });
      container.add(button);
   

  }
  public static void main(String[] args) {
      new DialongDemo();
  }
}
//弹窗的窗口
class MyDialogDemo extends JDialog {
  public MyDialogDemo(){
      this.setVisible(true);
      this.setSize(100,100);
      this.setBounds(100,100,500,500);
    // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      Container container = new Container();
      container.setLayout(null);
      container.add(new Label("阿娆"));

  }
}

image-20211204172050232

点击按钮,弹出弹窗

标签

label

new label("阿娆")

图标Icon

import javax.swing.*;
import java.awt.*;

//图标是个接口,需要实现类,Frame继承
public class IconDemo extends JFrame implements Icon {
  private int width;
  private int height;
//无参构造
  public IconDemo() {
  }

  public IconDemo(int width, int height) {
      this.width = width;
      this.height = height;
  }

  public void init() {
      IconDemo iconDemo = new IconDemo(15, 15);
      JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);

      Container container = getContentPane();
      container.add(label);

      this.setVisible(true);
      this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

  @Override
  public void paintIcon(Component c, Graphics g, int x, int y) {
      g.fillOval(x, y, width, height);
  }

  @Override
  public int getIconWidth() {
      return width;
  }

  @Override
  public int getIconHeight() {
      return height;
  }

  public static void main(String[] args) {
      new IconDemo().init();
  }

}

image-20211204173939470

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class IconDemo0 extends JFrame {
   public IconDemo0() {
       //获取图片地址
       JLabel label = new JLabel("ImageIcon");
       URL url = IconDemo0.class.getResource("某个图片的位置");
       ImageIcon imageIcon = new ImageIcon(url);
       label.setIcon(imageIcon);
       label.setHorizontalAlignment(SwingConstants.CENTER);

       Container container = getContentPane();
       container.add(label);
       setVisible(true);
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

   public static void main(String[] args) {
       new IconDemo0();
  }
}

面板

JPanel

import javax.swing.*;
import java.awt.*;

public class Panel extends JFrame
{

           public Panel() {
           Container container = this.getContentPane();
           container.setLayout(new GridLayout(2, 1, 10, 10)); //后面的参数是间距

           JPanel panel1 = new JPanel(new GridLayout(1, 3));
           JPanel panel2 = new JPanel(new GridLayout(1, 2));
           JPanel panel3 = new JPanel(new GridLayout(2, 1));
           JPanel panel4 = new JPanel(new GridLayout(3, 2));

           panel1.add(new JButton("1"));
           panel1.add(new JButton("1"));
           panel1.add(new JButton("1"));
           panel2.add(new JButton("1"));
           panel2.add(new JButton("1"));
           panel3.add(new JButton("1"));
           panel3.add(new JButton("1"));
           panel4.add(new JButton("1"));
           panel4.add(new JButton("1"));
           panel4.add(new JButton("1"));
           panel4.add(new JButton("1"));
           panel4.add(new JButton("1"));
           panel4.add(new JButton("1"));

           container.add(panel1);
           container.add(panel2);
           container.add(panel3);
           container.add(panel4);

           this.setVisible(true);
           this.setSize(200, 200);
           this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

       public static void main(String[] args) {
           new Panel();
      }
  }

image-20211204174825459

import javax.swing.*;
import java.awt.*;

public class Panel02 extends JFrame{
   public Panel02() {
       Container container = this.getContentPane();
       JTextArea textArea = new JTextArea(20, 50);
       textArea.setText("JTextArea");

       JScrollPane scrollPane = new JScrollPane(textArea);

       container.add(scrollPane);


       this.setVisible(true);
       this.setBounds(100, 100, 300, 500);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

   public static void main(String[] args) {
       new Panel02();
  }
}

image-20211204175418524

按钮

图片按钮

   public class JButtonDemo01 extends JFrame {
   public JButtonDemo01() {
       Container container = this.getContentPane();
       URL url = JButtonDemo01.class.getResource("tx.jpg");
       Icon icon = new ImageIcon(url);
  //把图标放到按钮上
   JButton button = new JButton();
   button.setIcon(icon);
   button.setToolTipText("图片按钮");

   container.add(button);

   this.setVisible(true);
   this.setSize(500, 500);
   this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

单选按钮

import javax.swing.*;
import java.awt.*;

public class JButtonDemo02 extends JFrame {
  public JButtonDemo02() {
      Container container = this.getContentPane();
      //单选框
      JRadioButton jRadioButton1 = new JRadioButton("01");
      JRadioButton jRadioButton2 = new JRadioButton("02");
      JRadioButton jRadioButton3 = new JRadioButton("03");
      //由于单选框只能选择一个,分组一个组中只能
      // 刚掉选一个可以多选啦
//       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.setSize(500, 500);
      this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
  public static void main(String[] args) {
      new JButtonDemo02();
  }
}

多线按钮

public class JButtonDemo02 extends JFrame {
  public JButtonDemo02() {
      Container container = this.getContentPane();
      Checkbox checkbox1 = new Checkbox("01");//单词
      Checkbox checkbox2 = new Checkbox("02");
      container.add(checkbox1, BorderLayout.NORTH);
      container.add(checkbox2, BorderLayout.SOUTH);
      this.setVisible(true);
      this.setSize(500, 500);
      this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
  public static void main(String[] args) {
      new JButtonDemo02();
  }
}

image-20211204182643036

image-20211204182655771

列表

 

import javax.swing.*;
import java.awt.*;

class TestComboboxDemo01 extends JFrame {
  public TestComboboxDemo01() {
      Container container = this.getContentPane();
      JComboBox status = new JComboBox();
      status.addItem(null);
      status.addItem("阿娆就很漂亮");
      status.addItem("阿娆太漂亮了");
      pack();
      container.add(status);
      this.setVisible(true);
      this.setSize(500, 500);
      this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

  public static void main(String[] args) {
      new TestComboboxDemo01();
  }
}

image-20211204183338283

列表框

public class TestComboboxDemo02 extends JFrame {
   public TestComboboxDemo02() {

       Container container = this.getContentPane();

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

       this.setVisible(true);
       this.setSize(500, 500);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

   public static void main(String[] args) {
       new TestComboboxDemo02();
  }
}

文本框

找到一个偷懒神器就这一次

 

 

标签:container,Swing,JButton,add,import,new,public
来源: https://www.cnblogs.com/ar000517/p/15642893.html