[英]How can I organize my radio buttons on my JFrame?
在下面的代碼中,我無法將界面組織得看起來像所附的照片。 我已經嘗試了網格和流布局,但無法解決該問題。
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();
}
}
在不為您做全部事情的情況下,您可以通過以下方法實現單選按鈕面板之一:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.