在Java中向JFrame添加两个面板
所以我要做的是在一个JFrame里面有两个面板(必须是面板),一个是特定尺寸的,另一个是较小尺寸的,让较小尺寸的涂上特定颜色在Java中向JFrame添加两个面板,java,swing,jframe,Java,Swing,Jframe,所以我要做的是在一个JFrame里面有两个面板(必须是面板),一个是特定尺寸的,另一个是较小尺寸的,让较小尺寸的涂上特定颜色 public class Binary{ private JLabel header; private JTextField userInput1; private JButton doIt; private JButton clear; private JRadioButton binary, decimal; private JLabel number2; priv
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的框架中?我认为最好有
3个JPanel
:一个名为contentPane
的JPanel,并将其用作frame.setContentPane(contentPane)
(让它使用边框布局
,并允许它拥有另外两个框架。