您的位置:首页 > 其它

JFrame小练习1

2015-09-20 22:37 127 查看
1.文本域组件

public class TestJTextArea {

public static void main(String[] args) {

JFrame jf=new JFrame("演示文本域");

JPanel jp=new JPanel();

JTextArea jta=new JTextArea("演示文本域,演示文本域,演示文本域",6,6);

jta.setLineWrap(true);

jp.add(jta);

jf.add(jp);

jf.setSize(200,200);

jf.setLocation(200,200);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.setVisible(true);

}

}

2.列表框

public class TestJTextArea {

public static void main(String[] args) {

JFrame jf=new JFrame("演示文本域");

JPanel jp=new JPanel();

JTextArea jta=new JTextArea("演示文本域,演示文本域,演示文本域",6,6);

jta.setLineWrap(true);

jp.add(jta);

jf.add(jp);

jf.setSize(200,200);

jf.setLocation(200,200);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.setVisible(true);

}

}

3.下拉列表框

import java.awt.FlowLayout;

import javax.swing.AbstractListModel;

import javax.swing.ComboBoxModel;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class TestJComboBox extends AbstractListModel<String> implements ComboBoxModel<String>{

String selectedItem=null;

String[] listData={"现金支付","银行卡支付","支付宝支付"};

public String getElementAt(int index){

return listData[index];

}

public int getSize(){

return listData.length;

}

public Object getSelectedItem(){

return selectedItem;

}

public void setSelectedItem(Object arg0){

selectedItem=(String)arg0;

}

public int getIndex(){

for(int i=0;i<listData.length;i++){

if(listData[i].equals(getSelectedItem()))

return i;

}

return 0;

}

public static void main(String[] args) {

JFrame jf=new JFrame("演示下拉列表框");

jf.setSize(300,200);

jf.setLocation(200,200);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.setVisible(true);

jf.setLayout(new FlowLayout());

JLabel j1=new JLabel("请选择支付方式:");

jf.add(j1);

JComboBox<String>jcb=new JComboBox<>(new TestJComboBox());

jf.add(jcb);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: