减少java中标签和单选组之间的空间

发布于 2024-12-23 09:49:18 字数 5778 浏览 1 评论 0原文

我想减少标签和单选按钮组之间的间距。您可以更正标签和单选按钮组的格式吗?我得到了不必要的间距 我还想根据访问数据库中的数据更改单选按钮的标签。 请帮助

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)

I want to reduce the spacing between the labels and radio button group. Can you correct the formatting of labels and radio button group. I'm getting unnecessary spacing
Also I want to change the label of radio button as per the data from access database.
Please help

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);
        }
    }
}

i'm getting error as

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安人多梦 2024-12-30 09:49:18

组件的 setBorder(new EmptyBorder(0,0,0,0)) 怎么样?

What about setBorder(new EmptyBorder(0,0,0,0)) to the components?

夏九 2024-12-30 09:49:18

我认为您正在寻找 JLabel#setLabelFor(Component),那么你不关心关于减少JComponents之间的差距的问题

I think that you looking for JLabel#setLabelFor(Component), then you doesn's care about your issue about reduce gap betweens JComponents

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文