Hwk6x - Personal.psu.edu

paltryboarpigΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 1 μήνα)

62 εμφανίσεις

Nathan Cruz

Homework #6


6.1

i
mport java.awt.*;


import javax.swing.*;


import java.awt.event.*;




public class Hwk6


{


public static void main(String[] args)



{



// 1. Create a frame



JFrame frame = new JFrame("Button");






// 2. Optional: what
happens when frame closed?



frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);






// 3. Make mainPanel and add to frame



MainPanel mainPanel = new MainPanel();



frame.getContentPane().add(mainPanel);






// 4. Pack frame



frame.pack();







//
5. Make frame visible



frame.setVisible(true);



}




}


public class MainPanel extends JPanel


{



// Define components (aka instance variables)



private JButton rollButton, closeButton;



private JPanel buttonPanel;






// Constructor



public
MainPanel()



{




// Create components




numberButton = new JButton("Pick a number");




numberButton.addActionListener(new ButtonListener());




closeButton = new JButton("Close");




closeButton.addActionListener(new ButtonListener());




buttonPanel =

new JPanel();




buttonPanel.setBackground(Color.cyan);








// Add the button to the panel




buttonPanel.add(rollButton);




buttonPanel.add(closeButton);




this.add(buttonPanel);








// Set various frame/window attributes




this.setPreferredSiz
e(new Dimension(300,50));



}






// ButtonListener event handler



private class ButtonListener implements ActionListener



{




public void actionPerformed(ActionEvent evt)




{





if (evt.getSource() == numberButton)






handleRollButton();





if (evt.getSource() == closeButton)






System.exit(0);




}



}






public void handle numberButton ()


{



for (int num = 0 ; i < 100 ; i++)





{






num = (int) (Math.random ());





System.out.print(num);




}


}


}




6.2

public class MainPanel extends JPanel


{



// Define components (aka instance variables)



private JButton incrementButton, decrement
Button;



private JPanel buttonPanel;






// Constructor



public MainPanel()



{




// Create components




increment
Button = new JButton("
Increment
");




increment
Button.addActionListener(new ButtonListener());








// Add the button to the panel




buttonPanel.add(
incrementButton
);




buttonPanel.add(decrement
Button);




this.add(buttonPanel);








// Set
various frame/window attributes




this.setPreferredSize(new Dimension(300,50));



}






// ButtonListener event handler



private class ButtonListener implements ActionListener



{




public void actionPerformed(ActionEvent evt)




{





if (evt.getSour
ce() ==
increment
Button)






handle
incrementButton
();





if (evt.getSource() == decrement
Button)






handleincrementButton();




}



}






public void handle
increment
Button ()


{



for (int num

= 50 ; num

< 100
0

; i++)





{






num =
int++





System.out.print(num);




}


}




public void handle
decrement
Button ()


{



for (int num

= 50 ; num

< 100
0

; i++)





{






num =
int
--





System.out.print(num);




}


}


}






6.3

Public void handle bo
thButton()

{


if (evt.getSource() == incrementButton)

{




num++;



System.out.print(num)


}


if (evt.getSource() == decrementButton)

{




Num
--
;



System.out.print(num)


}

}