Can anyone help me with this code. I'm a beginner on java programming. I'm using netbeans JFrame Form, I added 2 buttons for my add and remove then a tabbed pane. The buttons doesn't work.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class BulletinBoard extends javax.swing.JFrame {
public class Tab{
public Tab(){
add.addActionListener(new MyAction());
remove.addActionListener(new MyAction());
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String str = e.getActionCommand();
if(add.equals(str)){
String st = JOptionPane.showInputDialog(null, "Enter Tab Name.");
if(!st.equals("")){
JPanel panel2 = new JPanel();
JLabel label = new JLabel("Your program is working successfully.");
panel2.add(label);
tab.add(st, panel2);
}
}
else if(str.equals(remove)){
tab.remove(tab.getTabCount()-1);
}
}
}
}
/** Creates new form BulletinBoard */
public BulletinBoard() {
initComponents();
Tab ar = new Tab();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
}
public void run() {
new BulletinBoard().setVisible(true);
}
});
}
This is the code that I follow, it is hard coded, but I can't implement it.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Tab{
JTabbedPane tab;
public static void main(String[] args){
Tab ar = new Tab();
}
public Tab(){
JFrame frame = new JFrame("Add Remove Tab Frame");
tab = new JTabbedPane();
frame.add(tab, BorderLayout.CENTER);
JPanel panel = new JPanel();
JButton button = new JButton("Add Tab");
button.addActionListener(new MyAction());
panel.add(button);
tab.add("Add", panel);
JPanel panel1 = new JPanel();
JButton button1 = new JButton("Remove Tab");
button1.addActionListener(new MyAction());
panel1.add(button1);
tab.add("Remove ", panel1);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String str = e.getActionCommand();
if(str.equals("Add Tab")){
String st = JOptionPane.showInputDialog(null, "Enter Tab Name.");
if(!st.equals("")){
JPanel panel2 = new JPanel();
JLabel label = new JLabel("Your program is working successfully.");
panel2.add(label);
tab.add(st, panel2);
}
}
else if(str.equals("Remove Tab")){
tab.remove(tab.getTabCount()-1);
}
}
}
}