私はしばらくの間 StackOverflow を使用してきましたが、これは私の最初の質問です。
別のクラス (ClassFrom) に JPanel オブジェクト (ここでは「パネル」と呼びます) を作成し、それを別のクラス (ClassTo) の別の JFrame オブジェクト (ここでは「フレーム」と呼びます) に表示したいと思いますが、 JLabel がクリックされたときに JPanel の「パネル」が JFrame の「フレーム」に表示されないため、これまでのところ何か問題があるようです。
誰でも私のコードを見て、可能な限り助けてください。本当に感謝しています。
これが私のコードです。
import javax.swing.JFrame;
// The Class with the JFame that gets the components from ClassFrom
public class ClassTo {
JFrame frame = new JFrame("The JFrame");
ClassFrom classFrom = new ClassFrom();
public static void main (String[] args)
{
// This is where there seems to be a problem
frame.add(classFrom.contentMethod());
}
}
import javax.swing.JLabel;
import javax.swing.JPanel;
// The Class with the components to be added to the JFrame in ClassTo
public class ClassFrom {
public static void contentMethod() {
JPanel panel = new JPanel();
JLabel label = new JLabel("Try Label");
panel.add(label);
}
}