在下面的代码中,我无法将界面整理成附图。我已经尝试过网格和流程布局,但我无法解决问题。
package app;
import javax.swing.*;
import java.awt.*;
public class app{
JFrame f1;
JButton buttton_1,buttton_2;
JLabel label_1,label_2;
JRadioButton radio_1,radio_2,radio_3,radio_4,radio_5,radio_6,radio_7;
app(){
JFrame f1 = new JFrame ("MathTest - Main Menu");
f1.setVisible(true);
f1.setSize(500,500);
f1.setLayout(new GridLayout(0,1));
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label_1 =new JLabel("select test type ");
radio_1=new JRadioButton("addition ");
radio_2=new JRadioButton("subtraction");
radio_3=new JRadioButton("mulitipcation");
radio_4=new JRadioButton("division");
label_2 =new JLabel("select a diffculty level");
radio_5=new JRadioButton("easy ");
radio_6=new JRadioButton("moderate");
radio_7=new JRadioButton("hard");
buttton_1=new JButton("start test");
buttton_2=new JButton("exit");
f1.add(label_1);
f1.add(radio_1);
f1.add(radio_2);
f1.add(radio_3);
f1.add(radio_4);
f1.add(label_2);
f1.add(radio_5);
f1.add(radio_6);
f1.add(radio_7);
f1.add(buttton_1);
f1.add(buttton_2);
}
public static void main(String[] args)
{
app xyz =new app();
}
}
答案 0 :(得分:1)
如果没有为您完成整个过程,您可以通过以下方式获得其中一个单选按钮面板:
JPanel typePanel = new JPanel(new GridLayout(0, 2));
typePanel.setBorder(BorderFactory.createLineBorder(Color.black, 1));
typePanel.add(new JLabel("Select a test type"));
typePanel.add(new JRadioButton("Addition"));
typePanel.add(new JLabel(""));
typePanel.add(new JRadioButton("Substraction"));
typePanel.add(new JLabel(""));
typePanel.add(new JRadioButton("Multiplication"));
typePanel.add(new JLabel(""));
typePanel.add(new JRadioButton("Division"));
对于底部的按钮,请记住可以设置流布局以使右侧的内容与构造函数对齐:
new FlowLayout(FlowLayout.RIGHT)