Java UI, trying to go to next page upon clicking button
I have a simple GUI with 3 radio buttons and a button. I want the user to select a radio option, then upon clicking the button the user will be directed to a different Java UI depending upon which radio button they selected. Here is my method:
private void btnContinueActionPerformed(java.awt.event.ActionEvent evt)
{
if(rbCSV.isSelect开发者_高级运维ed())
{
}
else if(rbExcel.isSelected())
{
}
else if(rbDatabase.isSelected())
{
}
else
{
}
}
I am new to Java Swing, and not sure of the syntax that will allow me to direct the user to the next page. Sort of like a Wizard, I guess. Any help would be awesome! Thanks!
Recommendations:
- Read the How to Use Radio Buttons tutorial
- Read the How to Use CardLayout tutorial
- Learn how to use a
ButtonGroup
, since you'll only want one button to be selected at a time
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;
}
}
}
JPanel panelCSV, panelExcel, panelDatabase;
CardLayout cardLayout = new CardLayout();
JPanel pagePanel = new JPanel(cardLayout);
pagePanel.add(panelCSV, "CSV");
pagePanel.add(panelExcel, "Excel");
pagePanel.add(panelDatabase, "Database");
public void btnContinueActionPerformed(ActionEvent e) {
if ( rbCSV.isSelected() ) {
cardLayout.show(pagePanel, "CSV");
} else if ( rbExcel.isSelected() ) {
cardLayout.show(pagePanel, "Excel");
} else if ( rbDatabase.isSelected() ) {
cardLayout.show(pagePanel, "Database");
} else {
// ....
}
}
精彩评论