在 Java 中将两个面板添加到 JFrame
Adding two panels to a JFrame in Java
所以我想要做的是在 JFrame 内部有 2 个面板(它必须是面板)并且有 1 个特定尺寸,另一个尺寸较小并且尺寸较小的一个涂有特定颜色.
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);
}
}
出于某种原因,我这里的代码基本上在我的第一个面板之上输出了一个小面板。
有什么我想念的吗?
针对您的问题,我找到了 2 个可能的答案。
- 您可以将两个按钮(执行并清除)添加到面板 2。最终会像这样:
您可以将空白的 JLabel 添加到面板 2。
size = new JLabel("//这里可以加多少空格,越多横向越大");
panel2.add(尺码);
//如果你想让它的垂直度更大,就多做几个JLabels,然后把它们添加到panel2。
这一个的结果将是这样的:
祝你好运!
所以我想要做的是在 JFrame 内部有 2 个面板(它必须是面板)并且有 1 个特定尺寸,另一个尺寸较小并且尺寸较小的一个涂有特定颜色.
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);
}
}
出于某种原因,我这里的代码基本上在我的第一个面板之上输出了一个小面板。 有什么我想念的吗?
针对您的问题,我找到了 2 个可能的答案。
- 您可以将两个按钮(执行并清除)添加到面板 2。最终会像这样:
您可以将空白的 JLabel 添加到面板 2。
size = new JLabel("//这里可以加多少空格,越多横向越大");
panel2.add(尺码);
//如果你想让它的垂直度更大,就多做几个JLabels,然后把它们添加到panel2。
这一个的结果将是这样的:
祝你好运!