通过类添加组件到面板?

时间:2015-12-07 12:02:36

标签: java jframe jpanel

我通过类设置了一个面板,并希望能够在实际的类中向面板添加组件。为简化起见,在下面的代码中我只想添加一个标签。以下是我到目前为止,它不喜欢我使用它。

package testframe2;

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class TestFrame2 {

    public static void main(String[] args) {
        new TestFrame2();
    }

    public TestFrame2() {


        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");

                frame.setUndecorated(true);
                frame.setAlwaysOnTop(true);               
                frame.setBackground(new Color(0, 0, 0, 0));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                                               
                frame.add(new TestPane());                                              
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });

    }

    public class TestPane extends JPanel {

        private JLabel label = new JLabel("hello world");
        this.add(label);       

        }

}

3 个答案:

答案 0 :(得分:1)

public class TestPane extends JPanel {

     private JLabel label = new JLabel("hello world");
      this.add(label);       

}

这是无效的语法,将this.add(label);放在方法(或TestPane构造函数)中

答案 1 :(得分:0)

试试这个。我没有尝试构建它,你可能还有一些错误。

package testframe2;

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class TestFrame2 {

public static void main(String[] args) {
    new TestFrame2();
}

public TestFrame2() {


    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            JFrame frame = new JFrame("Testing");

            frame.setUndecorated(true);
            frame.setAlwaysOnTop(true);               
            frame.setBackground(new Color(0, 0, 0, 0));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                                               
            frame.getContentPane().add(new TestPane());                                              
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

        }
    });

}

public class TestPane extends JPanel {

    private JLabel label = new JLabel("hello world");

    public TestPane() {
        super();
        this.add(label);
    }
}
}

答案 2 :(得分:0)

当然你可以这样做。但写出正确的Java!从JPanel扩展TestPane,然后在构造函数中创建组件并将其添加到自定义Panel。

public class TestPane extends JPanel {
    private JLabel label;
        public TestPane(){          
            super();
            label = new JLabel("hello world");
            add(label);
        }    
    }
}