I'm trying to add a JPanel to a tab. The panel is created with the LanguageTab class but I can't figure out why it doesn't work. It maybe stupid but self taught is not enough here. Hope someone can give me a little hand. thanks!
I have my code down here for you to understand what I'm going through!
public class MainTab extends JPanel {
JTabbedPane mainTab;
JPanel languageTab;
JFrame mainFrame;
JPanel mainPanel;
public MainTab(){
mainFrame = new JFrame();
mainTab = new JTabbedPane();
mainPanel = new JPanel();
//mainPanel.add(new JTextField("ciao"));
mainPanel.add(new JLabel(new ImageIcon("C:\\Users\\angelica\\Desktop\\developed.jpg")));
//languageTab = new LanguageTab();
mainTab.add("main",mainPanel);
mainTab.add("Language Tab",languageTab);
add(mainTab);
mainFrame.add(mainTab);
mainFrame.setVisible(true);
mainFrame.setDefaultLookAndFeelDecorated(true);
}
public static void main(String args[]){
MainTab mt = new MainTab();
}
}
and this is my LanguageTab
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
public class LanguageTab extends JPanel implements ActionListener{
public LanguageTab(){
ImageIcon icona = new ImageIcon("C:\\Users\\angelica\\workspace\\whatever\\src\\whatever\\ita.jpg");
ImageIcon icona_DSA = new ImageIcon("C:\\Users\\angelica\\workspace\\whatever\\src\\whatever\\ita2.jpg");
ImageIcon iconb = new ImageIcon("C:\\Users\\angelica\\workspace\\whatever\\src\\whatever\\brit.jpg");
ImageIcon iconb_DSA =new ImageIcon("C:\\Users\\angelica\\workspace\\whatever\\src\\whatever\\brit2.jpg");
JPanel langTab= new JPanel();
langTab.setLayout(new GridLayout(2,2));
JButton ADHDbutton = new JButton("ADHD ENGLISH");
ADHDbutton.setIcon(iconb);
ADHDbutton.setActionCommand("adhd_english");
ADHDbutton.addActionListener(this);
JButton ADHDbutton1 = new JButton("ADHD \n ITALIANO");
ADHDbutton1.setIcon(icona);
ADHDbutton1.setActionCommand("adhd_italiano");
ADHDbutton1.addActionListener(this);
JButton DSAbutton = new JButton("DSA ENGLISH");
DSAbutton.setIcon(iconb_DSA);
DSAbutton.setActionCommand("dsa_english");
DSAbutton.addActionListener(this);
JButton DSAbutton1 = new JButton("DSA ITALIANO");
DSAbutton1.setIcon(icona_DSA);
DSAbutton1.setActionCommand("dsa_italiano");
DSAbutton1.addActionListener(this);
langTab.add(ADHDbutton);
langTab.add(ADHDbutton1);
langTab.add(DSAbutton);
langTab.add(DSAbutton1);
//return toModify;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
You need to set the size of your JFrame
so that it's large enough to see its contents.
Add the following as the last line of your MainTab
constructor:
mainFrame.setSize(new Dimension(300, 300));
Additionally, inside your LanguageTab
constructor, you have a variable called langTab
, when the class itself ( this
) is supposed to represent the language tab. You then add all your buttons to langTab
and never do anything with it.
Delete the variable langTab
replace every call to it with this
in the LanguageTab
Constructor. Your constructor should then look something like this:
public LanguageTab() {
// button initialization goes here...
this.add(ADHDbutton);
this.add(ADHDbutton1);
this.add(DSAbutton);
this.add(DSAbutton1);
}
Now that your question has been answered, if you don't mind I would like to suggest a few things that might improve your future code quality.
Only use global variables when it's necessary to.
Give your variables the smallest possible scope
package
scope by default. As I mentioned before it is often necessary to make swing components global for various reasons, but it is almost never necessary for them to have a scope larger than private
(so they can only be accessed from within the class in which they are declared. Access static methods in a "static way".
JFrame
's setDefaultLookAndFeelDecorated()
method using your variable mainFrame
. mainFrame
is an instance of the JFrame
class (you declared it by saying mainFrame = new JFrame()
), so you only use it to call instance methods. setDefaultLookAndFeelDecorated()
is a static method (declared with the keyword static ) and should therefore be accessed with the class name: JFrame.setDefaultLookAndFeelDecorated(true)
Be careful with your reference types.
languageTab
variable (in the MainTab
class) as a JPanel
. JPanel does not implement the ActionListener
interface like your LanguageTab
does, so by declaring your variable as a JPanel
, you lose the ability to call actionPerformed()
on it (or more likely set it as an action listener of some swing component). Variables in Java, by convention, should begin with a lower case letter.
LanguageTab
class names that start with capital letters. The Java convention is for variable names to begin with a lower case letter, in the same way that classes should begin with an upper case letter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.