我在jpanel里放了4个按钮,按钮上按顺序分别显示1,2,3,4
gridlayout是2,2.
完成后,我想删除第4个按钮,就是使用jpanel的remove方法。但效果是第4个按钮界面上仍然显示,实际上已经删除。
而我删除其他的按钮是没有问题的。
不知道为什么删除最后一个按钮,界面上没有显示出来?

解决方案 »

  1.   


    package p1;import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.LayoutManager;
    import java.awt.Point;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.nio.charset.UnmappableCharacterException;
    import java.util.ArrayList;
    import java.util.HashMap;import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLayeredPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JWindow;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.PlainDocument;public class mainfile {
    public static void main(String[] args) {
    new myui();
    }
    }class myui {
    private floatwindow floattable = null;
    private mainframe mainframe = null; myui() {
    floattable = new floatwindow();
    mainframe = new mainframe(floattable); mainframe.setBounds(100, 100, 500, 600);
    mainframe.setVisible(true);
    mainframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); floattable.setVisible(true);
    floattable.setAlwaysOnTop(true);
    }}class floatwindow extends JWindow {
    private JPanel controlpanel = null;
    private JPanel btnspanel = null;
    private JButton floatclosebtn = null;
    private floatwindow floatwindow = null; private Point formerPoint = new Point();
    private int screenwidth = 0;
    private int screenheight = 0; HashMap<Integer, floatbtn> usemap = new HashMap<Integer, floatbtn>();
    HashMap<Integer, floatbtn> unusemap = new HashMap<Integer, floatbtn>(); floatwindow() {
    java.awt.Toolkit xToolkit = java.awt.Toolkit.getDefaultToolkit();
    screenwidth = (int) xToolkit.getScreenSize().getWidth();
    screenheight = (int) xToolkit.getScreenSize().getHeight(); floatwindow = this;
    floatclosebtn = new JButton("浮动框退出");
    floatclosebtn.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    floatwindow.setVisible(false);
    }
    });
    this.setBounds(500, 50, 200, 600);
    controlpanel = new JPanel();
    controlpanel.add(floatclosebtn);
    controlpanel.setPreferredSize(new Dimension(100, 80));
    controlpanel.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
    this.setLayout(new BorderLayout(10, 10));
    this.add(BorderLayout.NORTH, controlpanel); btnspanel = new JPanel();
    btnspanel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
    btnspanel.setLayout(new GridLayout());
    this.initbtn();
    this.add(BorderLayout.CENTER, btnspanel); this.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
    formerPoint = e.getPoint();
    }
    }); this.addMouseMotionListener(new MouseMotionListener() { @Override
    public void mouseMoved(MouseEvent e) {
    // TODO Auto-generated method stub } @Override
    public void mouseDragged(MouseEvent e) {
    // TODO Auto-generated method stub
    int offx = e.getX() - (int) formerPoint.getX();
    int offy = e.getY() - (int) formerPoint.getY();
    int currentx = floatwindow.getX() + offx;
    int currenty = floatwindow.getY() + offy;
    if (currentx < 0 || currenty < 0
    || currentx + 200 > screenwidth
    || currenty + 600 > screenheight)
    floatwindow.setLocation(floatwindow.getX(), floatwindow
    .getY());
    else
    floatwindow.setLocation(currentx, currenty); }
    });
    } private void initbtn() {
    GridLayout initlayout = new GridLayout(2, 2);
    btnspanel.setLayout(initlayout);
    floatbtn btn1 = new floatbtn(1);
    usemap.put(1, btn1);
    floatbtn btn2 = new floatbtn(2);
    usemap.put(2, btn2);
    floatbtn btn3 = new floatbtn(3);
    usemap.put(3, btn3);
    floatbtn btn4 = new floatbtn(4);
    usemap.put(4, btn4);
    btnspanel.add(btn1);
    btnspanel.add(btn2);
    btnspanel.add(btn3);
    btnspanel.add(btn4); } public void addbtn(int type) {
    if (type <= 0)
    return; floatbtn temp = usemap.get(type);
    if (temp != null)
    return;
    temp = unusemap.get(type);
    if (temp == null)
    temp = new floatbtn(type); usemap.put(type, temp); int btnnum = usemap.size();
    GridLayout layout = new GridLayout((btnnum + 1) / 2, 2);
    btnspanel.setLayout(layout);
    btnspanel.add(temp);
    btnspanel.revalidate();
    } public void delbtn(int type) {
    if (type <= 0)
    return; floatbtn temp1 = usemap.get(type);
    if (temp1 == null)
    return;
    floatbtn temp2 = unusemap.get(type); if (temp2 == null)
    unusemap.put(type, temp1); usemap.remove(type);
    btnspanel.remove(temp1); int btnnum = usemap.size();
    GridLayout layout = new GridLayout((btnnum + 1) / 2, 2);
    btnspanel.setLayout(layout); btnspanel.revalidate();
    }}class floatbtn extends JButton {
    private int type; public int getType() {
    return type;
    } public void setType(int type) {
    this.type = type;
    } floatbtn(int tp) {
    super();
    type = tp;
    this.setText(String.valueOf(type));
    switch (type) {
    case 1: {
    this.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    mainframe.text.setText("1号按钮");
    }
    }); break;
    }
    case 2: {
    this.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    mainframe.text.setText("2号按钮");
    }
    });
    break;
    }
    case 3: {
    this.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    mainframe.text.setText("3号按钮");
    }
    });
    break;
    }
    case 4: {
    this.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    mainframe.text.setText("4号按钮");
    }
    });
    break;
    }
    case 5: {
    this.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    mainframe.text.setText("5号按钮");
    }
    });
    break;
    }
    case 6: {
    this.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    mainframe.text.setText("6号按钮");
    }
    });
    break;
    } case 7: {
    this.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    mainframe.text.setText("7号按钮");
    }
    });
    break;
    }
    case 8: {
    this.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    mainframe.text.setText("8号按钮");
    }
    });
    break;
    }
    case 9: {
    this.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    mainframe.text.setText("9号按钮");
    }
    });
    break;
    }
    case 10: {
    this.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    mainframe.text.setText("10号按钮");
    }
    });
    break;
    }
    default:
    break;
    }
    }}class mainframe extends JFrame {
    private JButton addbtn = new JButton("添加按钮");
    private JButton delbtn = new JButton("删除按钮");
    public static JTextField text = new JTextField();
    private JTextField choicetext = new JTextField();
    private floatwindow floatwindow = null; mainframe(floatwindow floatwin) {
    super();
    floatwindow = floatwin;
    addbtn.setBounds(120, 100, 100, 50);
    delbtn.setBounds(120, 200, 100, 50);
    text.setBounds(120, 300, 200, 50);
    choicetext.setBounds(120, 400, 100, 50);
    choicetext.setDocument(new NumOnlyDocument());
    this.setLayout(null);
    this.add(addbtn);
    this.add(delbtn);
    this.add(text);
    this.add(choicetext); addbtn.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    String temp = choicetext.getText();
    if (temp.compareTo("") == 0)
    return;
    int choice = Integer.parseInt(temp);
    floatwindow.addbtn(choice); }
    });
    delbtn.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    String temp = choicetext.getText();
    if (temp.compareTo("") == 0)
    return;
    int choice = Integer.parseInt(choicetext.getText());
    floatwindow.delbtn(choice);
    }
    });
    }}class NumOnlyDocument extends PlainDocument {
    public void insertString(int offset, String s, AttributeSet attrSet)
    throws BadLocationException {
    try {
    Integer.parseInt(s);
    } catch (NumberFormatException ex) {
    return;
    }
    super.insertString(offset, s, attrSet);
    }
    }
      

  2.   

    把所有的btnspanel.revalidate()换成
    btnspanel.validate();
    btnspanel.repaint();
      

  3.   

    你有这个你程序不报错?
     @Override
    你是点击删除是删除button4?
      

  4.   

    而且你这个是实现接口,不应该有@Override 注释