InLab10 Solution

InLab10 Solution - // Constructor public LabelTester2() {...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 //Part 1 import javax.swing.* import java.awt.GridLayout public class LabelTester extends JFrame { // Constructor public LabelTester() { setLayout(new GridLayout(2, 1, 5, 5)) add(new JLabel("This is Duke")) add(new JLabel(new ImageIcon("duke.jpg"))) } // Main method public static void main(String[] args) { LabelTester frame = new LabelTester() frame.setTitle("LabelTester") frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) frame.setSize(200, 150) frame.setVisible(true) } } //Part 2 import javax.swing.* import java.awt.* import java.awt.event.* public class LabelTester2 extends JFrame { // Declare components as instance variables // so that the ActionListener can access them private JButton buttonShow = new JButton("Show") private JButton buttonHide = new JButton("Hide") private JLabel label = new JLabel("No Image")
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // Constructor public LabelTester2() { // Set up the frame setLayout(new GridLayout(3, 1, 5, 5)) add(buttonShow) add(buttonHide) add(label) // Add event handler buttonShow.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { label.setText(null) label.setIcon(new ImageIcon("duke.jpg")) } }) // Add event handler Sheet1 Page 2 buttonHide.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { label.setText("No image") label.setIcon(null) } }) } // Main method public static void main(String args) { LabelTester2 frame = new LabelTester2() frame.setTitle("Duke") frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) frame.setSize(60, 250) frame.setVisible(true) } }...
View Full Document

Page1 / 2

InLab10 Solution - // Constructor public LabelTester2() {...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online