2015-04-05 93 views
1

所以我想要做的是在JFrame裏面有2個面板(它必須是面板),並且有1個特定的尺寸,另一個尺寸更小並且尺寸更小中等大小的人畫了一定的顏色。將兩個面板添加到Java中的JFrame中

public class Binary{ 

private JLabel header; 
private JTextField userInput1; 
private JButton doIt; 
private JButton clear; 
private JRadioButton binary, decimal; 
private JLabel number2; 
private JFrame frame1; 
private JPanel panel1; 
private JPanel panel2; 

public Binary(){ 

    frame1 = new JFrame("Number Converter"); // frame 
    frame1.setLayout(new FlowLayout()); 
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    panel1 = new JPanel(); // first panel (light grey) 
    panel1.setSize(250, 475); 
    frame1.add(panel1); 

    header = new JLabel("1- Select the mode: "); 
    panel1.add(header); 

    ButtonGroup choices= new ButtonGroup(); 
    binary = new JRadioButton("Binary to Decimal"); // add the first radiobutton binary to decimal 
    choices.add(binary); 
    decimal = new JRadioButton("Decimal to Binary"); // add the second radiobutton decimal to binary 
    choices.add(decimal); 
    frame1.add(binary); // adds both to the program 
    frame1.add(decimal); 

    userInput1 = new JTextField(20); // Adds a blank text field for user input 
    frame1.add(userInput1); 

    number2 = new JLabel("2- Enter some words then click Do It:"); 
    frame1.add(number2); 

    panel2 = new JPanel(); // second panel, bottom dark grey 
    panel2.setOpaque(true); 
    panel2.setBackground(Color.GRAY); 
    panel2.setSize(500, 500); 
    frame1.add(panel2); 

    doIt = new JButton("Do It"); // left button do it 
    frame1.add(doIt); 

    clear = new JButton("Clear"); // right button clear 
    frame1.add(clear); 

    frame1.setSize(250, 500); 
    frame1.setVisible(true); 
} 

}

出於某種原因,我在這裏的代碼基本上輸出在我的第一個面板頂部的微不足道的面板。 有什麼我失蹤了嗎?

+0

您如何期望能夠容納高度爲475的面板,高度爲500的面板和一堆控制器,所有這些都在高度爲500,寬度爲250的框架中? – RealSkeptic 2015-04-05 19:33:54

+0

我認爲最好有'3 JPanels':一個名爲'contentPane'的JPanel,並將其用作'frame.setContentPane(contentPane)'(使用BorderLayout',並允許它有兩個其他的框架。 – CoderMusgrove 2015-04-05 21:24:29

回答

2

我找到2個可能的答案給你的問題。

  1. 您可以將兩個按鈕(執行它&清除)添加到panel2。這最終會是這樣的:

1st solution

  • 您可以添加一個空白的JLabel到PANEL2。

    size = new JLabel(「//你可以在這裏添加儘可能多的空間,你擁有的越多,它將會越大。

    panel2.add(size);

    //如果您想讓它更大一些,只需製作更多JLabel並將它們添加到panel2即可。

  • 這一項的結果將是這樣的:

    2nd solution

    祝您好運!