I have a program in which a JPanel is added to a JFrame:
public class Test{
Test2 test = new Test2();
JFrame frame = new JFrame();
Test(){
...
frame.setLayout(new BorderLayout());
frame.add(test, BorderLayout.CENTER);
...
}
//main
...
}
public class Test2{
JPanel test2 = new JPanel();
Test2(){
...
}
}
I get an error asking me to change type of 'panel' to 'component'. I do I fix this error? It wants me to do: Component panel = new Component();
JPanel is a subclass of Component, so any method that takes a Component as an argument can also take a JPanel as an argument. Older versions didn't let you add directly to a JFrame; you had to use JFrame. getContentPane(). add(Component).
You can't put one JFrame inside another. You have a couple of design choices here. You can change your JFrames to JPanels. This is probably the easiest change.
The panels connect to one another. So all you need to do us use a panel with a FlowLayout that uses a horizontal gap of 0. Your main code can be something like: JPanel main = new JPanel( new FlowLayout(FlowLayout.
public class Test{
Test2 test = new Test2();
JFrame frame = new JFrame();
Test(){
...
frame.setLayout(new BorderLayout());
frame.add(test, BorderLayout.CENTER);
...
}
//main
...
}
//public class Test2{
public class Test2 extends JPanel {
//JPanel test2 = new JPanel();
Test2(){
...
}
do it simply
public class Test{
public Test(){
design();
}//end Test()
public void design(){
JFame f = new JFrame();
f.setSize(int w, int h);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
JPanel p = new JPanel();
f.getContentPane().add(p);
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
new Test();
}catch(Exception e){
e.printStackTrace();
}
}
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With