I have a JPanel (mainPanel) with CardLayout
which contains 4 JPanels (childPanel).
Each childPanel
contains:
- a JPopupMenu with 4 JMenuItems to switch between the childPanels (First,Last,Next,Previous)
- a label with the time when it got visible
- a
toggleButton
that shows/hides the label
I created this working version but I think it can be optimized. Any advice is welcome.
public final class Prozor extends JFrame
{
private static final Dimension SIZE = new Dimension(400, 300);
private final JPanel panelButtoni = new JPanel();
private JButton prvi;
private JButton zadnji;
private JButton sljedeci;
private JButton prethodni;
private CardLayout cardLayout = new CardLayout();
private JPanel panelCards = new JPanel(cardLayout);
private JPopupMenu crveniMeni= new JPopupMenu();
private JPanel crveniPanel= new JPanel();
private JPopupMenu crveniKontekstni= new JPopupMenu();
private JLabel crvenaLabelica = new JLabel();
private JToggleButton crveniButton;
private JPopupMenu plaviMeni= new JPopupMenu();
private JPanel plaviPanel = new JPanel();
private JPopupMenu plaviKontekstni = new JPopupMenu();
private JLabel plavaLabelica = new JLabel();
private JToggleButton plaviButton;
private JPopupMenu zeleniMeni = new JPopupMenu();
private JPanel zeleniPanel = new JPanel();
private JPopupMenu zeleniKontekstni = new JPopupMenu();
private JLabel zelenaLabelica = new JLabel();
private JToggleButton zeleniButton;
private JPopupMenu zutiMeni= new JPopupMenu();
private JPanel zutiPanel = new JPanel();
private JPopupMenu zutiKontekstni = new JPopupMenu();
private JLabel zutaLabelica = new JLabel();
private JToggleButton zutiButton;
public Prozor(String title)
{
super(title);
setPreferredSize(SIZE);
setMinimumSize(SIZE);
setMaximumSize(SIZE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
kreirajbuttonAkcije();
kreirajCrveniPanel();
kreirajPlaviPanel();
kreirajZeleniPanel();
kreirajZutiPanel();
this.add(panelCards);
pack();
}
public static void main(String[] args)
{
Prozor p = new Prozor("ime");
p.setVisible(true);
}
private void kreirajCrveniPanel() {
crveniPanel.setBackground(Color.RED);
crveniButton = new JToggleButton("Prikazi vrijeme");
crveniButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae)
{
if (crveniButton.isSelected()) {
crvenaLabelica.setText(Calendar.getInstance().getTime().toString());
crvenaLabelica.setVisible(true);
}
else
{
crvenaLabelica.setVisible(false);
}
}
});
JMenuItem jmPrvi = new JMenuItem("Prvi");
jmPrvi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.first(panelCards);
}
});
JMenuItem jmZadnji = new JMenuItem("Zadnji");
jmZadnji.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.last(panelCards);
}
});
JMenuItem jmSljedeci = new JMenuItem("Sljedeći");
jmSljedeci.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.next(panelCards);
}
});
JMenuItem jmPrethodni = new JMenuItem("Prethodni");
jmPrethodni.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.previous(panelCards);
}
});
crveniPanel.addMouseListener(new MouseAdapter()
{
@Override
public void mouseReleased(MouseEvent me) {
if (me.isPopupTrigger()) {
crveniMeni.show(me.getComponent(), me.getX(), me.getY());
}
}
});
crveniMeni.add(jmPrvi);
crveniMeni.add(jmZadnji);
crveniMeni.add(jmPrethodni);
crveniMeni.add(jmSljedeci);
crveniPanel.add(crveniMeni);
crveniPanel.add(crvenaLabelica);
crveniPanel.add(crveniButton);
panelCards.add(crveniPanel);
}
private void kreirajPlaviPanel() {
plaviPanel.setBackground(Color.BLUE);
plaviButton =new JToggleButton("Prikazi vrijeme");
plaviButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (plaviButton.isSelected()) {
plavaLabelica.setText(Calendar.getInstance().getTime().toString());
plavaLabelica.setVisible(true);
}else{
plavaLabelica.setVisible(false);
}
}
});
JMenuItem jmPrvi = new JMenuItem("Prvi");
jmPrvi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.first(panelCards);
}
});
JMenuItem jmZadnji = new JMenuItem("Zadnji");
jmZadnji.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.last(panelCards);
}
});
JMenuItem jmSljedeci = new JMenuItem("Sljedeći");
jmSljedeci.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.next(panelCards);
}
});
JMenuItem jmPrethodni = new JMenuItem("Prethodni");
jmPrethodni.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.previous(panelCards);
}
});
plaviPanel.addMouseListener(new MouseAdapter()
{
@Override
public void mouseReleased(MouseEvent me) {
if (me.isPopupTrigger()) {
plaviMeni.show(me.getComponent(), me.getX(), me.getY());
}
}
});
plaviMeni.add(jmPrvi);
plaviMeni.add(jmZadnji);
plaviMeni.add(jmPrethodni);
plaviMeni.add(jmSljedeci);
plaviPanel.add(plaviMeni);
plaviPanel.add(plavaLabelica);
plaviPanel.add(plaviButton);
panelCards.add(plaviPanel);
}
private void kreirajZeleniPanel() {
zeleniPanel.setBackground(Color.GREEN);
zeleniButton=new JToggleButton("Prikazi vrijeme");
zeleniButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (zeleniButton.isSelected()) {
zelenaLabelica.setText(Calendar.getInstance().getTime().toString());
zelenaLabelica.setVisible(true);
}else{
zelenaLabelica.setVisible(false);
}
}
});
JMenuItem jmPrvi = new JMenuItem("Prvi");
jmPrvi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.first(panelCards);
}
});
JMenuItem jmZadnji = new JMenuItem("Zadnji");
jmZadnji.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.last(panelCards);
}
});
JMenuItem jmSljedeci = new JMenuItem("Sljedeći");
jmSljedeci.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.next(panelCards);
}
});
JMenuItem jmPrethodni = new JMenuItem("Prethodni");
jmPrethodni.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.previous(panelCards);
}
});
zeleniPanel.addMouseListener(new MouseAdapter()
{
@Override
public void mouseReleased(MouseEvent me) {
if (me.isPopupTrigger()) {
zeleniMeni.show(me.getComponent(), me.getX(), me.getY());
}
}
});
zeleniMeni.add(jmPrvi);
zeleniMeni.add(jmZadnji);
zeleniMeni.add(jmPrethodni);
zeleniMeni.add(jmSljedeci);
zeleniPanel.add(zeleniMeni);
zeleniPanel.add(zelenaLabelica);
zeleniPanel.add(zeleniButton);
panelCards.add(zeleniPanel);
}
private void kreirajZutiPanel() {
zutiPanel.setBackground(Color.YELLOW);
zutiButton=new JToggleButton("Prikazi vrijeme");
zutiButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (zutiButton.isSelected()) {
zutaLabelica.setText(Calendar.getInstance().getTime().toString());
zutaLabelica.setVisible(true);
}else{
zutaLabelica.setVisible(false);
}
}
});
JMenuItem jmPrvi = new JMenuItem("Prvi");
jmPrvi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.first(panelCards);
}
});
JMenuItem jmZadnji = new JMenuItem("Zadnji");
jmZadnji.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.last(panelCards);
}
});
JMenuItem jmSljedeci = new JMenuItem("Sljedeći");
jmSljedeci.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.next(panelCards);
}
});
JMenuItem jmPrethodni = new JMenuItem("Prethodni");
jmPrethodni.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
cardLayout.previous(panelCards);
}
});
zutiPanel.addMouseListener(new MouseAdapter()
{
@Override
public void mouseReleased(MouseEvent me) {
if (me.isPopupTrigger()) {
zutiMeni.show(me.getComponent(), me.getX(), me.getY());
}
}
});
zutiMeni.add(jmPrvi);
zutiMeni.add(jmZadnji);
zutiMeni.add(jmPrethodni);
zutiMeni.add(jmSljedeci);
zutiPanel.add(zutiMeni);
zutiPanel.add(zutaLabelica);
zutiPanel.add(zutiButton);
panelCards.add(zutiPanel);
}
private void kreirajbuttonAkcije() {
prvi= new JButton("Prvi");
prvi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae)
{
cardLayout.first(panelCards);
}
});
zadnji= new JButton("Zadnji");
zadnji.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae)
{
cardLayout.last(panelCards);
}
});
sljedeci= new JButton("Sljedeći");
sljedeci.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae)
{
cardLayout.next(panelCards);
}
});
prethodni= new JButton("Prethodni");
prethodni.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae)
{
cardLayout.previous(panelCards);
}
});
panelButtoni.add(prvi);
panelButtoni.add(zadnji);
panelButtoni.add(prethodni);
panelButtoni.add(sljedeci);
this.add(panelButtoni,BorderLayout.NORTH);
}
}