2015-04-05 52 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

    祝您好运!