In my code below, I couldn't organize my interface to look like the attached photo. I've tried grid and flow layouts, and I have been unable to solve the problem.
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();
}
}
Without doing the whole thing for you, here is how you can achive one of those radio button panels:
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"));
For the buttons at the bottom, remember that a flow layout can be set to align things on the right side with the constructor:
new FlowLayout(FlowLayout.RIGHT)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.