2011-12-29 97 views
2

我想减少标签和单选按钮组之间的间距。你能纠正标签和单选按钮组的格式吗?我得到不必要的间距 另外我想改变单选按钮的标签,根据来自访问数据库的数据。 请帮助减少java中标签和广播组之间的空间

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) 

回答

3

什么setBorder(new EmptyBorder(0,0,0,0))的组成部分?

+0

ü可以指定把这个? – 2011-12-29 06:26:49

+0

添加到您需要更近的组件。例如。到JLabels – StanislavL 2011-12-29 06:36:43

+0

我想缩小label1 radiobutton1 – 2011-12-29 06:43:39

1

我认为你寻找JLabel#setLabelFor(Component),那么你没有按的关心你的问题,约减少差距中间人JComponents

+0

i '一个java beginner.please指定语句 – 2011-12-29 07:27:01

+0

aLabel.setLabelFor(choice [0]); – mKorbel 2011-12-29 07:31:23

+0

嘿我想将红色部分移动到左侧 – 2011-12-29 07:38:01