Java 中的Swing 中的问题
importjava.awt.*;importjavax.swing.*;publicclassJfram_ExampleextendsJFrame{JPanelpane...
import java.awt.*;
import javax.swing.*;
public class Jfram_Example extends JFrame {
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
public Jfram_Example()
{
setSize(300,200);
setVisible(true);
setTitle("自定义面板");
setLayout(new BorderLayout());
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container container=getContentPane();
JCheckBox jc1=new JCheckBox();
JCheckBox jc2=new JCheckBox();
JButton jb1=new JButton("确定");
JButton jb2=new JButton("取消");
panel2.add(jc1);
panel2.add(jc2);
panel2.add(jb1);
panel2.add(jb2);
panel1.add(new JComboBoxModelTest());
container.add(panel1,BorderLayout.NORTH);
container.add(panel2,BorderLayout.SOUTH);
}
public static void main(String[] args) {
new Jfram_Example();
}
}
class JComboBoxModelTest extends JFrame{
JComboBox jc=new JComboBox(new MyComboBox1());
JLabel jl=new JLabel("请选择证件:");
public JComboBoxModelTest(){
setSize(new Dimension(160,80));
setVisible(true);
setTitle("在窗口中设置下拉列表框");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(jl);
cp.add(jc);
}
public static void main(String[] args) {
new JComboBoxModelTest();
}
}
class MyComboBox1 extends AbstractListModel implements ComboBoxModel{
String selecteditem=null;
String[] test={"身份证","军人证","学生证","工作证"};
public Object getElementAt(int index){
return test[index];
}
public int getSize(){
return test.length;
}
public void setSelectedItem(Object item){
selecteditem=(String)item;
}
public Object getSelectedItem(){
return selecteditem;
}
public int getIndex(){
for(int i=0;i<test.length;i++){
if(test[i].equals(getSelectedItem()))
return i;
break;
}
return 0;
}
}
不能显示出 按钮 和 复选框 展开
import javax.swing.*;
public class Jfram_Example extends JFrame {
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
public Jfram_Example()
{
setSize(300,200);
setVisible(true);
setTitle("自定义面板");
setLayout(new BorderLayout());
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container container=getContentPane();
JCheckBox jc1=new JCheckBox();
JCheckBox jc2=new JCheckBox();
JButton jb1=new JButton("确定");
JButton jb2=new JButton("取消");
panel2.add(jc1);
panel2.add(jc2);
panel2.add(jb1);
panel2.add(jb2);
panel1.add(new JComboBoxModelTest());
container.add(panel1,BorderLayout.NORTH);
container.add(panel2,BorderLayout.SOUTH);
}
public static void main(String[] args) {
new Jfram_Example();
}
}
class JComboBoxModelTest extends JFrame{
JComboBox jc=new JComboBox(new MyComboBox1());
JLabel jl=new JLabel("请选择证件:");
public JComboBoxModelTest(){
setSize(new Dimension(160,80));
setVisible(true);
setTitle("在窗口中设置下拉列表框");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(jl);
cp.add(jc);
}
public static void main(String[] args) {
new JComboBoxModelTest();
}
}
class MyComboBox1 extends AbstractListModel implements ComboBoxModel{
String selecteditem=null;
String[] test={"身份证","军人证","学生证","工作证"};
public Object getElementAt(int index){
return test[index];
}
public int getSize(){
return test.length;
}
public void setSelectedItem(Object item){
selecteditem=(String)item;
}
public Object getSelectedItem(){
return selecteditem;
}
public int getIndex(){
for(int i=0;i<test.length;i++){
if(test[i].equals(getSelectedItem()))
return i;
break;
}
return 0;
}
}
不能显示出 按钮 和 复选框 展开
2个回答
展开全部
setSize(300,200);
setVisible(true);
把这俩句代码放到后面加载所有组件后在写这俩个代码
也就是放到
panel1.add(new JComboBoxModelTest());
container.add(panel1,BorderLayout.NORTH);
container.add(panel2,BorderLayout.SOUTH);
后面;你不能一早就将它设置为显示那么后面的东西根本就看不到的,要把所有的组件放好(add)然后在设置setVisible属性
setVisible(true);
把这俩句代码放到后面加载所有组件后在写这俩个代码
也就是放到
panel1.add(new JComboBoxModelTest());
container.add(panel1,BorderLayout.NORTH);
container.add(panel2,BorderLayout.SOUTH);
后面;你不能一早就将它设置为显示那么后面的东西根本就看不到的,要把所有的组件放好(add)然后在设置setVisible属性
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询