请问下大佬们为什么我这个代码运行之后除了个框什么都没有?

来源:1-9 经验总结

年纪轻轻想有猫

2018-08-18 19:23

package com.Swing_Domo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//用面板
public class practise extends JFrame{
 //创建下拉列表
 JComboBox<String> jc=new JComboBox<>(new MyComboBox());
 JButton b1=new JButton("确定");
 JButton b2=new JButton("取消");
 //设置复选框组件
 JCheckBox jc1=new JCheckBox("男",true);
 JCheckBox jc2=new JCheckBox("女",true);
 JCheckBox jc3=new JCheckBox("不男不女???",true);
 public practise() {
  Container c=getContentPane();
  setTitle("皮一下窗体");
  setSize(300,300);
  setVisible(true);
  setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  c.setBounds(0,0,100,100);
  b1.setBounds(280,110,30,10);
  b2.setBounds(280, 190, 30, 10);
  jc1.setBounds(120,150,10,10);
  jc2.setBounds(150,150,10,250);
  jc3.setBounds(180,150,10,20);
  c.add(b1);
  c.add(b2);
  c.add(jc1);
  c.add(jc2);
  c.add(jc3);
  //设置事件监听器
  b1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   }
  });
  //设置监听器
  b2.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   }
  });
  jc1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    
   }
  });
  jc2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
       
      }
  });
  jc3.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
       
      }
  });
  
  
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new practise();
  
 }
 
}

 class MyComboBox extends AbstractListModel <String>implements
  ComboBoxModel<String>{
  String selectedItem=null;
  String[] test= {"田螺菇凉","巴扎黑","小明"};
  public String 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;
  }
  
  
 }

写回答 关注

2回答

  • 神圣的莫小奇
    2018-08-22 17:59:23

    //这一行代码有问题,你创建一个新的窗体容器,各个组件都添加到了新建的里面,并没有添加到窗体里,代码有问题 改一下试试。

    import javax.swing.*;

    import java.awt.*;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    //用面板

    public class practise extends JFrame{

     //创建下拉列表

     JComboBox<String> jc=new JComboBox<>(new MyComboBox());

     JButton b1=new JButton("确定");

     JButton b2=new JButton("取消");

     //设置复选框组件

     JCheckBox jc1=new JCheckBox("男",true);

     JCheckBox jc2=new JCheckBox("女",true);

     JCheckBox jc3=new JCheckBox("不男不女???",true);

     public practise() {

      JFrame frame=new JFrame("皮一下");

      frame.setLayout(null);

      Container c=frame.getContentPane();

      c.setBounds(0,0,100,100);

      b1.setBounds(new Rectangle(40,110,80,30));

      b2.setBounds(new Rectangle(130, 110, 80, 30));

      jc1.setBounds(new Rectangle(10,10,40,20));

      jc2.setBounds(new Rectangle(10,40,40,20));

      jc3.setBounds(new Rectangle(10,70,150,20));

      frame.add(b1);

      c.add(b2);

      c.add(jc1);

      c.add(jc2);

      c.add(jc3);

      frame.setSize(300,300);

      frame.setVisible(true);

      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

      //设置事件监听器

      b1.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e) {

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

       }

      });

      //设置监听器

      b2.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e) {

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

       }

      });

      jc1.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e) {

        

       }

      });

      jc2.addActionListener(new ActionListener() {

          public void actionPerformed(ActionEvent e) {

           

          }

      });

      jc3.addActionListener(new ActionListener() {

          public void actionPerformed(ActionEvent e) {

           

          }

      });

      

      

     }

     public static void main(String[] args) {

      // TODO Auto-generated method stub

      new practise();

      

     }

     

    }

     class MyComboBox extends AbstractListModel <String>implements 

      ComboBoxModel<String>{

      String selectedItem=null;

      String[] test= {"田螺菇凉","巴扎黑","小明"};

      public String 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;

      }

      

      

     }



  • aowan
    2018-08-18 20:33:36

    setVisible(true);放最后一行

    还是不行的话,百度setVisible(true);的相关异常

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409779 学习 · 4339 问题

查看课程

相似问题