JTextArea是什么

和单行文本框JTextField不同的是,JTextArea是支持多行的文本框

导入

import javax.swing.JTextArea;

创建

创建一个名为area的多行文本

  • 第一个参数30为默认可见行数
  • 第二个参数20为默认可见列数
JTextArea area = new JTextArea(30, 20);

设置文本

设置文本,\n表示回车

area.setText("abc\n123456");

设置是否允许编辑

不允许编辑是false
允许编辑是true
默认是true

area.setEditable(false);

JComboBox是什么

JComboBox是下拉列表组件,它永远只能选中一个项,然而比单选按钮节省空间

导入

import javax.swing.JComboBox;

创建

这里使用String来创建一个color的数组,用以JComboBox的内容显示

String[] color = { "red", "blue" };
JComboBox cb = new JComboBox<String>(color);

监听

当选择不同的内容时,改变背景颜色

        cb.addItemListener(e -> {

            int index = ((JComboBox) cb).getSelectedIndex();

            if (index == 0)

                area.setForeground(Color.RED);

            else

                area.setForeground(Color.BLUE);

        });

圆的半径计算器

file
file

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class Source2 extends JFrame {

    JTextField t1, t2;

    JTextArea area;

    JButton cal, reset;

    JComboBox cb;

    Source2() {

        setTitle("圆的面积计算");

        setLayout(new BorderLayout(10, 10));

        showNorth();

        showCenter();

        showSouth();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setSize(300, 220);

        setVisible(true);

    }

    void showNorth() {

        JPanel p1 = new JPanel();

        JPanel p2 = new JPanel();

        JPanel panel = new JPanel(new GridLayout(2, 0));

        JLabel l1 = new JLabel("圆的半径");

        JLabel l2 = new JLabel("圆的面积");

        t1 = new JTextField(10);

        t2 = new JTextField(10);

        t2.setEnabled(false);

        p1.add(l1);

        p1.add(t1);

        p2.add(l2);

        p2.add(t2);

        panel.add(p1);

        panel.add(p2);

        add(panel, BorderLayout.NORTH);

        KeyListener listener2 = new KeyListener() {

            public void keyTyped(KeyEvent e) {

                System.out.println(e.getKeyChar() );

            }

            public void keyReleased(KeyEvent e) {

            }

            public void keyPressed(KeyEvent e) {

            }

        };

        t1.addKeyListener(listener2);

    }

    void showCenter() {

        JPanel panel = new JPanel();

        area = new JTextArea(30, 20);

        area.setText("abc\n123456");

        area.setEditable(false);

        area.setForeground(Color.RED);

        panel.add(area);

        add(panel, BorderLayout.CENTER);

    }

    void showSouth() {

        String[] color = { "red", "blue" };

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));

        cal = new JButton("计算");

        cb = new JComboBox<String>(color);

        reset = new JButton("清空");

        panel.add(cal);

        panel.add(cb);

        panel.add(reset);

        add(panel, BorderLayout.SOUTH);

        ActionListener listener1 = e -> {

            if (e.getSource() == cal) {

                if (t1.getText().isEmpty())

                    area.setText("请输入半径!!!");

                else {

                    String s = t1.getText();

                    double radius = Double.parseDouble(s);

                    double result = radius * radius * 3.14;

                    t2.setText("" + result);

                    area.setText(radius + " * " + radius + " * 3.14 = " + result);

                }

            } else {

                t1.setText("");

                t2.setText("");

                area.setText("");

            }

        };

        cal.addActionListener(listener1);

        reset.addActionListener(listener1);

        cb.addItemListener(e -> {

            int index = ((JComboBox) cb).getSelectedIndex();

            if (index == 0)

                area.setForeground(Color.RED);

            else

                area.setForeground(Color.BLUE);

        });

    }

    public static void main(String[] args) {

        new Source2();

    }

}
最后修改:2022 年 03 月 08 日
如果觉得我的文章对你有用,请随意赞赏