Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tutorialspoint.gui;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.*;
- public class SwingControlDemo {
- private JFrame mainFrame;
- private JLabel headerLabel;
- private JLabel statusLabel;
- private JPanel controlPanel;
- public SwingControlDemo(){
- prepareGUI();
- }
- public static void main(String[] args){
- SwingControlDemo swingControlDemo = new SwingControlDemo();
- swingControlDemo.showButtonDemo();
- }
- private void prepareGUI(){
- mainFrame = new JFrame("Pong");
- mainFrame.setSize(1280,720);
- mainFrame.setLayout(new GridLayout(5, 2));
- mainFrame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent windowEvent){
- System.exit(0);
- }
- });
- headerLabel = new JLabel("", JLabel.CENTER);
- statusLabel = new JLabel("",JLabel.CENTER);
- statusLabel.setSize(350,100);
- controlPanel = new JPanel();
- controlPanel.setLayout(new FlowLayout());
- mainFrame.add(headerLabel);
- mainFrame.add(controlPanel);
- mainFrame.add(statusLabel);
- mainFrame.setVisible(true);
- }
- private static ImageIcon createImageIcon(String path,
- String description) {
- java.net.URL imgURL = SwingControlDemo.class.getResource(path);
- if (imgURL != null) {
- return new ImageIcon(imgURL, description);
- } else {
- System.err.println("Couldn't find file: " + path);
- return null;
- }
- }
- private void showButtonDemo(){
- headerLabel.setText("Good game and Have fun ;)");
- //resources folder should be inside SWING folder.
- ImageIcon icon = new ImageIcon("exit_test.png");
- ImageIcon ExitIcon = new ImageIcon(getClass().getResource("/com/tutorialspoint/gui/images/" + "exit_test.png"));
- // JButton chooseButton = new JButton("Choose Background");
- JButton ExitButton = new JButton(ExitIcon);
- ExitButton.setPreferredSize(new Dimension(300,100));
- // JButton AboutButton = new JButton("About");
- // JButton SingleButton = new JButton("Single Player");
- // JButton MultyButton = new JButton("Multy Player", icon);
- // JButton ControlsButton = new JButton("Controls", icon);
- // ControlsButton.setHorizontalTextPosition(SwingConstants.LEFT);
- //
- ExitButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- statusLabel.setText("Exit Button clicked.");
- statusLabel.setAlignmentX(100);
- statusLabel.setAlignmentY(30);
- }
- });
- //
- // AboutButton.addActionListener(new ActionListener() {
- // public void actionPerformed(ActionEvent e) {
- // statusLabel.setText("About Button clicked.");
- // }
- // });
- //
- // SingleButton.addActionListener(new ActionListener() {
- // public void actionPerformed(ActionEvent e) {
- // statusLabel.setText("Single Player Button clicked.");
- // }
- // });
- //
- // MultyButton.addActionListener(new ActionListener() {
- // public void actionPerformed(ActionEvent e) {
- // statusLabel.setText("Multy Player Button clicked.");
- // }
- // });
- //
- // ControlsButton.addActionListener(new ActionListener() {
- // public void actionPerformed(ActionEvent e) {
- // statusLabel.setText("Control Button clicked.");
- // }
- // });
- //
- // chooseButton.addActionListener(new ActionListener() {
- // public void actionPerformed(ActionEvent e) {
- // Color backgroundColor = JColorChooser.showDialog(mainFrame,
- // "Choose background color", Color.ORANGE);
- // if(backgroundColor != null){
- // controlPanel.setBackground(backgroundColor);
- // mainFrame.getContentPane().setBackground(backgroundColor);
- // }
- // }
- // });
- //
- //
- // controlPanel.add(chooseButton);
- controlPanel.add(ExitButton);
- // controlPanel.add(AboutButton);
- // controlPanel.add(SingleButton);
- // controlPanel.add(MultyButton);
- // controlPanel.add(ControlsButton);
- mainFrame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement