2

ラベルとラジオボタングループの間隔を狭くしたい。ラベルとラジオ ボタン グループの書式を修正できますか。不要なスペーシングが発生しています また、アクセスデータベースからのデータに従って、ラジオボタンのラベルを変更したいと考えています。助けてください

import javax.swing.*;
import java.awt.*;
import javax.swing.border.TitledBorder;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.Box;
import javax.swing.JApplet;
import java.awt.Container;
import java.sql.*;

public class CreateRadioButton11 extends JApplet {

    JFrame jtfMainFrame;
    JButton getAccountButton, lastButton, firstButton, gotoButton, previousButton, nextButton;
    JTextField jtfInput;
    static JRadioButton[] choice = new JRadioButton[5];

    public CreateRadioButton11() {

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

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        JPanel panel = new JPanel();
        nextButton = new JButton(">");
        previousButton = new JButton("<");
        lastButton = new JButton(">|");
        firstButton = new JButton("|<");
        gotoButton = new JButton("Goto");
        jtfInput = new JTextField(20);
        gotoButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                jtfInput.setText("Button 1!");
            }
        });
        getAccountButton = new JButton("Finish");
        panel.add(jtfInput);
        panel.add(getAccountButton);
        panel.add(previousButton);
        panel.add(nextButton);
        panel.add(lastButton);
        panel.add(firstButton);
        panel.add(gotoButton);

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


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

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

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

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

        choice[0] = new JRadioButton("a");
        choice[0].setBackground(Color.red);
        choice[1] = new JRadioButton("b");
        choice[1].setBackground(Color.red);
        choice[2] = new JRadioButton("c");
        choice[2].setBackground(Color.red);
        choice[3] = new JRadioButton("d");
        choice[3].setBackground(Color.red);
        choice[4] = new JRadioButton("e");
        choice[4].setBackground(Color.red);

        ButtonGroup bGroup = new ButtonGroup();
        for (int i = 0; i < 5; i++) {
            bGroup.add(choice[i]);
        }

        JPanel panEast = new JPanel(new BorderLayout(5, 5));
        jtfMainFrame.setContentPane(panEast);
        JPanel panlabels = new JPanel(new GridLayout(0, 1));
        JPanel pancontrols = new JPanel(new GridLayout(0, 1));
        panEast.add(panlabels, BorderLayout.WEST);
        panEast.add(pancontrols, BorderLayout.CENTER);
        panlabels.add(aLabel);
        pancontrols.add(choice[0]);
        panlabels.add(bLabel);
        pancontrols.add(choice[1]);
        panlabels.add(cLabel);
        pancontrols.add(choice[2]);
        panlabels.add(dLabel);
        pancontrols.add(choice[3]);
        panlabels.add(eLabel);
        pancontrols.add(choice[4]);
        panEast.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEtchedBorder(), "Select your answer"));
        panel.add("West", panEast);
        Container contentPane = jtfMainFrame.getContentPane();
        contentPane.add(panel, BorderLayout.CENTER);
        contentPane.add(panEast, BorderLayout.NORTH);
        jtfMainFrame.add(panel);
        jtfMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jtfMainFrame.setVisible(true);


    }

    public static void main(String[] args) {
        CreateRadioButton11 r = new CreateRadioButton11();
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            String dataSourceName = "access";
            String dbURL = "jdbc:odbc:" + dataSourceName;
            Connection con = DriverManager.getConnection(dbURL, "", "");

            Statement s = con.createStatement();
            s.execute("create table TEST12345 ( column_name integer )");
            s.execute("insert into TEST12345 values(1)");
            s.execute("select column_name from TEST12345");
            ResultSet rs = s.getResultSet();
            if (rs != null) {
                while (rs.next()) {
                    System.out.println("Data from column_name: " + rs.getString(1));
                }
            }
            choice[0].setText(rs.getString(1));
            s.execute("drop table TEST12345");
            s.close();
            con.close();
        } catch (Exception err) {
            System.out.println("ERROR: " + err);
        }
    }
}

私はエラーが発生しています

Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself
        at java.awt.Container.checkAddToSelf(Unknown Source)
        at java.awt.Container.addImpl(Unknown Source)
        at java.awt.Container.add(Unknown Source)
        at CreateRadioButton11.<init>(CreateRadioButton11.java:172)
        at CreateRadioButton11.main(CreateRadioButton11.java:190)
4

2 に答える 2

3

setBorder(new EmptyBorder(0,0,0,0))コンポーネントはどうですか?

于 2011-12-29T06:11:59.200 に答える
1

JLabel#setLabelFor(Component)を探していると思いますが、間のギャップを減らすことについての問題を気にしているJComponents

于 2011-12-29T07:22:43.763 に答える