請問下大佬們?yōu)槭裁次疫@個代碼運行之后除了個框什么都沒有?
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{
?//創(chuàng)建下拉列表
?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);
??//設置事件監(jiān)聽器
??b1.addActionListener(new ActionListener() {
???public void actionPerformed(ActionEvent e) {
????setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
???}
??});
??//設置監(jiān)聽器
??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;
??}
??
??
?}
2018-08-22
//這一行代碼有問題,你創(chuàng)建一個新的窗體容器,各個組件都添加到了新建的里面,并沒有添加到窗體里,代碼有問題 改一下試試。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//用面板
public class practise extends JFrame{
?//創(chuàng)建下拉列表
?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);
? //設置事件監(jiān)聽器
? b1.addActionListener(new ActionListener() {
? ?public void actionPerformed(ActionEvent e) {
? ? setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
? ?}
? });
? //設置監(jiān)聽器
? 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;
? }
??
??
?}
2018-08-18
setVisible(true);放最后一行
還是不行的話,百度setVisible(true);的相關異常