栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java-JFrame

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/572389.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号