Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Last active February 1, 2016 12:53
Show Gist options
  • Save AnnaBoro/0f74abdf91fd4524c45f to your computer and use it in GitHub Desktop.
Save AnnaBoro/0f74abdf91fd4524c45f to your computer and use it in GitHub Desktop.
+chooseTank
package lesson6_9.adapter.tank6;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ActionFieldUI implements ActionListener {
private JFrame frameDialog;
private JDialog dialog;
private ButtonGroup buttonGroup;
private JRadioButton button1;
private JRadioButton button2;
private JRadioButton button3;
private JButton okButton;
public ActionFieldUI() {
}
public void chooseTankDialog() {
// frameDialog = new JFrame();
// frameDialog.setMinimumSize(new Dimension(500, 500));
// frameDialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dialog = new JDialog(frameDialog);
dialog.setSize(new Dimension(400, 400));
GridBagLayout gbl = new GridBagLayout();
dialog.setLayout(gbl);
GridBagConstraints c = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
JLabel label = new JLabel("What tank would you like to play?");
gbl.setConstraints(label, c);
buttonGroup = new ButtonGroup();
button1 = new JRadioButton("T34", true);
button2 = new JRadioButton("Tiger");
button3 = new JRadioButton("BT7");
button1.setActionCommand(button1.getText());
button2.setActionCommand(button2.getText());
button3.setActionCommand(button3.getText());
buttonGroup.add(button1);
buttonGroup.add(button2);
buttonGroup.add(button3);
c = new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
gbl.setConstraints(button1, c);
c = new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
gbl.setConstraints(button2, c);
c = new GridBagConstraints(0, 3, 1, 1, 0, 0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
gbl.setConstraints(button3, c);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
okButton = new JButton("Ok");
c = new GridBagConstraints(0, 4, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
gbl.setConstraints(okButton, c);
dialog.getContentPane().add(label);
dialog.getContentPane().add(button1);
dialog.getContentPane().add(button2);
dialog.getContentPane().add(button3);
dialog.getContentPane().add(okButton);
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
});
dialog.setModal(true);
dialog.setVisible(true);
frameDialog.setVisible(true);
}
public static void main(String[] args) {
new ActionFieldUI().chooseTankDialog();
}
@Override
public void actionPerformed(ActionEvent e) {
if (buttonGroup.getSelection() != null) {
String str = buttonGroup.getSelection().getActionCommand();
System.out.println(str);
}
}
public void setFrameDialog(JFrame frameDialog) {
this.frameDialog = frameDialog;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment