Java-JFrame

Label和标签居中

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

public class JFrameDemo02 {
    public static void main(String[] args) {
        new MyJFrame2().init();
    }
}
class MyJFrame2 extends JFrame{
    public void init(){
          //获得一个容器
        Container contentPane = this.getContentPane();
        contentPane.setBackground(Color.yellow);

        //设置文字 JLabel
        JLabel jLabel = new JLabel("JLabel");
        this.add(jLabel,BorderLayout.CENTER);

        //设置文本居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);


        this.setVisible(true);
        this.setBounds(100,100,200,200);
    }

}

弹窗

JDialog,弹窗默认有关闭事件

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

//主窗口
public class DialogDemo extends JFrame {
    public DialogDemo() {
        this.setVisible(true);
        this.setBounds(100,200,300,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //JFrame 容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);
        //按钮
        JButton jButton = new JButton("点击弹出一个弹窗");
        jButton.setBounds(50,60,200,35);

        //点击按钮生成弹窗,需要监听器
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialog();
            }
        });

        container.add(jButton);
    }

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

//弹窗
class MyDialog extends JDialog{
    public MyDialog() {
        this.setVisible(true);
        this.setBounds(200,200,200,200);
        Container container = this.getContentPane();
        JLabel jLabel = new JLabel("这是一个弹窗");

        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        this.add(jLabel);
    }
}

图标

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 jLabel = new JLabel("iconTest", iconDemo, SwingConstants.CENTER);

        Container container = iconDemo.getContentPane();
        container.add(jLabel);

        iconDemo.setVisible(true);
        iconDemo.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

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

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

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

图片

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

public class ImageIconDemo extends JFrame {
    public ImageIconDemo() {
        //获取图片地址
        JLabel jLabel = new JLabel("ImageIcon");
        URL url = ImageIconDemo.class.getResource("log.png");

        ImageIcon imageIcon = new ImageIcon(url);

        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

        Container container = this.getContentPane();
        container.add(jLabel);

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

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

JPanel

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

public class JPanelDemo extends JFrame {

    public JPanelDemo() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2,1));
        JPanel jPanel = new JPanel(new GridLayout(1,2));

        jPanel.add(new Button("1"));
        jPanel.add(new Button("1"));
        jPanel.add(new Button("1"));
        container.add(jPanel);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(200,200,400,300);
    }

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

JScrollPane

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

public class JScrollDemo extends JFrame {
    public JScrollDemo() {
        Container container = this.getContentPane();

        //文本域
        TextArea textArea = new TextArea(20,50);
        textArea.setText("javaGUI");

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

        this.setVisible(true);
        this.setBounds(200,200,500,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

按钮

图片按钮

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

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01() throws HeadlessException {
        Container container = this.getContentPane();
        //获取图片URL
        URL url = JButtonDemo01.class.getResource("log.png");
        ImageIcon imageIcon = new ImageIcon(url);

        //添加图片
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);
        jButton.setToolTipText("这是一个图片按钮");

        //add
        container.add(jButton);

        //老三样
        this.setVisible(true);
        this.setBounds(300,300,500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

单选按钮

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

public class JButtonDemo02  extends JFrame {
    public JButtonDemo02() throws HeadlessException {
        Container container = this.getContentPane();
        //获取图片URL
        URL url = JButtonDemo01.class.getResource("log.png");
        ImageIcon imageIcon = new ImageIcon(url);

        //创建单选框对象
        JRadioButton radioButton1 = new JRadioButton("RadioButton1");
        JRadioButton radioButton2 = new JRadioButton("RadioButton2");
        JRadioButton radioButton3 = new JRadioButton("RadioButton3");

        //成组
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
        buttonGroup.add(radioButton3);

        //添加到Container
        container.add(radioButton1,BorderLayout.SOUTH);
        container.add(radioButton2,BorderLayout.CENTER);
        container.add(radioButton3,BorderLayout.NORTH);

        //老三样
        this.setVisible(true);
        this.setBounds(300,300,500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

多选按钮

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

public class JButtonDemo03  extends JFrame {
    public JButtonDemo03() throws HeadlessException {
        Container container = this.getContentPane();
        //获取图片URL
        URL url = JButtonDemo01.class.getResource("log.png");
        ImageIcon imageIcon = new ImageIcon(url);

        //创建单选框对象
        JCheckBox jCheckBox1 = new JCheckBox("JCheckBox1");
        JCheckBox jCheckBox2 = new JCheckBox("JCheckBox2");
        JCheckBox jCheckBox3 = new JCheckBox("JCheckBox3");

        //添加到Container
        container.add(jCheckBox1,BorderLayout.SOUTH);
        container.add(jCheckBox2,BorderLayout.CENTER);
        container.add(jCheckBox3,BorderLayout.NORTH);

        //老三样
        this.setVisible(true);
        this.setBounds(300,300,500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

列表

下拉列表

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

public class TestComboBoxDemo01 extends JFrame {
    public TestComboBoxDemo01(){
        Container container = this.getContentPane();

        //下拉列表
        JComboBox jComboBox = new JComboBox();

        jComboBox.addItem(null);
        jComboBox.addItem("男");
        jComboBox.addItem("女");
        jComboBox.addItem("保密");

        //add
        container.add(jComboBox);

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

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

列表框

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

public class TestComboBoxDemo02 extends JFrame {
    public TestComboBoxDemo02() throws InterruptedException {
        Container container = this.getContentPane();

//        String[] contents = {"1","2","3","4","5","6",};
        Vector contents = new Vector();
        contents.add("1");
        contents.add("2");
        contents.add("3");
        contents.add("4");
        //列表框
        JList jList = new JList(contents);

        container.add(jList);

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

    public static void main(String[] args) throws InterruptedException {
        new TestComboBoxDemo02();
    }
}
  • 应用场景
    • 下拉框选择一些单个选项(两个推荐单选框,多个推荐下拉框)。
    • 列表可以用于展示一些动态信息。

文本框

文本框

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

public class TestTextDemo01 extends JFrame {
    public TestTextDemo01() {
        Container container = this.getContentPane();

        JTextField jTextField1 = new JTextField("Hello");
        JTextField jTextField2 = new JTextField("world");

        container.add(jTextField1,BorderLayout.NORTH);
        container.add(jTextField2,BorderLayout.SOUTH);

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

    public static void main(String[] args) throws InterruptedException {
        new TestTextDemo01();
    }
}

密码框

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

public class TestTextDemo03 extends JFrame {
    public TestTextDemo03(){
        Container container = this.getContentPane();

        JPasswordField jPasswordField1 = new JPasswordField("123");
        JPasswordField jPasswordField2 = new JPasswordField("456");
        jPasswordField1.setEchoChar('*');

        container.add(jPasswordField1,BorderLayout.NORTH);
        container.add(jPasswordField2,BorderLayout.SOUTH);

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

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

文本域

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

public class TestTextDemo02 extends JFrame {
    public TestTextDemo02(){
        Container container = this.getContentPane();

        JTextArea jTextArea1 = new JTextArea("123");
        JTextArea jTextArea2 = new JTextArea("123");


        container.add(jTextArea1,BorderLayout.NORTH);
        container.add(jTextArea2,BorderLayout.SOUTH);

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

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

猜你喜欢

转载自blog.csdn.net/Mr_yao0/article/details/121438237