Emily
November 7th, 2010, 02:21
Pictures:
Only the registered members can see the link.
Only the registered members can see the link.
Only the registered members can see the link.
Code:
package Runnable;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.*;
public class DialogCommunication extends JFrame {
/**
* @author Emily
*/
private static final long serialVersionUID = 2226971595902108186L;
DialogClass dialogClass;
JLabel label;
public DialogCommunication() {
dialogClass = new DialogClass(this);
/*
* Jbuttons List
* Just contains the string, does not actually send them until later
*/
JButton launch = new JButton("Select Music");
JButton play = new JButton("Play/Pause");
JButton stop = new JButton("Stop");
JButton back = new JButton("Back");
JButton next = new JButton("Next");
/*
* Basically the listener for this Jbutton
* if the Jbutton is selected/clicked, it finds
* the specified class, and runs the void
* is the same for the rest of the jbuttons just calls
* different voids
*/
launch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialogClass.showDialog();
}
});
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialogClass.play();
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialogClass.stop();
}
});
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialogClass.back();
}
});
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialogClass.next();
}
});
/*
* Setting the Jbuttons, setting Size of Jframe
* Setting Properties, and closing operation when exited
* Sets background to white, and sends the string for the title
*/
JPanel north = new JPanel();
north.add(launch);
north.add(play);
north.add(stop);
north.add(back);
north.add(next);
label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
JFrame f = new JFrame();
f.setTitle("Media Player");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(north, "North");
f.getContentPane().add(label, "South");
f.setSize(450,110);
f.setLocation(200,200);
f.setVisible(true);
f.setBackground(Color.white);
f.setResizable(false);
}
/*
* Sets the label text
*/
public void setLabelText(String text) {
label.setText(text);
}
public static void main(String[] args) {
new DialogCommunication();
}
}
class DialogClass {
DialogCommunication dialogCommunication;
JDialog dialog;
public boolean isPlaying;
public DialogClass(DialogCommunication dc) {
dialogCommunication = dc;
createDialog();
}
/*
* Creates a dialog box that pops up when clicking select music
* sends input box, and then sends that string on the main screen
* after selecting the confirm Jbutton
*/
private void createDialog() {
dialog = new JDialog(dialogCommunication, "Song Name:", false);
final JTextField textField = new JTextField(16);
JPanel north = new JPanel();
north.add(textField);
JButton send = new JButton("Confirm");
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
dialogCommunication.setLabelText("Now Playing: "+text);
dialog.dispose();
}
});
JPanel south = new JPanel();
south.add(send);
dialog.getContentPane().add(north, "North");
dialog.getContentPane().add(south, "South");
dialog.setSize(200,140);
dialog.setLocationRelativeTo(dialogCommunication);
}
/*
* Makes the dialog visable
* Able to see the String on the main screen now
*/
public void showDialog() {
dialog.setVisible(true);
}
/*
* Controls hitting the Play/Pause button
*/
public void play() {
if(isPlaying == true) {
isPlaying = false;
System.out.print("Paused");
} else if(isPlaying == false) {
isPlaying = true;
System.out.print("Playing");
}
//TODO Finish
}
/*
* Controls hitting the Stop button
*/
public void stop() {
System.out.print("Stop");
//TODO Finish
}
/*
* Controls hitting the Next button
*/
public void next() {
System.out.print("Next");
//TODO Finish
}
/*
* Controls hitting the Back button
*/
public void back() {
System.out.print("Back");
//TODO Finish
}
}
Explain Parts:
JButton launch = new JButton("Select Music");
This is where you declare your button, you give it a declare name, and also give it the string name that it will produce
launch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialogClass.showDialog();
}
});
To put it in simple talk, basically we use the declare name, and call the ActionListener.
The action listener, is simply when the button is clicked, then it will preform an event, that calls a void declared later
JPanel north = new JPanel();
north.add(launch);
First you declare your JPanel, in this case its called north.
Then, you add your button that you declared earlier, this is what makes the button appear, if you skip this then it will not appear on the JFrame when it is ran.
JFrame f = new JFrame();
Again we declare a new JFrame
These are some 'basic' snippets you can use with JFrame
f.setTitle("Media Player");
Sets the title of the JFrame
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Closes the program when clicking the red X.
f.setSize(450,110);
Sets the size of the JFrame 'Client'
f.setLocation(200,200);
Sets the location on popup
f.setVisible(true);
Makes the JFrame Visible
f.setBackground(Color.white);
Sets the color of the background of the client, you can do pretty much any color combination you want
f.setResizable(false);
Allows the client to be changed in size after opened (if true), or Denys that option (if false)
private void createDialog() {
dialog = new JDialog(dialogCommunication, "Song Name:", false);
final JTextField textField = new JTextField(16);
JPanel north = new JPanel();
north.add(textField);
JButton send = new JButton("Confirm");
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
dialogCommunication.setLabelText("Now Playing: "+text);
dialog.dispose();
}
});
JPanel south = new JPanel();
south.add(send);
dialog.getContentPane().add(north, "North");
dialog.getContentPane().add(south, "South");
dialog.setSize(200,140);
dialog.setLocationRelativeTo(dialogCommunication);
}
This simply opens a new box, with an input box, that you may type a string
It sends a JButton "Confirm", and if selected closes the box, and sends the string on the main screen
*Note*
This is does not actually play music, it is more of an example on using Jbuttons, and Jframes. Currently this is incomplete as I am still writing it.
Credits:
Emily
Java Forums, for a quick question on setting the text after confirm.
Only the registered members can see the link.
Only the registered members can see the link.
Only the registered members can see the link.
Code:
package Runnable;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.*;
public class DialogCommunication extends JFrame {
/**
* @author Emily
*/
private static final long serialVersionUID = 2226971595902108186L;
DialogClass dialogClass;
JLabel label;
public DialogCommunication() {
dialogClass = new DialogClass(this);
/*
* Jbuttons List
* Just contains the string, does not actually send them until later
*/
JButton launch = new JButton("Select Music");
JButton play = new JButton("Play/Pause");
JButton stop = new JButton("Stop");
JButton back = new JButton("Back");
JButton next = new JButton("Next");
/*
* Basically the listener for this Jbutton
* if the Jbutton is selected/clicked, it finds
* the specified class, and runs the void
* is the same for the rest of the jbuttons just calls
* different voids
*/
launch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialogClass.showDialog();
}
});
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialogClass.play();
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialogClass.stop();
}
});
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialogClass.back();
}
});
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialogClass.next();
}
});
/*
* Setting the Jbuttons, setting Size of Jframe
* Setting Properties, and closing operation when exited
* Sets background to white, and sends the string for the title
*/
JPanel north = new JPanel();
north.add(launch);
north.add(play);
north.add(stop);
north.add(back);
north.add(next);
label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
JFrame f = new JFrame();
f.setTitle("Media Player");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(north, "North");
f.getContentPane().add(label, "South");
f.setSize(450,110);
f.setLocation(200,200);
f.setVisible(true);
f.setBackground(Color.white);
f.setResizable(false);
}
/*
* Sets the label text
*/
public void setLabelText(String text) {
label.setText(text);
}
public static void main(String[] args) {
new DialogCommunication();
}
}
class DialogClass {
DialogCommunication dialogCommunication;
JDialog dialog;
public boolean isPlaying;
public DialogClass(DialogCommunication dc) {
dialogCommunication = dc;
createDialog();
}
/*
* Creates a dialog box that pops up when clicking select music
* sends input box, and then sends that string on the main screen
* after selecting the confirm Jbutton
*/
private void createDialog() {
dialog = new JDialog(dialogCommunication, "Song Name:", false);
final JTextField textField = new JTextField(16);
JPanel north = new JPanel();
north.add(textField);
JButton send = new JButton("Confirm");
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
dialogCommunication.setLabelText("Now Playing: "+text);
dialog.dispose();
}
});
JPanel south = new JPanel();
south.add(send);
dialog.getContentPane().add(north, "North");
dialog.getContentPane().add(south, "South");
dialog.setSize(200,140);
dialog.setLocationRelativeTo(dialogCommunication);
}
/*
* Makes the dialog visable
* Able to see the String on the main screen now
*/
public void showDialog() {
dialog.setVisible(true);
}
/*
* Controls hitting the Play/Pause button
*/
public void play() {
if(isPlaying == true) {
isPlaying = false;
System.out.print("Paused");
} else if(isPlaying == false) {
isPlaying = true;
System.out.print("Playing");
}
//TODO Finish
}
/*
* Controls hitting the Stop button
*/
public void stop() {
System.out.print("Stop");
//TODO Finish
}
/*
* Controls hitting the Next button
*/
public void next() {
System.out.print("Next");
//TODO Finish
}
/*
* Controls hitting the Back button
*/
public void back() {
System.out.print("Back");
//TODO Finish
}
}
Explain Parts:
JButton launch = new JButton("Select Music");
This is where you declare your button, you give it a declare name, and also give it the string name that it will produce
launch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialogClass.showDialog();
}
});
To put it in simple talk, basically we use the declare name, and call the ActionListener.
The action listener, is simply when the button is clicked, then it will preform an event, that calls a void declared later
JPanel north = new JPanel();
north.add(launch);
First you declare your JPanel, in this case its called north.
Then, you add your button that you declared earlier, this is what makes the button appear, if you skip this then it will not appear on the JFrame when it is ran.
JFrame f = new JFrame();
Again we declare a new JFrame
These are some 'basic' snippets you can use with JFrame
f.setTitle("Media Player");
Sets the title of the JFrame
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Closes the program when clicking the red X.
f.setSize(450,110);
Sets the size of the JFrame 'Client'
f.setLocation(200,200);
Sets the location on popup
f.setVisible(true);
Makes the JFrame Visible
f.setBackground(Color.white);
Sets the color of the background of the client, you can do pretty much any color combination you want
f.setResizable(false);
Allows the client to be changed in size after opened (if true), or Denys that option (if false)
private void createDialog() {
dialog = new JDialog(dialogCommunication, "Song Name:", false);
final JTextField textField = new JTextField(16);
JPanel north = new JPanel();
north.add(textField);
JButton send = new JButton("Confirm");
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
dialogCommunication.setLabelText("Now Playing: "+text);
dialog.dispose();
}
});
JPanel south = new JPanel();
south.add(send);
dialog.getContentPane().add(north, "North");
dialog.getContentPane().add(south, "South");
dialog.setSize(200,140);
dialog.setLocationRelativeTo(dialogCommunication);
}
This simply opens a new box, with an input box, that you may type a string
It sends a JButton "Confirm", and if selected closes the box, and sends the string on the main screen
*Note*
This is does not actually play music, it is more of an example on using Jbuttons, and Jframes. Currently this is incomplete as I am still writing it.
Credits:
Emily
Java Forums, for a quick question on setting the text after confirm.