2015-12-12 47 views
0

我想要做的是每次按下按鈕時更改GUI的右側。第一個按鈕顯示JLabel第二個按鈕a JTextField。專家組預期的結果改變。結果是,當我按下按鈕什麼都沒有發生。在gui中更改面板

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); 
     } 

    } 
} 
+0

你的問題是什麼? [how-to-ask](http://stackoverflow.com/help/how-to-ask) –

+0

我正在嘗試創建一個gui,其左側有2個按鈕,右側有一個面板。按下第一個按鈕我想讓右側的面板顯示一個JLabel,而按下第二個按鈕我希望它顯示一個JTextField。我的按鈕不會對右側面板做任何事情! – Justanewb

回答

1

您需要使用CardLayout。如果你需要幫助問我。

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); 
    } 
}