如何滚动此单选按钮面板

时间:2016-07-01 22:22:34

标签: java swing jscrollpane layout-manager null-layout-manager

我是新手。有一天我可能会使用布局管理器,但请告诉我如何向我的Frame添加垂直滚动条。我意识到,我采取了非正统的方法,但我正在学习。阅读API后,我感到非常难过。我如何将垂直滚动条添加到以下JFrame

enter image description here

我的代码段如下:

import javax.swing.JScrollPane;
import java.awt.event.*;
import javax.swing.*;  
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
//import java.util.Arrays;

public class NewGameGUI implements ActionListener
{ 
  private final JRadioButton[] rb = new JRadioButton[20];
    JFrame f3;  
    JButton b;
    JLabel label1; 
    public NewGameGUI()
    {  
        label1 = new JLabel("Please choose a team");
        f3 = new JFrame("Ballon d'or");  
        f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        rb[0] = new JRadioButton("Arsenal");  
        rb[1] = new JRadioButton("Aston Villa");
        rb[2] = new JRadioButton("Bournemouth");
        rb[3] = new JRadioButton("Chelsea");
        rb[4] = new JRadioButton("Crystal Palace");
        rb[5] = new JRadioButton("Everton");
        rb[6] = new JRadioButton("Leicester City");
        rb[7] = new JRadioButton("Liverpool");
        rb[8] = new JRadioButton("Manchester United");
        rb[9] = new JRadioButton("Manchester City");
        rb[10] = new JRadioButton("Newcastle United");
        rb[11] = new JRadioButton("Norwich City");
        rb[12] = new JRadioButton("Southampton");
        rb[13] = new JRadioButton("Stoke City");
        rb[14] = new JRadioButton("Sunderland");
        rb[15] = new JRadioButton("Swansea City");
        rb[16] = new JRadioButton("Tottenham Hotspur");
        rb[17] = new JRadioButton("Watford");
        rb[18] = new JRadioButton("West Brom");
        rb[19] = new JRadioButton("West Ham");

        label1.setBounds(50,25,200,30);
        f3.add(label1);

        rb[0].setBounds(50,75,170,30);  
        rb[1].setBounds(50,100,170,30);  
        rb[2].setBounds(50,125,170,30);
        rb[3].setBounds(50,150,170,30);
        rb[4].setBounds(50,175,170,30);
        rb[5].setBounds(50,200,170,30);
        rb[6].setBounds(50,225,170,30);
        rb[7].setBounds(50,250,170,30);
        rb[8].setBounds(50,275,170,30);
        rb[9].setBounds(50,300,170,30);
        rb[10].setBounds(50,325,170,30);
        rb[11].setBounds(50,350,170,30);
        rb[12].setBounds(50,375,170,30);
        rb[13].setBounds(50,400,170,30);
        rb[14].setBounds(50,425,170,30);
        rb[15].setBounds(50,450,170,30);
        rb[16].setBounds(50,475,170,30);
        rb[17].setBounds(50,500,170,30);
        rb[18].setBounds(50,525,170,30);
        rb[19].setBounds(50,550,170,30);

       // JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

       // f3.setContentPane(pane);

        ButtonGroup bg=new ButtonGroup();  
        bg.add(rb[0]);
        bg.add(rb[1]);
        bg.add(rb[2]);  
        bg.add(rb[3]); 
        bg.add(rb[4]); 
        bg.add(rb[5]); 
        bg.add(rb[6]); 
        bg.add(rb[7]); 
        bg.add(rb[8]); 
        bg.add(rb[9]); 
        bg.add(rb[10]); 
        bg.add(rb[11]); 
        bg.add(rb[12]); 
        bg.add(rb[13]); 
        bg.add(rb[14]);
        bg.add(rb[15]);
        bg.add(rb[16]);
        bg.add(rb[17]);
        bg.add(rb[18]);
        bg.add(rb[19]);

        b = new JButton("OK");
        b.setBounds(300,300,180,30);  
        b.addActionListener(this);
        f3.add(rb[0]);
        f3.add(rb[1]);
        f3.add(rb[2]);
        f3.add(rb[3]);
        f3.add(rb[4]);
        f3.add(rb[5]);
        f3.add(rb[6]);
        f3.add(rb[7]);
        f3.add(rb[8]);
        f3.add(rb[9]);
        f3.add(rb[10]);
        f3.add(rb[11]);
        f3.add(rb[12]);
        f3.add(rb[13]);
        f3.add(rb[14]);
        f3.add(rb[15]);
        f3.add(rb[16]);
        f3.add(rb[17]);
        f3.add(rb[18]);
        f3.add(rb[19]);
        f3.add(b);

        f3.setSize(700,700);  

        f3.setLayout(null);  
        f3.setVisible(true);  
    }

    public void setFrame(JFrame f3){
        this.f3 = f3;
    }
    public JFrame getFrame(){

    return f3;
    }
            public void actionPerformed(ActionEvent e)
            {  
                if(rb[0].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[0].getText());  
                }  
                if(rb[1].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[1].getText()); 
                } 
                if(rb[2].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[2].getText()); 
                }  
                if(rb[3].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[3].getText()); 
                }
                if(rb[4].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[4].getText()); 
                }
                if(rb[5].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[5].getText()); 
                }
                if(rb[6].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[6].getText()); 
                }
                if(rb[7].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[7].getText()); 
                }
                if(rb[8].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[8].getText()); 
                }
                if(rb[9].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[9].getText()); 
                }
                if(rb[10].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[10].getText()); 
                }
                if(rb[11].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[11].getText()); 
                }
                if(rb[12].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[12].getText()); 
                }
                if(rb[13].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[2].getText()); 
                }
                if(rb[14].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[14].getText()); 
                }
                if(rb[15].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[15].getText()); 
                }
                if(rb[16].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[16].getText()); 
                }
                if(rb[17].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[17].getText()); 
                }
                if(rb[18].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[18].getText()); 
                }
                if(rb[19].isSelected())
                {
                   f3.dispose();
                   JOptionPane.showMessageDialog(f3,"You chose : " + rb[19].getText()); 
                }  
            }
}

2 个答案:

答案 0 :(得分:3)

  1. @Andrew Thompson使用nested layoutssuggested。在下面的示例中,

    • mainPanel使用GridLayout(1, 0)为团队面板和确定按钮添加一行和任意数量的列。

    • teamPanel使用GridLayout(0, 1),其中任意行数和一列用于团队单选按钮。

  2. 使用实现ListIterable来使用for-each简化代码。在下面的示例中,

    • List<String>代表团队names

    • 名为List<JRadioButton>的{​​{1}}为每个团队保留list

    • 循环JRadioButton构建names个实例,并将其添加到JRadioButtonlistButtonGroup

      < / LI>
    • OK 按钮处理程序中的teamPanel循环标识所选的list

  3. imagee

    JRadioButton

答案 1 :(得分:2)

这个GU通过组合布局来创建是微不足道的(虽然它也可以使用单个GridBagLayoutGroupLayout来完成。

对于组合布局,请将无线电按钮放入单列网格布局中。将该面板放在边框布局中心的滚动窗格中,该布局在页面起始约束中具有标签。将该面板(带有边框布局)放在另一个边框布局的行开始位置。将按钮放在外边框布局中心的网格包布局中。完成。

这是我对该策略的实施:

构建时提供给布局管理器的数字,以及按钮周围的空边框。按钮内部使用的边距都可以调整,以提供更多/更少的空白区域。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class NewGameGUIUsingLayouts {

    private JComponent ui = null;
    public static final String[] TEAM_NAMES = {
        "Arsenal", "Aston Villa", "Bournemouth", "Chelsea",  "Crystal Palace", 
        "Everton", "Leicester City", "Liverpool", "Manchester United", 
        "Manchester City", "Newcastle United",  "Norwich City", "Southampton",
        "Stoke City", "Sunderland", "Swansea City", "Tottenham Hotspur",
        "Watford", "West Brom", "West Ham"
    };

    NewGameGUIUsingLayouts() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(20, 20, 20, 20));

        JPanel teamSelectorPanel = new JPanel(new GridLayout(0, 1, 5, 10));
        ButtonGroup teamGroup = new ButtonGroup();
        for (String s : TEAM_NAMES) {
            JRadioButton radioButton = new JRadioButton(s);
            teamGroup.add(radioButton);
            teamSelectorPanel.add(radioButton);
        }
        JPanel lineStartPanel = new JPanel(new BorderLayout(20, 20));
        ui.add(lineStartPanel, BorderLayout.LINE_START);
        lineStartPanel.add(new JScrollPane(
                teamSelectorPanel, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), 
                BorderLayout.CENTER);
        lineStartPanel.add(
                new JLabel("Please choose a team"), BorderLayout.PAGE_START);
        JButton okButton = new JButton("OK");
        okButton.setMargin(new Insets(20, 50, 20, 50));
        okButton.setFont(okButton.getFont().deriveFont(50f));
        JPanel centerButtonPanel = new JPanel(new GridBagLayout());
        centerButtonPanel.setBorder(new EmptyBorder(100,100,100,100));
        centerButtonPanel.add(okButton);
        ui.add(centerButtonPanel, BorderLayout.CENTER);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                NewGameGUIUsingLayouts o = new NewGameGUIUsingLayouts();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                Dimension d = f.getSize();
                Dimension shortDimension = new Dimension(
                        (int)d.getWidth(), 
                        (int)d.getHeight()-200);
                f.setMinimumSize(shortDimension);
                f.setSize(shortDimension);

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}