Java - Graphics Create graphics classes based on the Java Swing Library Delivera
ID: 671098 • Letter: J
Question
Java - Graphics
Create graphics classes based on the Java Swing Library
Deliverables
myJPanel.java and other classes as requested below.
Contents
What we have till now is:
- app. java (which uses our extended class myJFrame.java)
- myJFrame.java creates a standard JPanel
But you cannot do much with a standard JPanel.
We need to create a smart version of JPanel.
Once we have our own JPanel we can
- add JButtons to it.
- set different colors to the background.
For this lab you need to
create your own version of a JPanel using inheritance
Change our myJFrame.java so that it now creates your version of a JPanel instead of the standard JPanel.
Suggestions
In order to add a JButton to a JPanel:
JButton j1 = new JButton (“hello”);
add(j1);
Code So Far:
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JButtonListenerExample
{
static JFrame frame;
public static void main(String[] args)
{
// schedule this for the event dispatch thread (edt)
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
displayJFrame();
}
});
}
static void displayJFrame()
{
frame = new JFrame("Our JButton listener example");
// create our jbutton
JButton showDialogButton = new JButton("Click Me");
// add the listener to the jbutton to handle the "pressed" event
showDialogButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// display/center the jdialog when the button is pressed
JDialog d = new JDialog(frame, "Hello", true);
d.setLocationRelativeTo(frame);
d.setVisible(true);
}
});
// put the button on the frame
frame.getContentPane().setLayout(new FlowLayout());
frame.add(showDialogButton);
// set up the jframe, then display it
frame.setDefaultCloseOperation
(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(640, 480));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.