通过 class?

Add a component to a panel through a class?

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

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);       

        }

}

试试这个。我没有尝试构建它,您可能还有更多错误。

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);
    }
}
}
public class TestPane extends JPanel {

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

}

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

当然可以。但是写对Java!从 JPanel 扩展您的 TestPane,然后在构造器中创建您的组件并将其添加到您的自定义面板。

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