NullPointerException continues - java

When I am trying to call JPanel panel2 of Panel2 class on triggering of action event from Next JButton of Panel1 class, I am getting NullPointerException. How to resolve this? plzz help.
public class PanelEventTest
{
/**
* #param args
*/
JFrame frame;
void originalFrame()
{
frame = new JFrame();
frame.setSize(500, 300);
frame.setVisible(true);
frame.setLayout(new FlowLayout());
frame.add(new TestPanel1().panel1());
frame.add(new TestPanel2().panel2());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new PanelEventTest().originalFrame();
}
}
public class TestPanel1
{
JPanel panel1;
JButton next;
JPanel panel1()
{
panel1 = new JPanel();
next = new JButton("Next");
next.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
new TestPanel2().panel2.removeAll();
}
});
panel1.add(next);
return panel1;
}
}
public class TestPanel2
{
JPanel panel2;
JList jlist;
String[] list = {"Sachin","Tarun","Vipin"};
JPanel panel2()
{
panel2 = new JPanel();
jlist = new JList(list);
panel2.add(jlist);
panel2.add(new JLabel("Test"));
return panel2;
}
}
My last question Nullpointerexception with JPanel was successfully resolved by you guys. Plz help in this. This exception is eating my head.

If you try changing this line:
new TestPanel2().panel2.removeAll();
To:
new TestPanel2().panel2().removeAll();
Will solve the problem, but the current logic is flawed.
A better solution is:
Change TestPanel2 to:
public class TestPanel2 {
JPanel panel2;
JList jlist;
String[] list = { "Sachin", "Tarun", "Vipin" };
public TestPanel2() { // was: JPanel panel2() {
panel2 = new JPanel();
jlist = new JList(list);
panel2.add(jlist);
panel2.add(new JLabel("Test"));
// was: return panel2;
}
}
And then modify TestPanel1 to:
public class TestPanel1 {
JPanel panel1;
JButton next;
public TestPanel1(final JFrame frame, TestPanel2 tp2) { // was: JPanel panel1() {
panel1 = new JPanel();
next = new JButton("Next");
final JPanel panel2 = tp2.panel2; // line created
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
panel2.removeAll(); // was: new TestPanel2().panel2.removeAll();
frame.validate(); // line created
frame.paint(); // line created
}
});
panel1.add(next);
// was: return panel1;
}
}
Finally, on PanelEventTest.originalFrame(), change:
frame.add(new TestPanel1().panel1());
frame.add(new TestPanel2().panel2());
to:
TestPanel2 tp2 = new TestPanel2();
frame.add(new TestPanel1(frame, tp2).panel1);
frame.add(tp2.panel2);
frame.validate();
frame.repaint();
Explanation
You are creating methods, when you needed constructors. You must read this: Understanding constructors.
Also, you need to pass TestPanel2 and the frame to TestPanel1:
Your code was creating a new TestPanel2 (attached to no one) and then calling removeAll() on it's pannel. This has no effect at all (as this panel is not shown anywhere).
The changed then code will call removeAll() on TestPanel1's panel.
Also, you need to revalidate/repaint the components everytime you make a change on them.
Currently you change them when you create the frame (adding the panels) and when you remove the panel2 (in the "Next" button's action).

Related

How can I use JFrame.pack() correctly?

Well, Hey guys.
For how I see it, Pack method is used inorder to store in the minimum size that can still hold all the elements inside a frame.(Correct me if I'm worng).
But I think that I'm using it not in the right way.
I tried to add some compoments to a frame under a jpanel.
Then I called the pack method, and the frame suit it self to the compoments.
After I remove all of the old compoments using JFrame.removeAll(), I added new compoments under a new JPanel. And called the pack method again but this time the panel from the begging was in the minimum size that can't hold the first JPanel and not the new JPanel.
My code :
public Window() {
frame = new JFrame("Auto VPN Connection");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addInstace("loading", new JPanel());
JPanel panel = getInstance("loading");
panel.setSize(new Dimension(300, 200));
JLabel label = new JLabel();
label.setText("Loading..");
panel.add(label);
frame.add(panel);
frame.pack();
frame.show();
Start();
}
public void clearFrame() {
if (frame.getContentPane().getComponents().length > 0)
frame.removeAll();
}
public void Start() {
addInstace("home", new JPanel());
JPanel panel = getInstance("home");
panel.setSize(new Dimension(500, 300));
JButton button = new JButton();
button.setSize(new Dimension(200, 30));
panel.setLayout(null);
button.setText("Welcome");
panel.add(button);
frame.add(panel);
frame.pack();
frame.revalidate();
}
public JPanel getInstance(String name) {
for (Pair<String, JPanel>key : array) {
if (key.getLeft().equalsIgnoreCase(name))
return key.getRight();
}
return null;
}
public void addInstace(String name, JPanel panel) {
if (!instanceExists(name)) {
array.add(new Pair<>(name, panel));
} else {
System.out.print("Exists!");
}
}
public boolean instanceExists(String name) {
for (Pair<String, JPanel>key : array) {
if (key.getLeft().equalsIgnoreCase(name))
return true;
}
return false;
}
If I don't call pack method in Start() so the frame pack It self for the size of the JLabel and It's okay.
But if I do call pack method again in Start(), the frame not pack It self either to the JLabel and to the JButton.
Here are some pictures that exam my situation:
Pack not called in the Start method
Pack is now called in the Start method
Thank you for taking time and read my question, That's it.

Why JPanel.focusGaind and Lost don't work?

Please take a look at the following code (I've missed the imports purposely)
public class MainFrame extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(10, 11, 414, 240);
contentPane.add(tabbedPane);
JPanel panel = new JPanel();
panel.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent arg0) {
System.out.println("lost");
// I want to do something here, if I reach here!
}
#Override
public void focusGained(FocusEvent arg0) {
System.out.println("gained");
// I want to do something here, if I reach here!
}
});
tabbedPane.addTab("New tab", null, panel, null);
JButton button = new JButton("New button");
panel.add(button);
JPanel panel_1 = new JPanel();
tabbedPane.addTab("New tab", null, panel_1, null);
JPanel panel_2 = new JPanel();
tabbedPane.addTab("New tab", null, panel_2, null);
}
}
I've created this class to test it and then add the onFocusListener in my main code, but it's not working the way I expect. Please tell what's wrong or is this the right EvenetListener at all?
JPanels are not focusable by default. If you ever wanted to use a FocusListener on them, you'd first have to change this property via setFocusable(true).
But even if you do this, a FocusListener is not what you want.
Instead I'd look to listen to the JTabbedPane's model for changes. It uses a SingleSelectionModel, and you can add a ChangeListener to this model, listen for changes, check the component that is currently being displayed and if your component, react.
You are using setBounds and null layouts, something that you will want to avoid doing if you are planning on creating and maintaining anything more than a toy Swing program.
Edit
For example:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.event.*;
#SuppressWarnings("serial")
public class MainPanel extends JPanel {
private static final int PREF_W = 450;
private static final int PREF_H = 300;
private static final int GAP = 5;
private static final int TAB_COUNT = 5;
private JTabbedPane tabbedPane = new JTabbedPane();
public MainPanel() {
for (int i = 0; i < TAB_COUNT; i++) {
JPanel panel = new JPanel();
panel.add(new JButton("Button " + (i + 1)));
panel.setName("Panel " + (i + 1));
tabbedPane.add(panel.getName(), panel);
}
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new BorderLayout());
add(tabbedPane, BorderLayout.CENTER);
tabbedPane.getModel().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent evt) {
Component component = tabbedPane.getSelectedComponent();
System.out.println("Component Selected: " + component.getName());
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
MainPanel mainPanel = new MainPanel();
JFrame frame = new JFrame("MainPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
JPanel is a lightweight container and it is not a Actionable component so it does not get focus events. It lets you add focus listener because of swing component hierarchy. In Order to get tab selected events you need to use JTabbedPane#addChangeListener.
Hope this helps.

Get Parent Frame object from JPanel object

Is it possible to get the parent JFrame object from JPanel class??
Actually I am trying to make a GUI using NetBeans.The GUI has a Frame and Two panels.
1) Login Panel (having two text field and button)
2) Second Panel
When JFrame loaded, I add LoginPanel in it initially.
public ParentJFrame() { //in constructor
initComponents();
this.setLayout(new BorderLayout());
this.setBounds(300,300, 300, 300);
this.getContentPane().add(new LoginPanel());
}
After click on button (of LoginPanel) , I am trying to remove LoginPanel from JFrame and adding SecondPanel.
Now I am removing LoginPanel and adding SecondPanel in LoginPanel class where I can access the username , password fields but here I am unable to get the JFrame object from which I have to remove this component.
If I try this in ParentJFrameClass then it is not possible for me to access username , password field's value. (As this is the structure provided by netbeans)
So what Should I do now? What would be the solution in this caseI hope I have explained the problem in detail but in case if anything is not clear please let me know
Experts Please help
To use cardlayout to change between the panels,like follwing code may help you,you need to modify it to meet your need:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* replace the real panel of yours
* do the right process
*/
public class CardLayoutLoginDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
JFrame frame = new LoginFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class LoginFrame extends JFrame {
public LoginFrame() {
super("CardLayout Demo");
//Create the panel that contains the "cards".
mainPanel = new JPanel(new CardLayout());
mainPanel.add(getFirstPanel(), FIRST);
mainPanel.add(getSecondPanel(), SECOND);
this.setContentPane(mainPanel);
this.pack();
}
//use your first panel
private JPanel getFirstPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,2));
panel.add(new JLabel("Username: "));
panel.add(new JTextField(10));
panel.add(new JLabel("password: "));
panel.add(new JTextField(10));
JButton btnLogin = new JButton("Login");
JButton btnCancel = new JButton("Cancel");
btnLogin.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
CardLayout cl = (CardLayout)(mainPanel.getLayout());
cl.show(mainPanel, SECOND);
}
});
btnCancel.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
panel.add(btnLogin);
panel.add(btnCancel);
return panel;
}
//use the second panel
private JPanel getSecondPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,2));
panel.add(new JLabel("Second panel"));
panel.add(new JLabel("other processs"));
JButton btnOther = new JButton("Ok");
JButton btnBack = new JButton("Back");
panel.add(btnOther);
panel.add(btnBack);
btnBack.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
CardLayout cl = (CardLayout)(mainPanel.getLayout());
cl.show(mainPanel, FIRST);
}
});
return panel;
}
private final String FIRST = "First panel";
private final String SECOND = "Second panel";
private static final long serialVersionUID = 1L;
private JPanel mainPanel;
}

Getting Static from NonStatic Java (ActionListener)

From my action listener which I believe is static, im trying to call a non static method from a class. How can i call my method from my class?
public class addContent {
User Darryl = new User();
public static void addStuff(){
//Panel and Frame
JPanel panel = new JPanel(new BorderLayout());
JFrame frame = new JFrame("PandaHunterV3");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Setup labels
JLabel label = new JLabel("Label");
frame.getContentPane().add(label);
//Setup buttons
JButton button = new JButton("Button");
frame.getContentPane().add(button);
//Action listener
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
Darryl.showHealth(); // HERE IS THE PROBLEM.
}
});
//Crap
frame.pack();
frame.setVisible(true);
}
}
and my class from which i am trying to call the methods from
public class User {
int health;
User(){
health = 50;
}
public void showHealth(){
System.out.print(health);
}
public void incHealth(){
health += 20;
}
}
EDIT:
Mark the Daryl instance as static or the method addStuff() as non-static.
btw. use low case for naming the variables / instances and upper-case for class names.
public class AddContent {
private User darryl = new User();
public void addStuff(){
//Panel and Frame
JPanel panel = new JPanel(new BorderLayout());
JFrame frame = new JFrame("PandaHunterV3");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Setup labels
JLabel label = new JLabel("Label");
frame.getContentPane().add(label);
//Setup buttons
JButton button = new JButton("Button");
frame.getContentPane().add(button);
//Action listener
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
AddContent.this.darryl.showHealth(); // SHOULD BE FINE
}
});
//Crap
frame.pack();
frame.setVisible(true);
}
}

Adding JPanels from other classes to the cardLayout

I've got 3 windows in 3 separate classes and I would like to use cardLayout so that when you click the next button, the next window will appear. How do I add JPanels containing different elements to one cardLayout? This is the first window: (the only difference is the background though - but it represents the idea of how I got it actually)
public class Window1 extends JPanel implements ActionListener {
static CardLayout cardLayout = new CardLayout();
public Window1() {
init();
}
private void init() {
JPanel jp = new JPanel(new BorderLayout());
JPanel jp2 = new Window2();
//JPanel jp3 = new Window3();
JLabel textLabel = new JLabel("Window1");
jp.setBackground(Color.GREEN);
jp.add(textLabel, BorderLayout.CENTER);
JButton nextButton = new JButton("NEXT");
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
jp.add(nextButton, BorderLayout.EAST);
setLayout(cardLayout);
add(jp, "string");
add(jp2, "string");
//add(jp3, "string");
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("next")) {
// go to the next window
cardLayout.next(this);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("test");
frame.getContentPane().setLayout(Window1.cardLayout);
frame.getContentPane().add(new Window1(), "Center");
frame.getContentPane().add(new Window2(), "Center");
frame.getContentPane().add(new Window3(), "Center");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 450);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
The second window:
public class Window2 extends JPanel implements ActionListener {
//static CardLayout cardLayout = new CardLayout();
public Window2() {
init();
}
private void init() {
setLayout(new BorderLayout());
JLabel textLabel = new JLabel("Window2");
setBackground(Color.RED);
add(textLabel, BorderLayout.CENTER);
JButton nextButton = new JButton("NEXT");
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
add(nextButton, BorderLayout.EAST);
//setLayout(cardLayout);
//JPanel jp3 = new Window3();
//add(jp3, "string");
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("next")) {
// go to the next window??
System.out.println("window2");
Window1.cardLayout.next(this);
}
}
}
And the last one:
public class Window3 extends JPanel implements ActionListener {
public Window3() {
init();
}
private void init() {
setLayout(new BorderLayout());
JLabel textLabel = new JLabel("Window3");
setBackground(Color.BLUE);
add(textLabel, BorderLayout.CENTER);
JButton nextButton = new JButton("NEXT");
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
add(nextButton, BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("next")) {
// go to the next window
// Window1.cardLayout.next(this);
}
}
}
I had made a small program, hopefully the comments written in the program, might be able to guide you, to understand how to use CardLayout.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* Here we are first declaring our class that will act as the
* base for other panels or in other terms the base for CardLayout.
*/
public class CardLayoutTest
{
private static final String CARD_JBUTTON = "Card JButton";
private static final String CARD_JTEXTFIELD = "Card JTextField";
private static final String CARD_JRADIOBUTTON = "Card JRadioButton";
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Card Layout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
// This JPanel is the base for CardLayout for other JPanels.
final JPanel contentPane = new JPanel();
contentPane.setLayout(new CardLayout(20, 20));
/* Here we be making objects of the Window Series classes
* so that, each one of them can be added to the JPanel
* having CardLayout.
*/
Window1 win1 = new Window1();
contentPane.add(win1, CARD_JBUTTON);
Window2 win2 = new Window2();
contentPane.add(win2, CARD_JTEXTFIELD);
Window3 win3 = new Window3();
contentPane.add(win3, CARD_JRADIOBUTTON);
/* We need two JButtons to go to the next Card
* or come back to the previous Card, as and when
* desired by the User.
*/
JPanel buttonPanel = new JPanel();
final JButton previousButton = new JButton("PREVIOUS");
previousButton.setBackground(Color.BLACK);
previousButton.setForeground(Color.WHITE);
final JButton nextButton = new JButton("NEXT");
nextButton.setBackground(Color.RED);
nextButton.setForeground(Color.WHITE);
buttonPanel.add(previousButton);
buttonPanel.add(nextButton);
/* Adding the ActionListeners to the JButton,
* so that the user can see the next Card or
* come back to the previous Card, as desired.
*/
previousButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.previous(contentPane);
}
});
nextButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});
// Adding the contentPane (JPanel) and buttonPanel to JFrame.
frame.add(contentPane, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
class Window1 extends JPanel
{
/*
* Here this is our first Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of Two JButtons.
*/
private ActionListener action;
public Window1()
{
init();
}
private void init()
{
final JButton clickButton = new JButton("CLICK ME");
final JButton dontClickButton = new JButton("DON\'T CLICK ME");
action = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == clickButton)
{
JOptionPane.showMessageDialog(null, "Hello there dude!"
, "Right Button", JOptionPane.INFORMATION_MESSAGE);
}
else if (ae.getSource() == dontClickButton)
{
JOptionPane.showMessageDialog(null, "I told you not to click me!"
, "Wrong Button", JOptionPane.PLAIN_MESSAGE);
}
}
};
clickButton.addActionListener(action);
dontClickButton.addActionListener(action);
add(clickButton);
add(dontClickButton);
}
}
class Window2 extends JPanel implements ActionListener
{
/*
* Here this is our second Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of a JLabel and a JTextField
* with GridLayout.
*/
private JTextField textField;
public Window2()
{
init();
}
private void init()
{
setLayout(new GridLayout(1, 2));
JLabel userLabel = new JLabel("Your Name : ");
textField = new JTextField();
textField.addActionListener(this);
add(userLabel);
add(textField);
}
public void actionPerformed(ActionEvent e)
{
if (textField.getDocument().getLength() > 0)
JOptionPane.showMessageDialog(null, "Your Name is : " + textField.getText()
, "User\'s Name : ", JOptionPane.QUESTION_MESSAGE);
}
}
class Window3 extends JPanel
{
/*
* Here this is our third Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of Two JLabels and two JCheckBox
* with GridLayout.
*/
private ActionListener state;
public Window3()
{
init();
}
public void init()
{
setLayout(new GridLayout(2, 2));
JLabel maleLabel = new JLabel("MALE", JLabel.CENTER);
final JCheckBox maleBox = new JCheckBox();
JLabel femaleLabel = new JLabel("FEMALE", JLabel.CENTER);
final JCheckBox femaleBox = new JCheckBox();
state = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (maleBox == (JCheckBox) ae.getSource())
{
femaleBox.setSelected(false);
JOptionPane.showMessageDialog(null, "Congrats you are a Male"
, "Gender : ", JOptionPane.INFORMATION_MESSAGE);
}
else if (femaleBox == (JCheckBox) ae.getSource())
{
maleBox.setSelected(false);
JOptionPane.showMessageDialog(null, "Congrats you are a Female"
, "Gender : ", JOptionPane.INFORMATION_MESSAGE);
}
}
};
maleBox.addActionListener(state);
femaleBox.addActionListener(state);
add(maleLabel);
add(maleBox);
add(femaleLabel);
add(femaleBox);
}
}
There are a couple things:
As your program can only run 1 main method, each class doesnt need one, but whichever you do run should create an instance of each panel you want to add to your CardLayout.
You also do not seem to add your Windows to your CardLayout at all. You could try the following (uncomplied). This would only be needed in one class:
private static void createAndShowGUI() {
JFrame frame = new JFrame("test");
frame.getContentPane().setLayout(Window1.cardLayout);
frame.getContentPane().add(new Window1());
frame.getContentPane().add(new Window2());
frame.getContentPane().add(new Window3());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 450);
frame.setVisible(true);
}
Further (and this might just be due to the simplicity of your example), I would just have 1 class, and it would take the name of the panel and the color of the background to the constructor. These could be passed into your init() method and your design would be somewhat streamlined.

Categories