Reputation: 13
What I am trying to do is change the right side of my GUI each time I press a button. First button shows a JLabel
second button a JTextField
. Expected outcome change in panels. Outcome is that when I press the buttons nothing happens.
package javaapplication37;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.*;
public class Gui extends JFrame {
JTextField f1;
JPanel b, p1, p2;
JPanel p3;
JLabel l1;
JButton b2, b1;
String a;
public Gui() {
a="Input here";
setSize(600,600);
l1=new JLabel("8a petuxei");
b = new JPanel();
p3 = new JPanel();
p1 = new JPanel();
p2 = new JPanel();
b.setLayout(new GridLayout(2, 1));
b1 = new JButton("Eleos");
b2 = new JButton("elpizw");
b.add(b1);
b.add(b2);
b.setSize(150,600);
p1.setSize(450,600);
add(b);
add(p1);
ActionListener pou = new Listener(p1);
b1.addActionListener(pou);
p2.add(l1);
f1=new JTextField(a);
a=f1.getText();
p3.add(f1);
}
public class Listener implements ActionListener {
JPanel k;
public Listener(JPanel k) {
this.k = k;
}
@Override
public void actionPerformed(ActionEvent e) {
k.remove(getContentPane());
k.add(p2);
}
}
public class l implements ActionListener {
JPanel k;
public l(JPanel k) {
this.k = k;
}
@Override
public void actionPerformed(ActionEvent e) {
k.remove(getContentPane());
k.add(p3);
}
}
}
Upvotes: 0
Views: 253
Reputation: 418
You need to be using a CardLayout. If you need help ask me.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Gui extends JFrame implements ActionListener {
private JPanel menu = new JPanel();
private CardLayout contentLayout = new CardLayout();
private JPanel content = new JPanel(contentLayout);
private java.util.List<Card> cardList = new ArrayList<>();
public Gui() {
int i;
for (i = 0; i < 10; i++) {
Card card;
if(i % 2 == 0) card = new TextAreaCard("Card " + i, "this is the content for card #" + i);
else card = new LabelCard("Card " + i, "content for Label Card #" + i);
JButton btn = new JButton(card.name);
menu.add(btn);
btn.addActionListener(this);
content.add(card, card.name);
cardList.add(card);
}
menu.setLayout(new GridLayout(i, 1));
add(menu, BorderLayout.WEST);
add(content, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
contentLayout.show(content, e.getActionCommand());
}
class Card extends JPanel{
final String name;
public Card(String name){
this.name = name;
}
}
class TextAreaCard extends Card implements ActionListener {
JTextArea textArea = new JTextArea();
JButton btn = new JButton("OK");
public TextAreaCard(String name, String text) {
super(name);
textArea.setText(text);
setLayout(new BorderLayout());
add(textArea, BorderLayout.CENTER);
add(btn, BorderLayout.SOUTH);
btn.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(this, textArea.getText(), "click OK", JOptionPane.NO_OPTION);
}
}
class LabelCard extends Card{
JLabel label = new JLabel();
public LabelCard(String name, String text) {
super(name);
label.setText(text);
add(label);
}
}
public static void main(String [] args){
Gui gui = new Gui();
gui.setSize(600, 500);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
}
Upvotes: 1