簡體   English   中英

當每個面板處於Gridlayout中時,如何將兩個面板添加到單個JFrame中?

[英]How do I add two panels to a single JFrame when each panel is in Gridlayout?

我已經進行了大量的挖掘工作,但是似乎無法弄清我在做什么錯。 我正在創建一個框架,並嘗試向其中添加兩個網格面板。 每個面板均設置為1行3列的網格布局。

但這是我運行它時出現的內容: 截圖

我知道我缺少一些簡單的東西,但是我很難弄清楚它到底是什么。 很抱歉這個問題,但我現在很茫然。 任何幫助將不勝感激!

public class MadewellSalesTaxWindow  extends JFrame
{
private JFrame frame; // the frame
private JPanel panel1; //top panel
private JPanel panel2; //bottom panel
private JLabel messageLabel; // label right of field
private JTextField TaxTextField; // label for text field
private JButton calcCountyButton; // these are the button names
private JButton calcStateButton;
private JButton calcTotalButton;
private final int WINDOW_WIDTH = 500;  // window width
private final int WINDOW_HEIGHT = 150; // window height
private final double STATE_TAX = 0.065;
private final double COUNTY_TAX = 0.03;
String pattern = "###,###,###,###.##";
DecimalFormat decimalFormat = new DecimalFormat(pattern); // output formatting

public MadewellSalesTaxWindow()
{
    // window title
    setTitle("Sales Tax Calculator");

    // window size
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

    // clarify what occurs upon window closing
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // call frame constructor
    frame = new JFrame();

    // calls the function that builds the panels
    buildPanel();

    // adds the panels to the frame
    add(panel1);
    add(panel2);

    // displays the window (very important)
   setVisible(true);
}

private void buildPanel()
{
    // this label gives the user instructions
    messageLabel = new JLabel("Enter total sales for the month: $");

    // 10 characters should be enough for monthly sales unless the company is massive
    TaxTextField = new JTextField(10);

    // creates the buttons
    calcCountyButton = new JButton("Calculate County Tax");
    calcStateButton = new JButton("Calculate State Tax");
    calcTotalButton = new JButton("Calculate Total Sales Tax");


    // adds action listeners to each of the buttons
    calcCountyButton.addActionListener(new CountyButtonListener());
    calcStateButton.addActionListener(new StateButtonListener());
    calcTotalButton.addActionListener(new TotalButtonListener());

    // create 2 new panels
    panel1 = new JPanel();
    panel2 = new JPanel();

    // add the appropriate elements to the top panel and the bottom panel
    panel1.add(messageLabel);
    panel1.add(TaxTextField);
    panel2.add(calcCountyButton);
    panel2.add(calcStateButton);
    panel2.add(calcTotalButton);

    // set the layouts for the panels so that they display correctly
    panel1.setLayout(new GridLayout(1,3)); 
    panel2.setLayout(new GridLayout(1,3)); 
}

在一個JFrame中看到兩個面板?

總之,您的框架需要一個布局來容納兩個面板。

setLayout(new GridLayout(2,1));

到構造函數。

暫無
暫無

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

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