मैं एक छोटा जीयूआई बनाने की कोशिश कर रहा हूं, इसमें 2 जेबूटन और 2 जेपीनेल हैं जिनमें से प्रत्येक पर कुछ ड्राइंग एनीमेशन है। डिफ़ॉल्ट रूप से इसे पहले जेपीनल दिखाना चाहिए, और दूसरे जेबटन पर क्लिक करके मैं अपना दूसरा जेपीनल देखना चाहता हूं। तो: मैं जेफ्रेम, पैनल 1 और पैनल 2 बना रहा हूं, जहां मैंने अपने एनिमेशन खींचे हैं, बटन 1 और बटन 2 बनाये हैं और उन्हें एक्शनलिस्टनर जोड़ रहे हैं। मेरे पास मेनपैनल भी है जो फ़ील्ड वैरिएबल में है। इसे "i" बदलकर मेरा कन्स्ट्रक्टर मेनपैनल को पैनल 1 (डिफ़ॉल्ट) या पैनल 2 में जोड़ता है (जेबटन 2 पर क्लिक करके मैं बदलता हूं)। मैं इस मेनपैनल को अपने फ्रेम में जोड़ता हूं। तो मेरा सवाल: क्लास मेनपैनल में मैंने रीफ्रेश किया है मेरी विधि, मुझे अपना जीयूआई सही तरीके से काम करने के लिए क्या लिखना चाहिए? धन्यवाद।जेबटन पर क्लिक करके जेपीनल कैसे जोड़ें?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GuiTest {
public static void main(String[] args) {
JFrame f = new JFrame();
MainPanel myPanel = new MainPanel();
f.add(myPanel);
Button1 button1 = new Button1();
Button2 button2 = new Button2();
myPanel.add(button1);
myPanel.add(button2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
class MainPanel extends JPanel {
Panel1 p1 = new Panel1();
Panel2 p2 = new Panel2();
public int i = 1; //this is being changed later by clicking JButton
// I use this setter later in actionPerformed in order to change i
public void setI(int i) {
this.i = i;
}
MainPanel() {
if (i == 1) {
this.add(p1);
}
if (i == 2) {
this.add(p2);
}
}
public void refreshMe() {
// Need some help here:
// I don't know what should I write, how to make a repaint of myPanel?
System.out.println("just test, if the method refreshMe working by clicking some button");
}
}
class Panel1 extends JPanel {
public Panel1() {
this.setBackground(Color.BLUE);
// a lot of drawing stuff going on here
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
class Panel2 extends JPanel {
public Panel2() {
this.setBackground(Color.GREEN);
// a lot of drawing stuff going on here
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
class Button1 extends JButton {
MainPanel someObj1 = new MainPanel();
Button1() {
setText("Show Annimation A");
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
someObj1.setI(1);
System.out.println("The variable i is now: " + someObj1.i);
someObj1.refreshMe();
}
});
}
}
class Button2 extends JButton {
MainPanel someObj2 = new MainPanel();
Button2() {
setText("Show Annimation B");
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
someObj2.setI(2);
System.out.println("The variable i is now: " + someObj2.i);
someObj2.refreshMe();
}
});
}
}
मेरे कोड और उदाहरण को सही करने के लिए धन्यवाद, इससे बहुत मदद मिलती है! हां, कार्डलाउट मेरी समस्या हल करता है। – user1924939