Java UI,单击按钮后尝试转到下一页

Bra*_*man 3 java user-interface swing

我有一个简单的GUI,有3个单选按钮和一个按钮.我希望用户选择一个单选按钮,然后单击该按钮,用户将被定向到另一个Java UI,具体取决于他们选择的单选按钮.这是我的方法:

 private void btnContinueActionPerformed(java.awt.event.ActionEvent evt)
 {
      if(rbCSV.isSelected())
      {

      }
      else if(rbExcel.isSelected())
      {

      }
      else if(rbDatabase.isSelected())
      {

      }
      else
      {

      }
 }
Run Code Online (Sandbox Code Playgroud)

我是Java Swing的新手,并且不确定允许我将用户引导到下一页的语法.我想,有点像巫师.任何帮助都是极好的!谢谢!

mre*_*mre 5

建议:

  1. 阅读如何使用单选按钮教程
  2. 阅读如何使用CardLayout教程
  3. 了解如何使用a ButtonGroup,因为您一次只能选择一个按钮

public final class RadioButtonDemo {
    private static CardPanel cards;
    private static Card cardOne;
    private static Card cardTwo;
    private static Card cardThree;

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame("RB Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(
                new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
        cards = new CardPanel();
        frame.getContentPane().add(cards);
        frame.getContentPane().add(new ControlPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static void createCards(){
        cardOne = new Card(
                "Card 1", 
                new JLabel("This is card one"), 
                Color.PINK);
        cardTwo = new Card(
                "Card 2", 
                new JLabel("This is card two"), 
                Color.YELLOW);
        cardThree = new Card(
                "Card 3", 
                new JLabel("This is card three"), 
                Color.CYAN);
    }

    private static final class Card extends JPanel{
        private final String name;

        public Card(
                final String name, 
                final JComponent component, 
                final Color c){
            super();
            this.name = name;
            setBorder(BorderFactory.createLineBorder(Color.BLACK));
            setBackground(c);
            add(component);
        }

        public final String getName(){
            return name;
        }
    }

    private static final class CardPanel extends JPanel{
        public CardPanel(){
            super(new CardLayout());
            createCards();
            add(cardOne, cardOne.getName());
            add(cardTwo, cardTwo.getName());
            add(cardThree, cardThree.getName());
        }
    }

    private static final class ControlPanel extends JPanel{
        private static JRadioButton showCardOneButton;
        private static JRadioButton showCardTwoButton;
        private static JRadioButton showCardThreeButton;
        private static JButton showButton;

        public ControlPanel(){
            super();
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            add(createJRadioButtonPanel());
            add(createJButtonPanel());
        }

        private final JPanel createJRadioButtonPanel(){
            final JPanel panel = new JPanel();

            showCardOneButton = new JRadioButton(cardOne.getName());
            showCardOneButton.setSelected(true);
            showCardTwoButton = new JRadioButton(cardTwo.getName());
            showCardThreeButton = new JRadioButton(cardThree.getName());
            ButtonGroup group = new ButtonGroup();
            group.add(showCardOneButton);
            group.add(showCardTwoButton);
            group.add(showCardThreeButton);
            panel.add(showCardOneButton);
            panel.add(showCardTwoButton);
            panel.add(showCardThreeButton);

            return panel;
        }

        private final JPanel createJButtonPanel(){
            final JPanel panel = new JPanel();

            showButton = new JButton("Show");
            showButton.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    CardLayout cl = (CardLayout) cards.getLayout();
                    if(showCardOneButton.isSelected()){
                        cl.show(cards, showCardOneButton.getText());
                    }
                    else if(showCardTwoButton.isSelected()){
                        cl.show(cards, showCardTwoButton.getText());
                    }
                    else if(showCardThreeButton.isSelected()){
                        cl.show(cards, showCardThreeButton.getText());
                    }
                }
            });
            panel.add(showButton);

            return panel;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述