こんにちは、私は基本的に 2 つのクラスを持っています。
私は持っている :
public class Main{
public static void main (String args[]) {
JFrame mainJFrame;
mainJFrame = new JFrame();
//some other code here
CenterPanel centerPanel = new CenterPanel();
centerPanel.renderPanel();
mainFrame.add(centerPanel.getGUI());
}
}
class CenterPanel{
JPanel center = new JPanel();
public void renderPanel(){
JButton enterButton = new JButton("enter");
JButton exitButton = new JButton("exit");
center.add(exitButton);
center.add(enterButton);
}
public JComponent getGUI(){
return center;
}
}
上記のコードは完全に機能します。ボタンの開始と終了を含む centerPanel をレンダリングします。私の質問は:
色の変更、アクションリスナーの追加など、メインのボタンを操作する必要があります。しかし、技術的には別のクラスに属しているため、メインではもうアクセスできません。したがって、メインでは、centerPanel は別のオブジェクトです。
ボタンにアクセスして使用するにはどうすればよいですか (セット、アクションリスナーなど)? 彼らが別のクラスから来たとしても、私はまだメイン内でそれを使いたいですか?どうもありがとう!