2013-12-08 90 views
0

我正在嘗試創建程序,您可以在第一頁上按其中一個按鈕,然後打開更多按鈕和標籤等。但我不知道如何製作,以便按下按鈕時會顯示其他按鈕和標籤。如何使按鈕顯示其他按鈕和選項。

的代碼是

public class Japp extends JApplet implements ActionListener{ 

    private JPanel panel1= new JPanel(); 
    private JPanel panel2= new JPanel(); 

    JPanel container = new JPanel(); 
    //container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS)); 

    JButton reg = new JButton ("Register"); 
    JButton log = new JButton ("Login"); 

    JLabel regusrlbl = new JLabel ("Enter Username"); 
    JLabel regpwdlbl = new JLabel ("Enter Password"); 
    JLabel regpwdconlbl = new JLabel ("Confirm Username"); 
    JTextField regusr = new JTextField(""); 
    JTextField regpwd = new JTextField(""); 
    JTextField regpwdcon = new JTextField(""); 
    JButton create = new JButton("Create"); 

    JLabel logusrlbl = new JLabel ("Enter Username"); 
    JLabel logpwdlbl = new JLabel ("Enter Password"); 
    JTextField logusr = new JTextField(""); 
    JTextField logpwd = new JTextField(""); 
    JButton login = new JButton("Login"); 

    public void init(){ 
    setLayout(null); 
    add(reg).setBounds(50,100,100,30); 
    add(log).setBounds(150,100,100,30); 

    } 
    @SuppressWarnings("deprecation") 

    public void actionPerformed(ActionEvent e) { 
     JButton source = (JButton)e.getSource(); 
     if (source.getLabel()=="reg"){ 

     // TODO Auto-generated method stub 
      remove(reg); 
      remove(log); 
      add(regusrlbl).setBounds(20,20, 150,50); 
      add(regusr).setBounds(20,50,100,30); 
      add(regpwdlbl).setBounds(200,20,150,50); 
      add(regpwd).setBounds(200,50,100,30); 
      add(regpwdconlbl).setBounds(195,80,150,30); 
      add(regpwdcon).setBounds(200,110,100,30); 
      add(create).setBounds(110,140,100,30); 
     // setup all the context... 
     } 
     else if(source.getLabel()=="log"){ 
      remove(reg); 
      remove(log); 
      add(logusrlbl).setBounds(20,20, 150,50); 
      add(logusr).setBounds(20,50,100,30); 
      add(logpwdlbl).setBounds(200,20,150,50); 
      add(logpwd).setBounds(200,50,100,30); 
      add(login).setBounds(110,140,100,30); 
     } 
    } 
} 

回答

2
  1. 避免null佈局。您無法控制字體在不同系統上的渲染方式,因此除非您僅在您開發的計算機上運行應用程序,否則應避免使用null佈局。學習如何使用和理解如何使用不同的佈局管理器來實現你想要的。
  2. 使用CardLayout,這是它的設計目的,允許您輕鬆更改可見組件的組。

這就要求你對你的應用程序分解成獨立的組件/面板,但只會讓你專注於每一部分獨立

更新與例如

enter image description hereenter image description hereenter image description here

的個性化需求
import java.awt.CardLayout; 
import java.awt.Container; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
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.JPanel; 
import javax.swing.JPasswordField; 
import javax.swing.JSeparator; 
import javax.swing.JTextField; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestCardLayout100 { 

    public static void main(String[] args) { 
     new TestCardLayout100(); 
    } 

    public TestCardLayout100() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       LoginPane loginPane = new LoginPane(); 
       RegisterPane registerPane = new RegisterPane(); 

       JPanel startPane = new JPanel(new GridBagLayout()); 
       GridBagConstraints gbc = new GridBagConstraints(); 
       gbc.gridx = 0; 
       gbc.gridy = 0; 
       startPane.add(loginPane, gbc); 
       gbc.gridx = 2; 
       startPane.add(registerPane, gbc); 
       gbc.gridx = 1; 
       gbc.fill = GridBagConstraints.VERTICAL; 
       startPane.add(new JSeparator(JSeparator.VERTICAL), gbc); 

       JPanel loggedInPane = new JPanel(new GridBagLayout()); 
       loggedInPane.add(new JLabel("Logged In...")); 

       JPanel registeredPane = new JPanel(new GridBagLayout()); 
       registeredPane.add(new JLabel("Registered...")); 

       final JFrame frame = new JFrame("Testing"); 
       final CardLayout cardLayout = new CardLayout(); 

       loginPane.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         cardLayout.show(frame.getContentPane(), "loggedIn"); 
        } 
       }); 
       registerPane.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         cardLayout.show(frame.getContentPane(), "registered"); 
        } 
       }); 

       Container contentPane = frame.getContentPane(); 
       contentPane.setLayout(cardLayout); 
       contentPane.add(startPane, "startPane"); 
       contentPane.add(loggedInPane, "loggedIn"); 
       contentPane.add(registeredPane, "registered"); 

       cardLayout.show(contentPane, "startPane"); 

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class LoginPane extends JPanel { 

     private final JLabel logusrlbl = new JLabel("Enter Username"); 
     private final JLabel logpwdlbl = new JLabel("Enter Password"); 
     private final JTextField logusr = new JTextField(10); 
     private final JTextField logpwd = new JPasswordField(10); 
     private final JButton login = new JButton("Login"); 

     public LoginPane() { 
      setLayout(new GridBagLayout()); 

      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      gbc.gridwidth = GridBagConstraints.REMAINDER; 
      add(new JLabel("Login"), gbc); 

      gbc.gridy++; 
      gbc.gridwidth = 1; 
      add(logusrlbl, gbc); 
      gbc.gridy++; 
      add(logpwdlbl, gbc); 

      gbc.gridx++; 
      gbc.weightx = 1; 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.gridy = 1; 

      add(logusr, gbc); 
      gbc.gridy++; 
      add(logpwd, gbc); 

      gbc.gridx = 0; 
      gbc.gridy++; 
      gbc.anchor = GridBagConstraints.EAST; 
      gbc.gridwidth = GridBagConstraints.REMAINDER; 
      gbc.fill = GridBagConstraints.NONE; 
      add(login, gbc); 

     } 

     public void addActionListener(ActionListener listener) { 
      login.addActionListener(listener); 
     } 

    } 

    public class RegisterPane extends JPanel { 

     private final JLabel logusrlbl = new JLabel("Enter Username"); 
     private final JLabel logpwdlbl = new JLabel("Enter Password"); 
     private final JTextField logusr = new JTextField(10); 
     private final JTextField logpwd = new JPasswordField(10); 
     private final JButton register = new JButton("Register"); 

     public RegisterPane() { 
      setLayout(new GridBagLayout()); 

      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      gbc.gridwidth = GridBagConstraints.REMAINDER; 
      add(new JLabel("Register"), gbc); 

      gbc.gridy++; 
      gbc.gridwidth = 1; 
      add(logusrlbl, gbc); 
      gbc.gridy++; 
      add(logpwdlbl, gbc); 

      gbc.gridx++; 
      gbc.weightx = 1; 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.gridy = 1; 

      add(logusr, gbc); 
      gbc.gridy++; 
      add(logpwd, gbc); 

      gbc.gridx = 0; 
      gbc.gridy++; 
      gbc.anchor = GridBagConstraints.EAST; 
      gbc.fill = GridBagConstraints.NONE; 
      gbc.gridwidth = GridBagConstraints.REMAINDER; 
      add(register, gbc); 

     } 

     public void addActionListener(ActionListener listener) { 
      register.addActionListener(listener); 
     } 

    } 

} 
+0

您可以詳談嗎?我在CardLayout上閱讀了很多,但我不知道如何將它應用於此代碼。我仍然是一個非常新的程序員。謝謝! – user2962041

+0

我用一個可運行的示例更新了它,但您確實應該閱讀鏈接教程(從第2點開始) – MadProgrammer

相關問題