import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.print.attribute.AttributeSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.border.MatteBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class Example extends JFrame{
private static int windowWidth = 500;
private static int windowHeight = 500;
private int popUpXPlayer;
private int popUpYPlayer;
private JPanel popUpPanelPlayer;
private JTextField textFieldPlayer;
private Popup popUpAddPlayer;
public static void main(String[] args){
Example e = new Example();
e.setSize(windowWidth, windowHeight);
e.setDefaultCloseOperation(EXIT_ON_CLOSE);
e.makeMenuBar();
e.makePopUpSettingsAddPlayer();
e.setVisible(true);
}
public void makeMenuBar(){
int menuBarWidth = windowWidth;
int menuBarHeight = (windowHeight/100)*14;
int menuBarBorderLength = windowWidth/250;
int menuButtonWidth = 3*menuBarHeight/4;
int menuButtonHeight = menuButtonWidth;
int menuButtonWidthSpace = menuButtonWidth/4;
int menuButtonHeightSpace = (menuBarHeight-menuButtonHeight-2*menuBarBorderLength)/2;
JPanel menuField = new JPanel();
menuField.setBackground(Color.blue);
menuField.setPreferredSize(new Dimension(menuBarWidth, menuBarHeight));
menuField.setBorder(new MatteBorder(menuBarBorderLength, menuBarBorderLength, menuBarBorderLength, menuBarBorderLength,Color.BLACK));
menuField.setLayout(new FlowLayout(FlowLayout.RIGHT, menuButtonWidthSpace, menuButtonHeightSpace));
menuField.setOpaque(false);
add(BorderLayout.NORTH, menuField);
JButton addPlayer = new JButton();
addPlayer.setPreferredSize(new Dimension(menuButtonWidth, menuButtonHeight));
addPlayer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
makePopUpAddPlayer();
popUpAddPlayer.show();
textFieldPlayer.requestFocusInWindow();
}
});
menuField.add(addPlayer);
}
public void makePopUpSettingsAddPlayer(){
int popUpWidth = windowWidth/3;
int popUpHeight = windowHeight/3;
popUpXPlayer = windowWidth/2-popUpWidth/2;
popUpYPlayer = windowHeight/2-popUpHeight/2;
int popUpBorderLength = windowHeight/250;
int namePanelHeightPop = popUpHeight/3;
int nameTextFieldHeightPop = namePanelHeightPop;
int buttonFieldHeightPop = namePanelHeightPop;
int buttonWidthPop = popUpWidth/3;
int buttonHeightPop = 2*buttonFieldHeightPop/3;
int buttonSpaceWidthPop = buttonWidthPop/3;
int buttonSpaceHeightPop = (buttonFieldHeightPop-buttonHeightPop)/2;
int buttonBorderLengthPop = windowHeight/350;
popUpPanelPlayer = new JPanel();
popUpPanelPlayer.setBackground(Color.white);
popUpPanelPlayer.setBorder(new MatteBorder(popUpBorderLength, popUpBorderLength, popUpBorderLength, popUpBorderLength, Color.BLACK));
popUpPanelPlayer.setPreferredSize(new Dimension(popUpWidth, popUpHeight));
popUpPanelPlayer.setLayout(new BorderLayout());
JPanel namePanel = new JPanel();
namePanel.setPreferredSize(new Dimension(popUpWidth, namePanelHeightPop));
namePanel.setOpaque(false);
JTextField textField = new JTextField();
textField.setPreferredSize(new Dimension(popUpWidth, namePanelHeightPop));
textField.setBorder(new MatteBorder(0,0,0,0,Color.BLACK));
textField.setBackground(popUpPanelPlayer.getBackground());
textField.setForeground(Color.BLACK);
textField.setFont(new Font("Calibri", Font.PLAIN, namePanelHeightPop));
textField.setText("Name:");
textField.setEditable(false);
namePanel.add(textField);
popUpPanelPlayer.add(BorderLayout.NORTH, namePanel);
JPanel nameTextField = new JPanel();
nameTextField.setPreferredSize(new Dimension(popUpWidth, nameTextFieldHeightPop));
nameTextField.setLayout(new BorderLayout());
nameTextField.setOpaque(false);
textFieldPlayer = new JTextField();
textFieldPlayer.setDocument(new textLimitDoc(8));
textFieldPlayer.setPreferredSize(new Dimension(popUpWidth, nameTextFieldHeightPop));
textFieldPlayer.setBorder(new MatteBorder(0,0,0,0,Color.BLACK));
textFieldPlayer.setBackground(popUpPanelPlayer.getBackground());
textFieldPlayer.setForeground(Color.black);
textFieldPlayer.setFont(new Font("Calibri", Font.PLAIN, nameTextFieldHeightPop));
textFieldPlayer.setHorizontalAlignment(JTextField.CENTER);
nameTextField.add(textFieldPlayer);
popUpPanelPlayer.add(BorderLayout.CENTER, nameTextField);
JPanel buttonField = new JPanel();
buttonField.setLayout(new FlowLayout(FlowLayout.CENTER, buttonSpaceWidthPop, buttonSpaceHeightPop));
buttonField.setPreferredSize(new Dimension(0, buttonFieldHeightPop));
buttonField.setBackground(Color.blue);
buttonField.setOpaque(false);
JButton okButton = new JButton();
okButton.setBorder(new MatteBorder(buttonBorderLengthPop, buttonBorderLengthPop, buttonBorderLengthPop, buttonBorderLengthPop, Color.black));
okButton.setBackground(Color.white);
okButton.setFont(new Font("Calibri",Font.PLAIN, 9*buttonHeightPop/10));
okButton.setForeground(Color.black);
okButton.setText("OK");
okButton.setPreferredSize(new Dimension(buttonWidthPop, buttonHeightPop));
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
textFieldPlayer.setText("");
popUpAddPlayer.hide();
}
});
buttonField.add(okButton);
JButton cancelButton = new JButton();
cancelButton.setBorder(new MatteBorder(buttonBorderLengthPop, buttonBorderLengthPop, buttonBorderLengthPop, buttonBorderLengthPop, Color.black));
cancelButton.setBackground(Color.white);
cancelButton.setFont(new Font("Calibri",Font.PLAIN, 9*buttonHeightPop/10));
cancelButton.setForeground(Color.black);
cancelButton.setText("X");
cancelButton.setPreferredSize(new Dimension(buttonWidthPop, buttonHeightPop));
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
textFieldPlayer.setText("");
popUpAddPlayer.hide();
}
});
buttonField.add(cancelButton);
popUpPanelPlayer.add(BorderLayout.SOUTH, buttonField);
}
public void makePopUpAddPlayer(){
PopupFactory factory = new PopupFactory();
popUpAddPlayer = factory.getPopup(this, popUpPanelPlayer, popUpXPlayer, popUpYPlayer);
}
public class textLimitDoc extends PlainDocument{
private int max;
public textLimitDoc(int max){
this.max = max;
}
public void replace(int offs, int length, String str, AttributeSet a) throws BadLocationException{
if(getLength() + str.length() <= max){
super.insertString(offs, str, (javax.swing.text.AttributeSet) a);
}
}
}
}