我也是学JAVA的,刚才写代码时碰到一个问题,能否给解答一下?下面的这段代码编译正常,但是JLabel8就是显示不出来?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;public class Register extends JFrame{
JPanel panel1=new JPanel();
JLabel jLabel1=new JLabel();
JTextField nickname=new JTextField();
JLabel jLabel2=new JLabel();
JLabel jLabel3=new JLabel();
JPasswordField password=new JPasswordField();
JLabel jLabel4=new JLabel();
JTextField email=new JTextField();
JLabel jLabel5=new JLabel();
JLabel jLabel6=new JLabel();
JTextPane info=new JTextPane();
JButton jButton1=new JButton();
JButton jButton2=new JButton();
JLabel jLabel7=new JLabel();
JRadioButton boy=new JRadioButton();
JRadioButton girl=new JRadioButton();
ButtonGroup buttonGroup=new ButtonGroup();//设置按钮组,使其只能选其一
JLabel jLabel8=new JLabel();
JComboBox place=new JComboBox();
ImageIcon[] icons=new ImageIcon[6]; public Register(){
super();
panel1.setLayout(null);
this.setTitle("张朋,这是我用JAVA做的一个窗口");
jLabel1.setText("昵称");
jLabel1.setBounds(new Rectangle(9,45,41,18));
nickname.setBounds(new Rectangle(50,44,128,22));
jLabel2.setText("请填写以下内容");
jLabel2.setBounds(new Rectangle(9,9,103,18));
jLabel3.setText("密码");
jLabel3.setBounds(new Rectangle(200,44,41,18));
password.setBounds(new Rectangle(247,42,100,22));
jLabel4.setText("电子邮件");
jLabel4.setBounds(new Rectangle(2,102,58,18));
email.setBounds(new Rectangle(55,96,124,22));
jLabel5.setText("头像");
jLabel5.setBounds(new Rectangle(193,96,51,18));
for(int i=0;i<6;i++){
icons[i]=new ImageIcon((i+1)+".gif");
}
ComboBoxModel mode=new AModel();
JComboBox headpic=new JComboBox(mode);
headpic.setBounds(new Rectangle(247,96,70,22));
headpic.setRenderer(new ACellRenderer());
headpic.setMaximumRowCount(3);
/*ComboBoxModel model=new HeadPicCombobox(pics);
ListCellRenderer renderer=new HeadpicCellRenderer();*/
jLabel6.setText("个人资料");
jLabel6.setBounds(new Rectangle(6,189,87,18));
info.setBounds(new Rectangle(5,208,363,103));
jButton1.setText("确定");
jButton1.setBounds(new Rectangle(147,330,79,29));
jButton2.setText("取消");
jButton2.setBounds(new Rectangle(260,329,79,29));
jLabel7.setText("性别");
jLabel7.setBounds(new Rectangle(9,156,41,18));
boy.setText("男");
boy.setBounds(new Rectangle(43,152,38,26));
buttonGroup.add(boy);
girl.setText("女");
girl.setBounds(new Rectangle(80,152,38,26));
buttonGroup.add(girl);
jLabel8.setText("来自");
jLabel8.setBounds(new Rectangle(20,189,50,30));
place.setBounds(new Rectangle(247,156,70,22));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
panel1.add(jLabel1);
panel1.add(jLabel2);
panel1.add(jLabel3);
panel1.add(jLabel4);
panel1.add(jLabel5);
panel1.add(jLabel6);
panel1.add(jLabel7);
panel1.add(jButton1);
panel1.add(jButton2);
panel1.add(nickname);
panel1.add(password);
panel1.add(email);
panel1.add(info);
panel1.add(boy);
panel1.add(girl);
panel1.add(place);
panel1.add(headpic);
setLocation(100,100);
setSize(300,500);
setVisible(true);
this.setContentPane(panel1);
}
public static void main(String[] args){
Register r=new Register();
r.show();
}
class AModel extends DefaultComboBoxModel{
AModel(){
for(int i=0;i<icons.length;i++){
ItemObj obj=new ItemObj(icons[i]);
addElement(obj);
}
}
}
}class ItemObj{
ImageIcon icon;
public ItemObj(ImageIcon icon){
this.icon=icon;
}
}class ACellRenderer extends JLabel implements ListCellRenderer{
ACellRenderer(){
setOpaque(true);
}
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus){
if(value!=null){
setIcon(((ItemObj)value).icon);
}
if(isSelected){
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}else{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
return this;
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;public class Register extends JFrame{
JPanel panel1=new JPanel();
JLabel jLabel1=new JLabel();
JTextField nickname=new JTextField();
JLabel jLabel2=new JLabel();
JLabel jLabel3=new JLabel();
JPasswordField password=new JPasswordField();
JLabel jLabel4=new JLabel();
JTextField email=new JTextField();
JLabel jLabel5=new JLabel();
JLabel jLabel6=new JLabel();
JTextPane info=new JTextPane();
JButton jButton1=new JButton();
JButton jButton2=new JButton();
JLabel jLabel7=new JLabel();
JRadioButton boy=new JRadioButton();
JRadioButton girl=new JRadioButton();
ButtonGroup buttonGroup=new ButtonGroup();//设置按钮组,使其只能选其一
JLabel jLabel8=new JLabel();
JComboBox place=new JComboBox();
ImageIcon[] icons=new ImageIcon[6]; public Register(){
super();
panel1.setLayout(null);
this.setTitle("张朋,这是我用JAVA做的一个窗口");
jLabel1.setText("昵称");
jLabel1.setBounds(new Rectangle(9,45,41,18));
nickname.setBounds(new Rectangle(50,44,128,22));
jLabel2.setText("请填写以下内容");
jLabel2.setBounds(new Rectangle(9,9,103,18));
jLabel3.setText("密码");
jLabel3.setBounds(new Rectangle(200,44,41,18));
password.setBounds(new Rectangle(247,42,100,22));
jLabel4.setText("电子邮件");
jLabel4.setBounds(new Rectangle(2,102,58,18));
email.setBounds(new Rectangle(55,96,124,22));
jLabel5.setText("头像");
jLabel5.setBounds(new Rectangle(193,96,51,18));
for(int i=0;i<6;i++){
icons[i]=new ImageIcon((i+1)+".gif");
}
ComboBoxModel mode=new AModel();
JComboBox headpic=new JComboBox(mode);
headpic.setBounds(new Rectangle(247,96,70,22));
headpic.setRenderer(new ACellRenderer());
headpic.setMaximumRowCount(3);
/*ComboBoxModel model=new HeadPicCombobox(pics);
ListCellRenderer renderer=new HeadpicCellRenderer();*/
jLabel6.setText("个人资料");
jLabel6.setBounds(new Rectangle(6,189,87,18));
info.setBounds(new Rectangle(5,208,363,103));
jButton1.setText("确定");
jButton1.setBounds(new Rectangle(147,330,79,29));
jButton2.setText("取消");
jButton2.setBounds(new Rectangle(260,329,79,29));
jLabel7.setText("性别");
jLabel7.setBounds(new Rectangle(9,156,41,18));
boy.setText("男");
boy.setBounds(new Rectangle(43,152,38,26));
buttonGroup.add(boy);
girl.setText("女");
girl.setBounds(new Rectangle(80,152,38,26));
buttonGroup.add(girl);
jLabel8.setText("来自");
jLabel8.setBounds(new Rectangle(20,189,50,30));
place.setBounds(new Rectangle(247,156,70,22));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
panel1.add(jLabel1);
panel1.add(jLabel2);
panel1.add(jLabel3);
panel1.add(jLabel4);
panel1.add(jLabel5);
panel1.add(jLabel6);
panel1.add(jLabel7);
panel1.add(jButton1);
panel1.add(jButton2);
panel1.add(nickname);
panel1.add(password);
panel1.add(email);
panel1.add(info);
panel1.add(boy);
panel1.add(girl);
panel1.add(place);
panel1.add(headpic);
setLocation(100,100);
setSize(300,500);
setVisible(true);
this.setContentPane(panel1);
}
public static void main(String[] args){
Register r=new Register();
r.show();
}
class AModel extends DefaultComboBoxModel{
AModel(){
for(int i=0;i<icons.length;i++){
ItemObj obj=new ItemObj(icons[i]);
addElement(obj);
}
}
}
}class ItemObj{
ImageIcon icon;
public ItemObj(ImageIcon icon){
this.icon=icon;
}
}class ACellRenderer extends JLabel implements ListCellRenderer{
ACellRenderer(){
setOpaque(true);
}
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus){
if(value!=null){
setIcon(((ItemObj)value).icon);
}
if(isSelected){
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}else{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
return this;
}
}
panel1.add(jLabel2);
panel1.add(jLabel3);
panel1.add(jLabel4);
panel1.add(jLabel5);
panel1.add(jLabel6);
panel1.add(jLabel7);
这7个都添加了为什么不加这个:
panel1.add(jLabel8);