五一期间java编程实例分享
目前。学的是对用户请求的响应,再贴一段代码。

/*在文本框中输入一段文字,然后再在文本域中输出*/
|
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TextEventDemo { JFrame frame; JPanel panel; JPanel panel_1 ; JTextField txtMessage; JTextArea txtgetMessage; JLabel spacelabel; public TextEventDemo(){ frame = new JFrame(); panel = new JPanel(); panel_1 = new JPanel(); txtMessage = new JTextField(); txtgetMessage = new JTextArea(); spacelabel = new JLabel(" "); txtMessage.addActionListener(new MyListener()); panel_1.add(spacelabel); panel_1.add(txtgetMessage); panel_1.setLayout(new BorderLayout()); panel_1.add(spacelabel,BorderLayout.NORTH); panel_1.add(txtgetMessage,BorderLayout.CENTER); panel.setLayout(new BorderLayout()); panel.add(txtMessage,BorderLayout.NORTH); panel.add(panel_1,BorderLayout.CENTER); frame.getContentPane().add(panel); frame.setSize(300,300); frame.setVisible(true); } class MyListener implements ActionListener{ public void actionPerformed(ActionEvent evt){ String str = txtMessage.getText(); txtgetMessage.append(str+"\n"); txtMessage.selectAll(); } } public static void main(String[] args){ new TextEventDemo(); } } |
刚出来效果那会,感觉很好玩!
暂时没遇到什么大问题。
下面SHOW一下我编写的一个小的还未成熟的程序:
|
import java.awt.*; import javax.swing.*; import java.awt.event.*; class SupplierInfo { String[] areas; JFrame frame; JPanel panel1; JPanel panel2; JPanel panel3; JPanel panel4; JPanel panel5; JLabel labelTitle; JLabel labelSuppllierID; JLabel labelSuppllierName; JLabel labelSuppllierCompany; JLabel labelSuppllierAdress; JLabel labelSuppllierEmail; JLabel labelSuppllierPhone; JLabel labelSuppllierRegion; JLabel labelSuppllierSex; JTextField txtSupplierID; JTextField txtSupplierName; JTextField txtSupplierCompany; JTextField txtSupplierEmail; JTextField txtSupplierPhone; JTextField txtSupplierAdress; JComboBox combosupplierRegion; ButtonGroup buttonGroup; JRadioButton radioButtonMan; JRadioButton radioButtonFemale; JButton btnSubmit; JButton btnCancel; //构造方法 public SupplierInfo(){ //实例化、初始化各容器、组件 frame = new JFrame("供应商信息录入"); panel1 =new JPanel(); panel2 =new JPanel(); panel3 =new JPanel(); panel4 =new JPanel(); panel5 =new JPanel(); labelTitle = new JLabel("供应商信息录入"); labelSuppllierID = new JLabel("供应商ID"); labelSuppllierName = new JLabel("供应商代表姓名"); labelSuppllierCompany = new JLabel("供应商公司"); labelSuppllierAdress = new JLabel("供应商地址"); labelSuppllierEmail = new JLabel("E-Mail"); labelSuppllierPhone = new JLabel("联系电话"); labelSuppllierSex = new JLabel("性别"); labelSuppllierRegion = new JLabel("供应商所在区域"); txtSupplierID = new JTextField("如:001",5); txtSupplierName = new JTextField("请输入真实姓名",5); txtSupplierCompany = new JTextField(10); txtSupplierAdress = new JTextField(15); txtSupplierEmail = new JTextField("*******@**.com",15); txtSupplierPhone = new JTextField(10); areas = new String[]{"云南省","杭州市","山东省","海南省","北京市","天津市","吉林省","湖南省","四川省"}; combosupplierRegion = new JComboBox(areas); buttonGroup = new ButtonGroup(); radioButtonMan = new JRadioButton("男"); radioButtonFemale = new JRadioButton("女"); buttonGroup.add(radioButtonMan); buttonGroup.add(radioButtonFemale); btnSubmit = new JButton("提交"); btnCancel = new JButton("撤销"); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(panel1,BorderLayout.NORTH); frame.getContentPane().add(panel2,BorderLayout.WEST); frame.getContentPane().add(panel3,BorderLayout.EAST); frame.getContentPane().add(panel4,BorderLayout.SOUTH); panel2.setLayout(new GridLayout(8,1)); panel3.setLayout(new GridLayout(8,1)); panel5.setLayout(new GridLayout(1,2)); //容器属性设置 frame.setVisible(true); frame.setSize(400, 300); //将各组件添加进容器。 panel1.add(labelTitle); panel5.add(radioButtonMan); panel5.add(radioButtonFemale); panel2.add(labelSuppllierID); panel3.add(txtSupplierID); panel2.add(labelSuppllierName); panel3.add(txtSupplierName); panel2.add(labelSuppllierSex); panel3.add(panel5); panel2.add(labelSuppllierCompany); panel3.add(txtSupplierCompany); panel2.add(labelSuppllierRegion); panel3.add(combosupplierRegion); panel2.add(labelSuppllierAdress); panel3.add(txtSupplierAdress); panel2.add(labelSuppllierEmail); panel3.add(txtSupplierEmail); panel2.add(labelSuppllierPhone); panel3.add(txtSupplierPhone); panel4.add(btnSubmit); panel4.add(btnCancel); } } public class SupplierInformation1 { public static void main(String[] args) { SupplierInfo info = new SupplierInfo(); } } |
等图形界面学完之后,运行效果会更加漂亮。期待........
这个星期学的布局类中,GridBagLayout类的使用还不是很了解。图像的创建,加载和显示不太会。
本文来源 我爱IT技术网 http://www.52ij.com/jishu/1294.html 转载请保留链接。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
