簡體   English   中英

(Java新手 - 面板轉換)如何在框架中的面板之間切換

[英](Java Newbie - Panel Transitions) How do I switch between panels in a frame

我有以下簡單的代碼,我不知道如何修改它,以便有3個單獨的面板切換到,每個按鈕一個:

package TouristLocations;

import javax.swing.*;

import java.awt.*;

public class buildApp extends JFrame {
  /**
     * 
     */
    private static final long serialVersionUID = 1L;

public static void main(String[] args){
    JFrame frame = new JFrame("Test");
    frame.setSize(400,500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    JLabel title = new JLabel("Locations");
    title.setFont(new Font("Serif", Font.BOLD, 40));
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 3;
    frame.add(title, c);

    JButton b1 = new JButton("View Locations");
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    frame.add(b1, c);

    JButton b2 = new JButton("Insert Locations");
    c.gridx = 1;
    c.gridy = 1;
    frame.add(b2, c);

    JButton b3 = new JButton("Help");
    c.gridx = 2;
    c.gridy = 1;
    frame.add(b3, c);

    TextArea text1 = new TextArea(15,40);
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 3;
    frame.add(text1, c);

    frame.pack();


  }
}

謝謝

聽起來你應該考慮使用JTabbedPane

除了如何使用選項卡式窗格外 ,您可能還需要查看此處此處提到的CardLayout

你應該創建一個主容器:

JPanel mainContainer = new JPanel();

//creation of each child
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();

...所以每個按鈕必須將這些面板添加到主容器中並調整新的面板大小,如下所示:

//for button1:
mainContainer.add(panel1);
panel1.setSize(mainContainer.getSize());

...對於button2操作,您必須按照上述相同的方式操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM