1

在我的项目中,我使用 Swing 控件。我曾将标签与按钮组一起使用,但有一条不需要的线。请帮忙。每个单选按钮组都有一个标签。不需要的行在那里。如何将标签和相应的单选按钮组添加到同一面板

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.util.Arrays;

public class Online extends JFrame {

    static JRadioButton[] choice = new JRadioButton[6];
    JFrame jtfMainFrame, jtfMainFrame1;

    public void createWindow() {

        jtfMainFrame = new JFrame("Online Examination");
        jtfMainFrame.setSize(800, 500);
        jtfMainFrame.setLocation(200, 150);
        jtfMainFrame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        JPanel pa = new JPanel();

        JPanel panlabels = new JPanel(new GridLayout(0, 1, 0, 60));

        JPanel pancontrols = new JPanel(new GridLayout(0, 1, 0, 60));
        JPanel panEast = new JPanel();

        JPanel pan = new JPanel(new FlowLayout());
        JLabel qLabel = new JLabel("Question.");
        qLabel.setOpaque(true);
        qLabel.setForeground(Color.blue);
        qLabel.setBackground(Color.lightGray);

        JLabel aLabel = new JLabel("Question.");
        aLabel.setOpaque(true);
        aLabel.setForeground(Color.blue);
        aLabel.setBackground(Color.lightGray);

        JLabel bLabel = new JLabel("a.");
        bLabel.setOpaque(true);
        bLabel.setForeground(Color.blue);
        bLabel.setBackground(Color.lightGray);

        JLabel cLabel = new JLabel("b.");
        cLabel.setOpaque(true);
        cLabel.setForeground(Color.blue);
        cLabel.setBackground(Color.lightGray);

        JLabel dLabel = new JLabel("c.");
        dLabel.setOpaque(true);
        dLabel.setForeground(Color.blue);
        dLabel.setBackground(Color.lightGray);

        JLabel eLabel = new JLabel("d.");
        eLabel.setOpaque(true);
        eLabel.setForeground(Color.blue);
        eLabel.setBackground(Color.lightGray);

        panlabels.add(aLabel, BorderLayout.WEST);
        panlabels.add(bLabel, BorderLayout.CENTER);
        panlabels.add(cLabel, BorderLayout.CENTER);
        panlabels.add(dLabel, BorderLayout.CENTER);
        panlabels.add(eLabel, BorderLayout.CENTER);
        //panlabels.add(fLabel, BorderLayout.WEST);
        //fLabel.setVisible(false);

        JLabel ques = new JLabel("q");
        ques.setBackground(Color.red);

        choice[1] = new JRadioButton("a");
        choice[1].setBackground(Color.red);

        choice[2] = new JRadioButton("b");
        choice[2].setBackground(Color.red);

        choice[3] = new JRadioButton("c");
        choice[3].setBackground(Color.red);

        choice[4] = new JRadioButton("d");
        choice[4].setBackground(Color.red);

        ButtonGroup bGroup = new ButtonGroup();
        pancontrols.add(ques, BorderLayout.WEST);
        for (int i = 1; i < 5; i++) {
            // pancontrols.add(aLabel,BorderLayout.WEST);
            bGroup.add(choice[i]);

            pancontrols.add(choice[i], BorderLayout.WEST);
        }
        choice[4].setVisible(true);
         panEast.add("West", panlabels);
        panEast.add("West", pancontrols);
        pa.add("Center", panEast);
        pa.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(), "Select your answer"));

        //getContentPane().add(label);
        //to be deleted pa.add("South", pan);
        pa.setBackground(Color.pink);
        jtfMainFrame.add(pa);
        jtfMainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);

        jtfMainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        Online r = new Online();
        r.createWindow();
    }
}
4

1 回答 1

1

首先,您对选项标签的命名约定已关闭。qLabel="questions"、aLabel="questions"、bLabel="a"、cLabel="b" 等。我建议您修复它以消除混淆并使您的代码更具可读性(即只有一个标签是问题)。

panEast.add("West",panlabels);其次,一般不建议您使用and 其他两个语句。继续阅读BorderLayout以找到更可接受的方法:http: //docs.oracle.com/javase/tutorial/uiswing/layout/border.html

至于您的问题,我已经重写了您的代码,所以事情确实排成一列,我将尝试指出我注释掉的内容以提供帮助:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.util.Arrays;

public class Online extends JFrame {

    static JRadioButton[] choice = new JRadioButton[6];
    JFrame jtfMainFrame, jtfMainFrame1;

    public void createWindow() {

        jtfMainFrame = new JFrame("Online Examination");
    jtfMainFrame.setSize(800, 500);
    jtfMainFrame.setLocation(200, 150);
    jtfMainFrame.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    JPanel pa = new JPanel();

    //JPanel panlabels = new JPanel(new GridLayout(0, 1, 0, 60));

    JPanel pancontrols = new JPanel(new GridLayout(0, 2, 0, 60));
    JPanel panEast = new JPanel();

    JPanel pan = new JPanel(new BorderLayout());
    JLabel qLabel = new JLabel("Question.");
    qLabel.setOpaque(true);
    qLabel.setForeground(Color.blue);
    qLabel.setBackground(Color.lightGray);

    JLabel aLabel = new JLabel("Question.");
    aLabel.setOpaque(true);
    aLabel.setForeground(Color.blue);
    aLabel.setBackground(Color.lightGray);

    JLabel bLabel = new JLabel("a.");
    bLabel.setOpaque(true);
    bLabel.setForeground(Color.blue);
    bLabel.setBackground(Color.lightGray);

    JLabel cLabel = new JLabel("b.");
    cLabel.setOpaque(true);
    cLabel.setForeground(Color.blue);
    cLabel.setBackground(Color.lightGray);

    JLabel dLabel = new JLabel("c.");
    dLabel.setOpaque(true);
    dLabel.setForeground(Color.blue);
    dLabel.setBackground(Color.lightGray);

    JLabel eLabel = new JLabel("d.");
    eLabel.setOpaque(true);
    eLabel.setForeground(Color.blue);
    eLabel.setBackground(Color.lightGray);


    //panlabels.add(fLabel, BorderLayout.WEST);
    //fLabel.setVisible(false);

    JLabel ques = new JLabel("q");
    ques.setBackground(Color.red);

    choice[1] = new JRadioButton("a");
    choice[1].setBackground(Color.red);

    choice[2] = new JRadioButton("b");
    choice[2].setBackground(Color.red);

    choice[3] = new JRadioButton("c");
    choice[3].setBackground(Color.red);

    choice[4] = new JRadioButton("d");
    choice[4].setBackground(Color.red);

    ButtonGroup bGroup = new ButtonGroup();
    //pancontrols.add(new JLabel(""));
    pancontrols.add(ques, BorderLayout.WEST);
    for (int i = 1; i < 5; i++) {
        // pancontrols.add(aLabel,BorderLayout.WEST);
        bGroup.add(choice[i]);

    }

    pancontrols.add(qLabel);
    pancontrols.add(ques);

    pancontrols.add(bLabel);
    pancontrols.add(choice[1]);
    pancontrols.add(cLabel);
    pancontrols.add(choice[2]);
    pancontrols.add(dLabel);
    pancontrols.add(choice[3]);
    pancontrols.add(eLabel);
    pancontrols.add(choice[4]);
    pancontrols.setSize(400,200);
    choice[4].setVisible(true);
    pa.add(pancontrols);
    pa.setBorder(BorderFactory.createTitledBorder(
        BorderFactory.createEtchedBorder(), "Select your answer"));

    //getContentPane().add(label);
    //to be deleted pa.add("South", pan);
        pa.setBackground(Color.pink);
        jtfMainFrame.add(pa);
        jtfMainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);

        jtfMainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        Online r = new Online();
        r.createWindow();
    }
}

基本上,我删除了你不必要的面板。您现在添加的唯一面板是 pancontrols 面板。我把它改成了new JPanel(new Gridlayout(0,2,0,60)). 使用两列,由于使所有内容都具有相同的大小GridLayout,因此您将始终将事物排成一列。GridLayout

最后,我choices[]从循环中提取了添加内容,并与标签一起执行以确保内容对齐。除了添加到的 pancontrols 之外,我删除了所有添加的面板pa,我假设您想在这种情况下向 pa 添加更多问题面板。这尤其涵盖了您的问题,但是您应该做很多事情(例如,使用选择[0],消除未使用的标签和面板等)

于 2012-04-14T14:05:33.857 回答