JAVA27

一、常用组件与容器

JFrame类提供了下面两种格式的构造方法。

(1)JFrame():创建不指定标题的窗体。

(2)JFrame(String title): 创建指定标题的窗体。

package com.hg.day27.demo01;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static javax.swing.WindowConstants.EXIT_ON_CLOSE;

public class DialogDemo01 extends JDialog {

    public DialogDemo01(JFrame jframe) {
        super(jframe,false);
        this.setBounds(100,100,500,500);

        Container container = this.getContentPane();
        container.setLayout(null);
//        container.add(new Label("大数据五班"));
        JButton button = new JButton("我是大数据五班");
        button.setBounds(30,30,200,50);
        container.add(button);
    }

    public static void main(String[] args) {
        JFrame j = new JFrame();
        j.setVisible(true);
        j.setBounds(100,100,700,500);
        j.setDefaultCloseOperation(EXIT_ON_CLOSE);

        Container container = j.getContentPane();
        container.setLayout(null);

        JButton button = new JButton("我要登录");
        button.setBounds(30,30,200,50);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                DialogDemo01 s = new  DialogDemo01(j);
                s.setVisible(true);
                System.out.println("我被点击");

            }
        });
        container.add(button);
    }
}


输出得到:
在这里插入图片描述
二。布局管理

package com.hg.day27.demo03;

import javax.swing.*;

import java.awt.*;

import static javax.swing.WindowConstants.EXIT_ON_CLOSE;


public class TestFlowLayout {
    public static void main(String[] args) {
        JFrame j = new JFrame();
        j.setVisible(true);
        j.setBounds(100,100,700,500);
        j.setDefaultCloseOperation(EXIT_ON_CLOSE);
        j.setLayout(new FlowLayout());
        j.setLayout(new FlowLayout(FlowLayout.RIGHT));

        JButton button1 = new JButton("button1");
        JButton button2 = new JButton("button2");
        JButton button3 = new JButton("button3");
        JButton button4 = new JButton("button4");
        JButton button5 = new JButton("button5");
        JButton button6 = new JButton("button6");
        j.add(button1);
        j.add(button2);
        j.add(button3);
        j.add(button4);
        j.add(button5);
        j.add(button6);

    }
}


在这里插入图片描述

Java 中,发送 HTTP GET 请求通常使用 HttpURLConnection 类或第三方库如 Apache HttpClient、OkHttp 等。这里我将提供一种使用 `java.net` 包内置的 HttpURLConnection 的基本示例: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public classHttpGetExample { public static void main(String[] args) throws Exception { // 创建 URL 对象,指定要请求的地址 String urlString = "https://example.com/api/resource"; URL url = new URL(urlString); // 打开连接,设置为 GET 请求 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); // 设置适当的头信息(如果需要) connection.setRequestProperty("User-Agent", "Mozilla/5.0"); // 连接并获取响应码 int responseCode = connection.getResponseCode(); System.out.println("Response Code : " + responseCode); // 如果状态码表明请求成功(2XX),读取服务器返回的数据 if (responseCode >= 200 && responseCode < 300) { BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer content = new StringBuffer(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } in.close(); // 打印接收到的响应内容 System.out.println(content.toString()); } // 关闭连接 connection.disconnect(); } } ``` 这个例子假设请求是成功的,实际上在生产环境中,你可能还需要处理异常和错误情况。如果你需要使用更现代化的库,例如 OkHttp,则可以导入相应的依赖,并使用它的 API 更简洁地发送请求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值