Skip to content

Instantly share code, notes, and snippets.

Created April 12, 2019 10:20
Show Gist options
  • Save ssaurel/42754b38584f266d1b2eda1f289b316f to your computer and use it in GitHub Desktop.
Save ssaurel/42754b38584f266d1b2eda1f289b316f to your computer and use it in GitHub Desktop.
Build UI method for the Twenty Questions game on SSaurel's Channel
private void buildUI() {
// we build the UI
JFrame frame = new JFrame("Twenty Questions on SSaurel");
Container contentPane = frame.getContentPane();
// add buttons
JPanel buttonsPanel = new JPanel();
yesButton = new JButton("Yes");
buttonsPanel.add(yesButton, BorderLayout.LINE_START);
restartButton = new JButton("Start");
buttonsPanel.add(restartButton, BorderLayout.LINE_START);
noButton = new JButton("No");
buttonsPanel.add(noButton, BorderLayout.LINE_END);
contentPane.add(buttonsPanel, BorderLayout.PAGE_END);
// add text area
textPane = new JTextPane();
updateText("Think to an animal, then click on Start");
// we define some style for the text pane
SimpleAttributeSet bSet = new SimpleAttributeSet();
StyleConstants.setAlignment(bSet, StyleConstants.ALIGN_CENTER);
StyleConstants.setFontSize(bSet, 22);
StyledDocument doc = textPane.getStyledDocument();
doc.setParagraphAttributes(0, doc.getLength(), bSet, false);
contentPane.add(textPane, BorderLayout.CENTER);
frame.setMinimumSize(new Dimension(500, 250));
// we center the JFrame
Dimension objDimension = Toolkit.getDefaultToolkit().getScreenSize();
int coordX = (objDimension.width - frame.getWidth()) / 2;
int coordY = (objDimension.height - frame.getHeight()) / 2;
frame.setLocation(coordX, coordY);
// we display the window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment