website/static/~brozek/index.html?labaide%2Ffall2017%2Fcpsc220%2Fnov15.html

175 lines
5.6 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="author" content="Fredrik Danielsson, http://lostkeys.se">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex" />
<title>Brandon Rozek</title>
<link rel="stylesheet" href="themes/bitsandpieces/styles/main.css" type="text/css" />
<link rel="stylesheet" href="themes/bitsandpieces/styles/highlightjs-github.css" type="text/css" />
</head>
<body>
<aside class="main-nav">
<nav>
<ul>
<li class="menuitem ">
<a href="index.html%3Findex.html" data-shortcut="">
Home
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Fcourses.html" data-shortcut="">
Courses
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Flabaide.html" data-shortcut="">
Lab Aide
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Fpresentations.html" data-shortcut="">
Presentations
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Fresearch.html" data-shortcut="">
Research
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Ftranscript.html" data-shortcut="">
Transcript
</a>
</li>
</ul>
</nav>
</aside>
<main class="main-content">
<article class="article">
<h1>Lecture Notes for November 15th</h1>
<p>Import the necessary libraries</p>
<pre><code class="language-java">import java.awt.*;
import java.awt.event.*;
import javax.swing.*;</code></pre>
<p>This time we'll be using the null layout. This will require you to set the x, and y positions of each of the elements on the screen</p>
<p>First we'll make a class called <code>GUILayout</code> which will extend <code>JPanel</code> and contain our layout and components</p>
<pre><code class="language-java">import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUILayout extends JPanel {
private JButton one, two;
private JTextField text;
private JTextArea area;
private int count; // Used to keep track of button pushes
private int number = 0;
GUILayout() {
// Set the layout to the null layout
setLayout(null);
// Tells the computer that you wish to have the size of your program to be 500x500
setPreferredSize(new Dimension(500, 500));
// Create the buttons
one = new JButton("Change color");
two = new JButton("Count Pushes");
// Set the x, y, width, height inside parenthesis
// This places the buttons on the specified locations on the screen
one.setBounds(10, 10, 100, 24);
two.setBounds(120, 10, 100, 24);
// Sets the color of the text of button 1 to blue
one.setForeground(Color.BLUE);
// Add the components to the screen
add(one);
add(two);
text = new JTextField("Today is Wednesday, a photographer is here.");
text.setBounds(10, 40 ,480, 24);
add(text);
area = new JTextArea(20, 20);
// Wrap the text area into the scroll pane
// This adds the scroll bars (vertical/horizontal)
JScrollPane sp = new JScrollPane(area);
sp.setBounds(10, 75, 490, 400);
add(sp);
// Bind the Listener class to the button one
one.addActionListener(new Listener());
// Bind it to two and text
two.addActionListener(new Listener());
text.addActionListener(new Listener());
}
// Create the class for the listener
private class Listener implements ActionListener {
// Define what you want to occur after an event
public void actionPerformed(ActionEvent event) {
if (event.getSource() == one) {
} else if (event.getSource() == two) {
count++;
// \n is needed to prevent scrolling
area.append("You pressed the button " + count + " times!");
} else if (event.getSource() == text) {
// Grab the number inside the text area and set it to the variable number
number = Integer.paseInt(text.getText());
number += 10;
// add "" to number so that it converts number to a String
text.setText(number + "");
// Convert the number to a String
String temp = Integer.toString(number);
area.setText(temp);
}
}
}
}</code></pre>
<p>The main code is really short, you just need to extend <code>JFrame</code> and some short code seen last class to set it up.</p>
<pre><code class="language-java">public class mainTester extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("Sample GUI"); // Sets the title to "Sample GUI"
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Instantiate the GUILayout file you made before
frame.getContentPane().add(new GUILayout());
}
}</code></pre>
</article>
</main>
<script src="themes/bitsandpieces/scripts/highlight.js"></script>
<script src="themes/bitsandpieces/scripts/mousetrap.min.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true
}
});
</script>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script>
hljs.initHighlightingOnLoad();
document.querySelectorAll('.menuitem a').forEach(function(el) {
if (el.getAttribute('data-shortcut').length > 0) {
Mousetrap.bind(el.getAttribute('data-shortcut'), function() {
location.assign(el.getAttribute('href'));
});
}
});
</script>
</body>
</html>