JavaSE14—窗体程序开发(下)
JavaSE14—窗体程序开发(下)
1.基础组件(续)
1)文本控件
1.单行文本框控件
Swing中的JTextField控件实现了单行文本框。JTextField类提供的常用构造方法。
JTextField jtext = new JTextField(列数);
2.密码控件
JPasswordField组件实现了一个密码框,用来接收用户输入的单行文本信息。
JPasswordField类提供了setEchoChar()方法,用来改变密码框中的显示字符。
3.文本域控件
JTextArea组件实现一个文本域,文本域可以接收用户输入的多行文本。创建之后,可以通过setLineWrap(boolean bool)方法设置文本是否自动换行,默认为false。
实例:创建窗体,在窗体中滚动面板,在面板中添加文本域控件,实现从文件中读取数据,显示在文本域中。
package qijia.si;import java.io.BufferedReader;import java.io.FileReader;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;public class FieldDemo extends JFrame{private JTextArea textArea;public FieldDemo(){super();getContentPane().setLayout(null);setBounds(100,100,378,311);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final JScrollPane scrollPane = new JScrollPane();scrollPane.setBounds(0,0,370,277);getContentPane().add(scrollPane);textArea = new JTextArea();scrollPane.setViewportView(textArea);String aline;try{BufferedReader br = new BufferedReader(new FileReader("src/qijia/si/FieldDemo.java"));while((aline = br.readLine())!=null){textArea.append(aline+'\n');}br.close();}catch(Exception e){e.printStackTrace();}}public static void main(String[] args){FieldDemo fd = new FieldDemo();fd.setVisible(true);}}2)列表控件package qijia.si;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JPanel;import javax.swing.JScrollPane;public class JListDemo extends JFrame{private JList list;public JListDemo(){super();getContentPane().setLayout(null);setBounds(100,100,266,182);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final JPanel panel = new JPanel();panel.setLayout(null);panel.setBounds(0,0,262,151);getContentPane().add(panel);final JLabel label = new JLabel();label.setText("爱好: ");label.setBounds(44,30,39,18);panel.add(label);final JScrollPane scrollPane = new JScrollPane();scrollPane.setBounds(92,28,100,105);panel.add(scrollPane);String[] like = {"读书","上网","旅游","游戏","听音乐"};list = new JList(like);scrollPane.setViewportView(list);}public static void main(String[] args){JListDemo jd = new JListDemo();jd.setVisible(true);}}package qijia.si;import java.net.URL;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class TestDemo extends JFrame{public TestDemo(){super();getContentPane().setLayout(null);setBounds(100,100,182,144);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final JPanel panel = new JPanel();panel.setLayout(null);panel.setBounds(0,0,176,164);getContentPane().add(panel);URL url = getClass().getResource("pig.png");ImageIcon imageIcon = new ImageIcon(url);final JButton button = new JButton();button.setIcon(imageIcon);button.setBounds(61,10,46,46);button.setContentAreaFilled(false);button.setBorder(null);panel.add(button);final JLabel message = new JLabel();message.setText("这是个不规则按钮");message.setBounds(37,77,91,81);panel.add(message);}public static void main(String[] args){TestDemo td = new TestDemo();td.setVisible(true);}}