2017-07-21 10 views
3

複数の画面でプログラムをコーディングしようとしていますが、タブ付きのペインを使用したくありません。私は、カードレイアウトで複数のJPanelを使用してみましたが、メソッドは単に動作していません。私ができることが必要なのは、ボタンがクリックされたときに新しいJPanelをロードすることです。ここに私のコードは次のとおりです。JButtonで新しいJPanelを開くには?

import java.awt.BorderLayout; 
import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import java.awt.CardLayout; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class IA extends JFrame { 

    private JPanel contentPane; 
    private JPanel home; 
    private JPanel clients; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        IA frame = new IA(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public IA() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(new CardLayout(0, 0)); 

     JPanel home = new JPanel(); 
     contentPane.add(home, "name_714429679706141"); 
     home.setLayout(null); 

     JButton btnClients = new JButton("Clients"); 
     btnClients.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       home.setVisible(false); 
       clients.setVisible(true); 
      } 
     }); 
     btnClients.setBounds(160, 108, 89, 23); 
     home.add(btnClients); 

     JPanel clients = new JPanel(); 
     contentPane.add(clients, "name_714431450350356"); 
     clients.setLayout(null); 

     JButton btnHome = new JButton("Home"); 
     btnHome.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       clients.setVisible(false); 
       home.setVisible(true); 
      } 
     }); 
     btnHome.setBounds(169, 107, 89, 23); 
     clients.add(btnHome); 
    } 

} 

答えて

0

問題は、重複した変数homeclientsを持っているということです。

folllowingは、変更された行(5行合計)に対するコメントと、それを修正するための変更したコードであり、次のコードが呼び出されると

import java.awt.CardLayout; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 

public class IA extends JFrame { 

    private final JPanel contentPane; 
    // private final JPanel home; // REMOVED 
    // private JPanel clients; // REMOVED 

    /** 
    * Launch the application. 
    */ 
    public static void main(final String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        IA frame = new IA(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public IA() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(new CardLayout(0, 0)); 

     final JPanel home = new JPanel(); 
     contentPane.add(home, "name_714429679706141"); 
     home.setLayout(null); 

     final JPanel clients = new JPanel(); // MOVED UP 
     contentPane.add(clients, "name_714431450350356"); // MOVED UP 
     clients.setLayout(null); // MOVED UP 

     JButton btnClients = new JButton("Clients"); 
     btnClients.addActionListener(new ActionListener() { 
      public void actionPerformed(final ActionEvent e) { 
       home.setVisible(false); 
       clients.setVisible(true); 
      } 
     }); 
     btnClients.setBounds(160, 108, 89, 23); 
     home.add(btnClients); 

     JButton btnHome = new JButton("Home"); 
     btnHome.addActionListener(new ActionListener() { 
      public void actionPerformed(final ActionEvent e) { 
       clients.setVisible(false); 
       home.setVisible(true); 
      } 
     }); 
     btnHome.setBounds(169, 107, 89, 23); 
     clients.add(btnHome); 
    } 

} 
+0

はどうもありがとうございました!それは完全に動作します!私はこれを動作させるために何時間も頑張っています!再度、感謝します! –

+1

申し訳ありませんが、私はそれを承認しなければならなかったことを認識しませんでした。 –

0

私はしかし、私はあなたがこれを成し遂げるためにActionListenerを使用する必要があります感を持って、この記事を見てみましょう... Java Swing. Opening a new JPanel from a JButton and making the buttons pretty 私はこれを左のでしょうこのリンクは、より役に立つかもしれませんコメントどうやらあなたはそのための50担当者を必要とする...

.. How to open a new window by clicking a button

0

clients変数がヌルに等しいです。

JButton btnClients = new JButton("Clients"); 
btnClients.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     home.setVisible(false); 
     clients.setVisible(true); 
    } 
}); 

書き込み、この:あなたがアクションリスナを追加する前に

JPanel clients = new JPanel(); 
contentPane.add(clients, "name_714431450350356"); 
clients.setLayout(null); 
JButton btnHome = new JButton("Home"); 
btnHome.setBounds(169, 107, 89, 23); 
clients.add(btnHome); 

関連する問題