Calling a non-static method out of a package
Hello,
my problem is that i cant call the "setCard"-method in the "HauptPanel"-class. I would apreciate every suggestion :(nod):
You propably wont need to look at this class:
Code:
import java.awt.*;
import javax.swing.*;
import panels.*;
public class JFrameWindow extends JFrame
{
HauptPanel hauptPanel;
JFrameWindow()
{
super("TITEL");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setMinimumSize(new Dimension(100,100));
setSize(500,500);
setResizable(false);
setLocationRelativeTo(null);
hauptPanel = new HauptPanel();
Container con = getContentPane();
con.add(hauptPanel);
hauptPanel.setCard("Hauptmenü");
setVisible(true);
}
private void resizeWindow(int newWidth, int newHeight)
{
this.setSize(newWidth,newHeight);
}
public static void main(String args[])
{
JFrameWindow frame = new JFrameWindow();
}
}
Code:
package panels;
import java.awt.CardLayout;
import javax.swing.*;
import java.awt.*;
import panels.*;
public class HauptPanel extends JPanel
{
private CardLayout cardLayout;
public HauptPanel()
{
cardLayout = new CardLayout();
setLayout( cardLayout );
// Panel zum CardLayout hinzufügen
add( new panels.HauptmenuePanel(), "Hauptmenü" );
add( new panels.OptionenPanel(), "Optionen" );
//add( new Panel3(), "Panel3" );
//add( new Panel4(), "Panel4" );
//add( new Panel5(), "Panel5" );
}
// Methode zeigt die gewünschte Seite (Panel) im Frame an
public void setCard( String cardname ) { <-- METHOD THAT HAS TO BE CALLED
cardLayout.show( this, cardname );
}
}
Here is the problem (at the very bottom):
Code:
package panels;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class HauptmenuePanel extends JPanel implements ActionListener {
JButton b1;
JButton b2;
public HauptmenuePanel() {
this.setBackground(Color.WHITE);
b1 = new JButton("Start");
this.add(b1);
b1.addActionListener(this);
b2 = new JButton("Optionen");
this.add(b2);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1){
System.out.println("Start-button was pressed!");
//hauptPanel.setCard("DrawingPanel");
}
if(e.getSource()==b2){
System.out.println("Optionen-button was pressed!");
//hauptPanel.setCard("Optionen"); <-- PROBLEM
}
}
}
Sorry for the wall of code, but you dont really need to understand it. I just need to call that one method. :smash:
Re: Calling a non-static method out of a package
You need to have a reference to the HauptPanel object in the HauptmenuePanel object. One way to do this is to pass this reference in the HauptmenuePanel class's constructor. For example (note changes indicated by the //!! comment):
Code:
class HauptmenuePanel extends JPanel implements ActionListener {
JButton b1;
JButton b2;
private HauptPanel hauptPanel; //!!
public HauptmenuePanel(HauptPanel hauptPanel) { //!!
this.hauptPanel = hauptPanel; //!!
this.setBackground(Color.WHITE);
b1 = new JButton("Start");
this.add(b1);
b1.addActionListener(this);
b2 = new JButton("Optionen");
this.add(b2);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
System.out.println("Start-button was pressed!");
hauptPanel.setCard("DrawingPanel");
// hauptPanel.setCard("DrawingPanel");
}
if (e.getSource() == b2) {
System.out.println("Optionen-button was pressed!");
hauptPanel.setCard("Optionen"); //!!<-- PROBLEM
}
}
}
and
Code:
public HauptPanel() {
cardLayout = new CardLayout();
setLayout(cardLayout);
add(new HauptmenuePanel(this), "Hauptmenu"); //!!
Re: Calling a non-static method out of a package
Hi,
thank you a lot, you really helped me :)
kindly regards, kaffee