一个Frame中点击一按纽,事件中构造一新类,该类会在Frame.BorderLayout.south加载一些Label,怎么实现?
我就是加不到原窗口中。

解决方案 »

  1.   

    这个问题主要是参数的传递,只要把原窗口中需要添加JLabel的容器传递到新的类中,新的类就可以将JLabel添加到原窗口中了。
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;/**
     * 例子
     */
    public class Test {
    private JFrame frame = null; private JButton button; private JPanel pane = null; private int i = 0; public Test() {
    frame = new JFrame("Test");
    pane = new JPanel();
    button = new JButton("添加 JLabel");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    new AddLabel(pane, i++);
    }
    });
    frame.getContentPane().add(button, BorderLayout.CENTER);
    frame.getContentPane().add(pane, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setVisible(true); } public static void main(String args[]) {
    new Test();
    }}
    class AddLabel { // pane是原窗口中容纳JLabel的容器,i为标签内容
    public AddLabel(JPanel pane, int i){
    pane.add(new JLabel(" [" + i + "] "));
    pane.updateUI();
    }}
      

  2.   

    5斗米~
      问下~
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        代表什么意思~~可以不需要~~~一般都有这句~我不是很理解~
     可以说下吗?
      

  3.   

    是点击关闭时关掉程序的意思
    当你需要实现点关闭时要确认时你可以设成这样
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)