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);
}
}
出於某種原因,我在這裏的代碼基本上輸出在我的第一個面板頂部的微不足道的面板。 有什麼我失蹤了嗎?
您如何期望能夠容納高度爲475的面板,高度爲500的面板和一堆控制器,所有這些都在高度爲500,寬度爲250的框架中? – RealSkeptic 2015-04-05 19:33:54
我認爲最好有'3 JPanels':一個名爲'contentPane'的JPanel,並將其用作'frame.setContentPane(contentPane)'(使用BorderLayout',並允許它有兩個其他的框架。 – CoderMusgrove 2015-04-05 21:24:29