JPanel、JScroll&文本域

JPanel

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

public class JPanelTest extends JFrame {

    public JPanelTest(){
        Container container = this.getContentPane();

        container.setLayout(new GridLayout(2,1,10,10));//后面参数是间距
        JPanel jPanel = new JPanel(new GridLayout(1,3));
        JPanel jPane2 = new JPanel(new GridLayout(1,1));
        JPanel jPane3 = new JPanel(new GridLayout(2,1));
        JPanel jPane4 = new JPanel(new GridLayout(3,1));

        jPanel.add(new JButton("1"));jPanel.add(new JButton("1"));jPanel.add(new JButton("1"));
        jPane2.add(new JButton("2"));
        jPane3.add(new JButton("3"));jPane3.add(new JButton("3"));
        jPane4.add(new JButton("4"));jPane4.add(new JButton("4"));jPane4.add(new JButton("4"));
        container.add(jPanel);container.add(jPane2);
        container.add(jPane3);container.add(jPane4);

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

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

运行结果为:
在这里插入图片描述

JScroll

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

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

        //文本域
        JTextArea jTextArea = new JTextArea(20, 50);
        jTextArea.setText("文本域内容");

        //jcroll面板
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        container.add(jScrollPane);

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

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

运行结果为:
在这里插入图片描述
当窗口大小小于设定时,出现滑动条,当放大窗口使其大于设定时,滑动条消失

好的,以下是可以实现该功能的JAVA程序: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TemperatureConverter extends JFrame implements ActionListener { private JTextField celsiusField, fahrenheitField; private JButton celsiusButton, fahrenheitButton; public TemperatureConverter() { super("温度转换器"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); celsiusField = new JTextField(10); fahrenheitField = new JTextField(10); celsiusButton = new JButton("摄氏转华氏"); fahrenheitButton = new JButton("华氏转摄氏"); JPanel panel1 = new JPanel(); panel1.setLayout(new GridLayout(2, 2)); panel1.add(new JLabel("摄氏温度:")); panel1.add(celsiusField); panel1.add(new JLabel("华氏温度:")); panel1.add(fahrenheitField); JPanel panel2 = new JPanel(); panel2.setLayout(new FlowLayout()); panel2.add(celsiusButton); panel2.add(fahrenheitButton); celsiusButton.addActionListener(this); fahrenheitButton.addActionListener(this); Container cp = getContentPane(); cp.setLayout(new BorderLayout()); cp.add(panel1, BorderLayout.CENTER); cp.add(panel2, BorderLayout.SOUTH); pack(); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == celsiusButton) { double celsius = Double.parseDouble(celsiusField.getText()); double fahrenheit = celsius * 9 / 5 + 32; fahrenheitField.setText(String.format("%.2f", fahrenheit)); } else if (e.getSource() == fahrenheitButton) { double fahrenheit = Double.parseDouble(fahrenheitField.getText()); double celsius = (fahrenheit - 32) * 5 / 9; celsiusField.setText(String.format("%.2f", celsius)); } } public static void main(String[] args) { new TemperatureConverter(); } } ``` 程序实现了一个简单的GUI界面,包含两个文本域和两个按钮。用户可以在“摄氏温度”文本域输入一个摄氏温度值,然后点击“摄氏转华氏”按钮,在“华氏温度”文本域中显示相应的华氏温度值。反之,在“华氏温度”文本域输入一个华氏温度值,然后点击“华氏转摄氏”按钮,在“摄氏温度”文本域中显示相应的摄氏温度值。 在程序中,我们使用了ActionListener接口来实现按钮的点击事件。当用户点击“摄氏转华氏”按钮时,程序将获取“摄氏温度”文本域中的数值,然后根据华氏温度转换公式计算出相应的华氏温度值,并将其显示在“华氏温度”文本域中。当用户点击“华氏转摄氏”按钮时,程序将获取“华氏温度”文本域中的数值,然后根据摄氏温度转换公式计算出相应的摄氏温度值,并将其显示在“摄氏温度”文本域中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值