Looking to add a KeyListener or MouseListener to my current java program. I miss
ID: 3807816 • Letter: L
Question
Looking to add a KeyListener or MouseListener to my current java program.
I missed this requirment for the program and now cannot seem to find a good idea on how to implement one into program. It can be very basic but im not sure what else i can add.
Any ideas?
Moives.java
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class Moives extends JFrame
{
private static Game movies[];
private static JButton sort = new JButton("Perform Sorting");
private static JTextArea textarea = new JTextArea(20, 35);
public Moives(){
sort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Sorting.selectionSort(movies);
//Displaying result in textarea
display();
}
});
setLayout(new FlowLayout());
add(new JScrollPane(textarea));
add(sort);
}
public static void setFrame(final JFrame frame, final int width, final int height) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.setTitle(frame.getClass().getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
});
}
public static void display()
{
String ans="";
for(Game Game:movies)
ans+=Game+" ";
textarea.setText(ans);
}
public static void main(String[] args){
//Game[] movies=new Game[7];
movies=new Game[7];
movies[0]= new Game ("The Godfather", "Francis Ford Coppola", 1972, 24.95, true);
movies[1]= new Game ("District 9", "Neill Blokamp", 2009, 19.95, false);
movies[2]= new Game ("Iron Man", "Jon Favreau", 2008, 15.95, false);
movies[3]= new Game ("All About Eve", "Joseph Mankiewicz", 1950, 17.50, false);
movies[4]= new Game ("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true);
movies[5]= new Game ("Iron Man 2", "Jon Favreau", 2010, 22.99, false);
movies[6]= new Game ("Casablanca", "Michael Curtiz", 1942, 19.95, false);
setFrame(new Moives(), 500, 500);
display();
}
}
Game.java
import java.text.NumberFormat;
public class Game implements Comparable{
private String tile, company;
private int year;
private double rating;
private boolean virtualReality;
private String title;
public Game (String title, String company, int year, double rating, boolean virtualReality){
this.title=title;
this.year= year;
this.company=company;
this.rating=rating;
this.virtualReality=virtualReality;
}
public String getTitle(){
return title;
}
public String toString(){
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String description;
description = fmt.format(rating) + " " + year + " ";
description += title + " " + company;
if (virtualReality)
description += " " + "Virtual Reality";
return description;
}
public int compareTo(Object otherObject){
Game otherGame=(Game)otherObject;
return getTitle().compareTo(otherGame.getTitle());
}
}
Sorting.java
public class Sorting {
public static void selectionSort(Comparable[] list){
int min;
Comparable temp;
for(int index=0; index min=index;
for (int scan = index+1; scan < list.length; scan++)
if (list[scan].compareTo(list[min])<0)
min=scan;
temp=list[min];
list[min]=list[index];
list[index]=temp;
}
}
}
Explanation / Answer
When the mouse enters and exits the frame or panel, we can highlight the button(change background) or we can change the label of the button.
This is example of changing the background colour of the button when mouse enters and exits.
public Moives(){
sort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Sorting.selectionSort(movies);
//Displaying result in textarea
display();
}
});
setLayout(new FlowLayout());
add(new JScrollPane(textarea));
add(sort);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.