SwingCodeExamplesNB as a Maven Netbeans Project
This commit is contained in:
1 parent
bd5d9d6304
commit
e0a3b009b6
30 files changed
+1428
No files matched your search
@@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>javateaching</groupId>
|
||||||
|
<artifactId>SwingCodeExamplesNB</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package javateaching.basicgui;
|
||||||
|
|
||||||
|
import java.awt.FlowLayout;
|
||||||
|
import javax.swing.ButtonGroup;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JCheckBox;
|
||||||
|
import javax.swing.JComboBox;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JRadioButton;
|
||||||
|
import javax.swing.JTextField;
|
||||||
|
import javax.swing.WindowConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paul
|
||||||
|
*/
|
||||||
|
public class BasicGUITest {
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
JFrame ourwindow = new JFrame("Yay! Window");
|
||||||
|
ourwindow.setLayout(new FlowLayout());
|
||||||
|
|
||||||
|
JLabel label = new JLabel("Enter your name");
|
||||||
|
JTextField text = new JTextField("Bill Bloggs");
|
||||||
|
JCheckBox cbox = new JCheckBox("Enable");
|
||||||
|
|
||||||
|
JRadioButton rbut = new JRadioButton("Leave");
|
||||||
|
JRadioButton rbut2 = new JRadioButton("Remain");
|
||||||
|
ButtonGroup brexit = new ButtonGroup();
|
||||||
|
brexit.add(rbut);
|
||||||
|
brexit.add(rbut2);
|
||||||
|
|
||||||
|
String[] comboOptions = {"red","green","blue"};
|
||||||
|
JComboBox comboBox = new JComboBox(comboOptions);
|
||||||
|
|
||||||
|
JButton buttonOK = new JButton("OK");
|
||||||
|
|
||||||
|
ourwindow.add(label);
|
||||||
|
ourwindow.add(text);
|
||||||
|
ourwindow.add(cbox);
|
||||||
|
ourwindow.add(rbut);
|
||||||
|
ourwindow.add(rbut2);
|
||||||
|
ourwindow.add(comboBox);
|
||||||
|
ourwindow.add(buttonOK);
|
||||||
|
|
||||||
|
for (int i = 0; i < 50; i++) ourwindow.add(new JButton("Button"));
|
||||||
|
|
||||||
|
ourwindow.setSize(600,400);
|
||||||
|
|
||||||
|
ourwindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
ourwindow.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package javateaching.basicgui;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.WindowConstants;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paul
|
||||||
|
*/
|
||||||
|
public class BorderLayoutDemo
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
JFrame frame = new JFrame("Border Layout");
|
||||||
|
frame.setLayout(new BorderLayout());
|
||||||
|
frame.add(new JButton("North"),BorderLayout.NORTH);
|
||||||
|
frame.add(new JButton("South"),BorderLayout.SOUTH);
|
||||||
|
frame.add(new JButton("East"),BorderLayout.EAST);
|
||||||
|
frame.add(new JButton("West"),BorderLayout.WEST);
|
||||||
|
frame.add(new JButton("Center"),BorderLayout.CENTER);
|
||||||
|
|
||||||
|
frame.setSize(400,400);
|
||||||
|
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
frame.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package javateaching.basicgui;
|
||||||
|
|
||||||
|
import javateaching.leanconsole.LeanConsole;
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.WindowConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paul
|
||||||
|
*/
|
||||||
|
public class ComboLayout
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
JFrame frame = new JFrame("Combo Layout");
|
||||||
|
frame.setLayout(new BorderLayout());
|
||||||
|
frame.add(new JButton("North"),BorderLayout.NORTH);
|
||||||
|
frame.add(new JButton("South"),BorderLayout.SOUTH);
|
||||||
|
frame.add(new JButton("East"),BorderLayout.EAST);
|
||||||
|
frame.add(new JButton("West"),BorderLayout.WEST);
|
||||||
|
|
||||||
|
JPanel middlePanel = new JPanel();
|
||||||
|
middlePanel.setLayout(new GridLayout(2,2));
|
||||||
|
middlePanel.add(new JButton("1"));
|
||||||
|
middlePanel.add(new JButton("2"));
|
||||||
|
middlePanel.add(new JButton("3"));
|
||||||
|
//middlePanel.add(new JButton("4"));
|
||||||
|
|
||||||
|
LeanConsole gc = new LeanConsole();
|
||||||
|
middlePanel.add(gc);
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; i++)
|
||||||
|
{
|
||||||
|
gc.println("Hello from the console");
|
||||||
|
}
|
||||||
|
|
||||||
|
frame.add(middlePanel,BorderLayout.CENTER);
|
||||||
|
|
||||||
|
frame.setSize(400,400);
|
||||||
|
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
frame.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package javateaching.basicgui;
|
||||||
|
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paul
|
||||||
|
*/
|
||||||
|
public class Demo {
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
JFrame topwindow = new JFrame("My wonderful window");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package javateaching.basicgui;
|
||||||
|
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.WindowConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paul
|
||||||
|
*/
|
||||||
|
public class GridLayoutTest
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
JFrame window = new JFrame("Grid Layout Test");
|
||||||
|
window.setLayout(new GridLayout(6,7));
|
||||||
|
for (int row = 1; row <= 6; row++)
|
||||||
|
{
|
||||||
|
for (int col = 1; col <= 7; col++)
|
||||||
|
{
|
||||||
|
JButton cell = new JButton(col+","+row+" ");
|
||||||
|
window.add(cell);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
window.pack();
|
||||||
|
window.setVisible(true);
|
||||||
|
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package javateaching.basicgui;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.JTextArea;
|
||||||
|
import javax.swing.WindowConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paul
|
||||||
|
*/
|
||||||
|
public class LeanGui
|
||||||
|
{
|
||||||
|
public static void main(String[] args) throws InterruptedException
|
||||||
|
{
|
||||||
|
JFrame ourwindow = new JFrame("Yay! Window");
|
||||||
|
ourwindow.setLayout(new BorderLayout());
|
||||||
|
JTextArea text = new JTextArea();
|
||||||
|
text.setLineWrap(true); // otherwise everything is on one line
|
||||||
|
text.setEditable(false); // otherwise people can type over the text
|
||||||
|
ourwindow.add(text,BorderLayout.CENTER);
|
||||||
|
|
||||||
|
ourwindow.setSize(500,400);
|
||||||
|
ourwindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
ourwindow.setVisible(true);
|
||||||
|
|
||||||
|
// the next code is to illustrate "printing"
|
||||||
|
while (true) // repeat forever
|
||||||
|
{
|
||||||
|
String currentText = text.getText();
|
||||||
|
currentText += "Hello! ";
|
||||||
|
text.setText(currentText);
|
||||||
|
Thread.sleep(10); // add a delay just so you can see it build up
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package javateaching.basicgui;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.JTextArea;
|
||||||
|
import javax.swing.WindowConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paul
|
||||||
|
*/
|
||||||
|
public class LeanGuiWithScroll
|
||||||
|
{
|
||||||
|
public static void main(String[] args) throws InterruptedException // needed for Thread.sleep
|
||||||
|
{
|
||||||
|
JFrame ourwindow = new JFrame("Yay! Window");
|
||||||
|
ourwindow.setLayout(new BorderLayout());
|
||||||
|
JTextArea text = new JTextArea();
|
||||||
|
text.setLineWrap(true); // otherwise everything is on one line
|
||||||
|
text.setEditable(false); // otherwise people can type over the text
|
||||||
|
|
||||||
|
// JScrollPane gives magical scrollbar powers
|
||||||
|
// We specify the component that we want scrollable
|
||||||
|
// in the JScrollPane constructor
|
||||||
|
JScrollPane scrolly = new JScrollPane(text);
|
||||||
|
// we add the JScrollPane to our window
|
||||||
|
ourwindow.add(scrolly,BorderLayout.CENTER);
|
||||||
|
|
||||||
|
ourwindow.setSize(500,400);
|
||||||
|
ourwindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
ourwindow.setVisible(true);
|
||||||
|
|
||||||
|
// the next code is to illustrate "printing"
|
||||||
|
while (true) // repeat forever
|
||||||
|
{
|
||||||
|
String currentText = text.getText();
|
||||||
|
currentText += "Hello! ";
|
||||||
|
text.setText(currentText);
|
||||||
|
// below line will make it scroll to the bottom after each
|
||||||
|
// "print"
|
||||||
|
text.setCaretPosition(text.getDocument().getLength());
|
||||||
|
Thread.sleep(10); // add a delay just so you can see it build up
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.guimoar;
|
||||||
|
|
||||||
|
import java.awt.FlowLayout;
|
||||||
|
import java.awt.Color;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paulneve
|
||||||
|
*/
|
||||||
|
public class FlowLayoutExample extends JPanel {
|
||||||
|
|
||||||
|
private JButton component1 = new JButton("click me");
|
||||||
|
private JLabel component2 = new JLabel("component 2");
|
||||||
|
private JLabel component3 = new JLabel("component 3");
|
||||||
|
private JLabel component4 = new JLabel("component 4");
|
||||||
|
private JLabel component5 = new JLabel("component 5");
|
||||||
|
|
||||||
|
public FlowLayoutExample()
|
||||||
|
{
|
||||||
|
FlowLayout fl = new FlowLayout();
|
||||||
|
this.setLayout(fl);
|
||||||
|
|
||||||
|
component2.setBackground(Color.GREEN);
|
||||||
|
component2.setOpaque(true);
|
||||||
|
|
||||||
|
component3.setBackground(Color.ORANGE);
|
||||||
|
component3.setOpaque(true);
|
||||||
|
|
||||||
|
component4.setBackground(Color.CYAN);
|
||||||
|
component4.setOpaque(true);
|
||||||
|
|
||||||
|
component5.setBackground(Color.WHITE);
|
||||||
|
component5.setOpaque(true);
|
||||||
|
|
||||||
|
this.add(component1);
|
||||||
|
this.add(component2);
|
||||||
|
this.add(component3);
|
||||||
|
this.add(component4);
|
||||||
|
this.add(component5);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.guimoar;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paulneve
|
||||||
|
*/
|
||||||
|
public class GridLayoutExample extends JPanel {
|
||||||
|
|
||||||
|
private JLabel component1 = new JLabel("component 1",JLabel.CENTER);
|
||||||
|
private JLabel component2 = new JLabel("component 2", JLabel.CENTER);
|
||||||
|
private JLabel component3 = new JLabel("component 3");
|
||||||
|
private JLabel component4 = new JLabel("component 4");
|
||||||
|
private JLabel component5 = new JLabel("<html><h1>component 5 with BIGNESS</h1></html>");
|
||||||
|
private JLabel component6 = new JLabel("component 6");
|
||||||
|
|
||||||
|
public GridLayoutExample()
|
||||||
|
{
|
||||||
|
GridLayout gl = new GridLayout(0,3);
|
||||||
|
this.setLayout(gl);
|
||||||
|
|
||||||
|
component1.setBackground(Color.YELLOW);
|
||||||
|
component1.setOpaque(true);
|
||||||
|
|
||||||
|
component2.setBackground(Color.GREEN);
|
||||||
|
component2.setOpaque(true);
|
||||||
|
|
||||||
|
component3.setBackground(Color.ORANGE);
|
||||||
|
component3.setOpaque(true);
|
||||||
|
|
||||||
|
component4.setBackground(Color.CYAN);
|
||||||
|
component4.setOpaque(true);
|
||||||
|
|
||||||
|
component5.setBackground(Color.WHITE);
|
||||||
|
component5.setOpaque(true);
|
||||||
|
|
||||||
|
component4.setBackground(Color.CYAN);
|
||||||
|
component4.setOpaque(true);
|
||||||
|
|
||||||
|
this.add(component1);
|
||||||
|
this.add(component2);
|
||||||
|
this.add(component3);
|
||||||
|
this.add(component4);
|
||||||
|
this.add(component5);
|
||||||
|
this.add(component6);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.guimoar;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
import java.util.Random;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paulneve
|
||||||
|
*/
|
||||||
|
public class GridTo500 extends JPanel {
|
||||||
|
public GridTo500()
|
||||||
|
{
|
||||||
|
GridLayout gl = new GridLayout(0,15);
|
||||||
|
this.setLayout(gl);
|
||||||
|
|
||||||
|
for (int i = 1; i < 501; i++)
|
||||||
|
{
|
||||||
|
JLabel numberJL = new JLabel(String.valueOf(i+" "));
|
||||||
|
numberJL.setOpaque(true);
|
||||||
|
numberJL.setBackground(Color.getHSBColor(new Random().nextFloat(), 1.0F, 1.0F ));
|
||||||
|
this.add(numberJL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package javateaching.guimoar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paul
|
||||||
|
*/
|
||||||
|
public class MixAndMatchDemo {
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
// shortest program evar :-)
|
||||||
|
MixAndMatchWindow mainwindow = new MixAndMatchWindow();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package javateaching.guimoar;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.WindowConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paul
|
||||||
|
*/
|
||||||
|
public class MixAndMatchWindow extends JFrame
|
||||||
|
{
|
||||||
|
private FlowLayoutExample fl = new FlowLayoutExample();
|
||||||
|
private GridLayoutExample gl = new GridLayoutExample();
|
||||||
|
private GridTo500 g500 = new GridTo500();
|
||||||
|
|
||||||
|
public MixAndMatchWindow()
|
||||||
|
{
|
||||||
|
super("Demo of mixing and matching layouts with JPanes");
|
||||||
|
this.setLayout(new BorderLayout());
|
||||||
|
this.add(fl,BorderLayout.NORTH);
|
||||||
|
this.add(gl,BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
// try commenting out this...
|
||||||
|
this.add(g500,BorderLayout.CENTER);
|
||||||
|
|
||||||
|
// and uncommenting these two...
|
||||||
|
//JScrollPane scrolly = new JScrollPane(g500);
|
||||||
|
//this.add(scrolly,BorderLayout.CENTER);
|
||||||
|
|
||||||
|
// and observe the difference!
|
||||||
|
|
||||||
|
this.setSize(600,500);
|
||||||
|
this.setVisible(true);
|
||||||
|
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
// also try flipping round the positions of the components and
|
||||||
|
// see how things work out...
|
||||||
|
}
|
||||||
|
}
|
||||||
+51
@@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.layoutmanagers;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Color;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
*/
|
||||||
|
public class BorderLayoutExample extends JPanel {
|
||||||
|
public BorderLayoutExample()
|
||||||
|
{
|
||||||
|
BorderLayout bl = new BorderLayout();
|
||||||
|
this.setLayout(bl);
|
||||||
|
|
||||||
|
JLabel component1 = new JLabel("component 1",JLabel.CENTER);
|
||||||
|
component1.setBackground(Color.YELLOW);
|
||||||
|
component1.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component2 = new JLabel("component 2", JLabel.CENTER);
|
||||||
|
component2.setBackground(Color.GREEN);
|
||||||
|
component2.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component3 = new JLabel("component 3");
|
||||||
|
component3.setBackground(Color.ORANGE);
|
||||||
|
component3.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component4 = new JLabel("component 4");
|
||||||
|
component4.setBackground(Color.CYAN);
|
||||||
|
component4.setOpaque(true);
|
||||||
|
|
||||||
|
/* JLabel component5 = new JLabel("<html><h1>component 5 with BIGNESS</h1></html>");
|
||||||
|
component5.setBackground(Color.WHITE);
|
||||||
|
component5.setOpaque(true); */
|
||||||
|
|
||||||
|
JPanel component5 = new GridTo500();
|
||||||
|
|
||||||
|
this.add(component1,BorderLayout.NORTH);
|
||||||
|
this.add(component2,BorderLayout.SOUTH);
|
||||||
|
this.add(component3,BorderLayout.EAST);
|
||||||
|
this.add(component4,BorderLayout.WEST);
|
||||||
|
this.add(component5,BorderLayout.CENTER);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.layoutmanagers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paul
|
||||||
|
*/
|
||||||
|
public class Cat {
|
||||||
|
|
||||||
|
private String colour;
|
||||||
|
private int numberOfLegs;
|
||||||
|
private int loudnessOfPurr;
|
||||||
|
|
||||||
|
public void purr()
|
||||||
|
{
|
||||||
|
// cat purrs
|
||||||
|
}
|
||||||
|
|
||||||
|
public void inexplicablyScratchYou()
|
||||||
|
{
|
||||||
|
// etc
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getColour()
|
||||||
|
{
|
||||||
|
return colour;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColour(String colour)
|
||||||
|
{
|
||||||
|
this.colour = colour;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLoudnessOfPurr()
|
||||||
|
{
|
||||||
|
return loudnessOfPurr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLoudnessOfPurr(int loudnessOfPurr)
|
||||||
|
{
|
||||||
|
this.loudnessOfPurr = loudnessOfPurr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumberOfLegs()
|
||||||
|
{
|
||||||
|
return numberOfLegs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNumberOfLegs(int numberOfLegs)
|
||||||
|
{
|
||||||
|
this.numberOfLegs = numberOfLegs;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package javateaching.layoutmanagers;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
*/
|
||||||
|
public class CombiningLayouts {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame frame = new JFrame();
|
||||||
|
frame.setLayout(new BorderLayout());
|
||||||
|
frame.add(new JButton("North"),BorderLayout.NORTH);
|
||||||
|
frame.add(new JButton("South"),BorderLayout.SOUTH);
|
||||||
|
frame.add(new JButton("East"),BorderLayout.EAST);
|
||||||
|
frame.add(new JButton("West"),BorderLayout.WEST);
|
||||||
|
|
||||||
|
JPanel middlePanel = new JPanel();
|
||||||
|
middlePanel.setLayout(new GridLayout(2,2));
|
||||||
|
middlePanel.add(new JButton("1"));
|
||||||
|
middlePanel.add(new JButton("2"));
|
||||||
|
middlePanel.add(new JButton("3"));
|
||||||
|
middlePanel.add(new JButton("4"));
|
||||||
|
frame.add(middlePanel,BorderLayout.CENTER);
|
||||||
|
|
||||||
|
frame.setSize(400,400);
|
||||||
|
frame.setVisible(true);
|
||||||
|
|
||||||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.layoutmanagers;
|
||||||
|
|
||||||
|
import java.awt.FlowLayout;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JCheckBox;
|
||||||
|
import javax.swing.JComboBox;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JList;
|
||||||
|
import javax.swing.JRadioButton;
|
||||||
|
import javax.swing.JTextField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
*/
|
||||||
|
public class ComponentShowcase {
|
||||||
|
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
// add top level window
|
||||||
|
JFrame ourWindow = new JFrame("Component showcase");
|
||||||
|
// set layout
|
||||||
|
ourWindow.setLayout(new FlowLayout());
|
||||||
|
|
||||||
|
// create components
|
||||||
|
JButton buttonOK = new JButton("OK");
|
||||||
|
JLabel label = new JLabel("Enter your name");
|
||||||
|
JTextField text = new JTextField("Bill Bloggs");
|
||||||
|
JCheckBox chbox = new JCheckBox("Option A");
|
||||||
|
JRadioButton rbut = new JRadioButton("Option 1");
|
||||||
|
JComboBox cbox = new JComboBox(new String [] {"red", "green", "blue"});
|
||||||
|
JList jlist = new JList(new String [] {"red", "green", "blue"});
|
||||||
|
|
||||||
|
// add components
|
||||||
|
ourWindow.add(buttonOK);
|
||||||
|
ourWindow.add(label);
|
||||||
|
ourWindow.add(text);
|
||||||
|
ourWindow.add(chbox);
|
||||||
|
ourWindow.add(rbut);
|
||||||
|
ourWindow.add(cbox);
|
||||||
|
ourWindow.add(jlist);
|
||||||
|
|
||||||
|
|
||||||
|
// set size
|
||||||
|
ourWindow.setSize(650,60); //ourWindow.pack();
|
||||||
|
// set visibility
|
||||||
|
ourWindow.setVisible(true);
|
||||||
|
ourWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.layoutmanagers;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
*/
|
||||||
|
public class ConstructorDemo {
|
||||||
|
|
||||||
|
public ConstructorDemo(String msg)
|
||||||
|
{
|
||||||
|
System.out.println("I've been constructed with "+msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConstructorDemo()
|
||||||
|
{
|
||||||
|
this("a default value");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
//ConstructorDemo z = new ConstructorDemo();
|
||||||
|
//ConstructorDemo x = new ConstructorDemo(" an elephant in a bun");
|
||||||
|
File file = new File("test.txt");
|
||||||
|
// original FileReader only code
|
||||||
|
try
|
||||||
|
{
|
||||||
|
BufferedReader bufInput1 =
|
||||||
|
new BufferedReader(new FileReader(file));
|
||||||
|
String str = bufInput1.readLine();
|
||||||
|
while (str != null)
|
||||||
|
{
|
||||||
|
//students.add(new Student(str));
|
||||||
|
ConstructorDemo c = new ConstructorDemo(str);
|
||||||
|
str = bufInput1.readLine();
|
||||||
|
|
||||||
|
}
|
||||||
|
bufInput1.close();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
try {
|
||||||
|
InputStreamReader reader =
|
||||||
|
new InputStreamReader(new FileInputStream("myfile.txt"), "UTF-8");
|
||||||
|
BufferedReader bufInput1 = new BufferedReader(reader);
|
||||||
|
} catch (FileNotFoundException ex) {
|
||||||
|
// do nothing
|
||||||
|
} catch (UnsupportedEncodingException ex)
|
||||||
|
{
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.layoutmanagers;
|
||||||
|
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.awt.event.MouseListener;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
*/
|
||||||
|
public class Controller implements ActionListener, MouseListener
|
||||||
|
{
|
||||||
|
private static Controller oneInstance ;//= new Controller();
|
||||||
|
|
||||||
|
public static Controller getInstance() {
|
||||||
|
if (oneInstance == null){
|
||||||
|
oneInstance = new Controller();
|
||||||
|
}
|
||||||
|
return oneInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// singleton pattern
|
||||||
|
private Controller()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actionPerformed(ActionEvent ae)
|
||||||
|
{
|
||||||
|
|
||||||
|
System.out.println(ae.getActionCommand());
|
||||||
|
if (ae.getActionCommand().equals("clickme")) {
|
||||||
|
GridTo500 g500 = new GridTo500();
|
||||||
|
JFrame newWindow = new JFrame("New");
|
||||||
|
newWindow.add(g500);
|
||||||
|
newWindow.setSize(500, 500);
|
||||||
|
newWindow.setVisible(true);
|
||||||
|
}
|
||||||
|
if (ae.getActionCommand().equals("abc"))
|
||||||
|
{
|
||||||
|
System.out.println("hello");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// public void actionPerformed(ActionEvent ae) {
|
||||||
|
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
|
// }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent me) {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mousePressed(MouseEvent me) {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseReleased(MouseEvent me) {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseEntered(MouseEvent me) {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseExited(MouseEvent me) {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.layoutmanagers;
|
||||||
|
|
||||||
|
import java.awt.FlowLayout;
|
||||||
|
import java.awt.Color;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
*/
|
||||||
|
public class FlowLayoutExample extends JPanel {
|
||||||
|
public FlowLayoutExample()
|
||||||
|
{
|
||||||
|
FlowLayout fl = new FlowLayout();
|
||||||
|
this.setLayout(fl);
|
||||||
|
|
||||||
|
/*JLabel component1 = new JLabel("component 1");
|
||||||
|
component1.setBackground(Color.YELLOW);
|
||||||
|
component1.setOpaque(true); */
|
||||||
|
|
||||||
|
JButton component1 = new JButton("click me");
|
||||||
|
component1.setActionCommand("clickme");
|
||||||
|
component1.addActionListener(Controller.getInstance());
|
||||||
|
|
||||||
|
JLabel component2 = new JLabel("component 2");
|
||||||
|
component2.setBackground(Color.GREEN);
|
||||||
|
component2.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component3 = new JLabel("component 3");
|
||||||
|
component3.setBackground(Color.ORANGE);
|
||||||
|
component3.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component4 = new JLabel("component 4");
|
||||||
|
component4.setBackground(Color.CYAN);
|
||||||
|
component4.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component5 = new JLabel("component 5");
|
||||||
|
component5.setBackground(Color.WHITE);
|
||||||
|
component5.setOpaque(true);
|
||||||
|
|
||||||
|
this.add(component1);
|
||||||
|
this.add(component2);
|
||||||
|
this.add(component3);
|
||||||
|
this.add(component4);
|
||||||
|
this.add(component5);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.layoutmanagers;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
*/
|
||||||
|
public class GridLayoutExample extends JPanel {
|
||||||
|
public GridLayoutExample()
|
||||||
|
{
|
||||||
|
GridLayout gl = new GridLayout(2,3);
|
||||||
|
this.setLayout(gl);
|
||||||
|
|
||||||
|
JLabel component1 = new JLabel("component 1",JLabel.CENTER);
|
||||||
|
component1.setBackground(Color.YELLOW);
|
||||||
|
component1.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component2 = new JLabel("component 2", JLabel.CENTER);
|
||||||
|
component2.setBackground(Color.GREEN);
|
||||||
|
component2.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component3 = new JLabel("component 3");
|
||||||
|
component3.setBackground(Color.ORANGE);
|
||||||
|
component3.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component4 = new JLabel("component 4");
|
||||||
|
component4.setBackground(Color.CYAN);
|
||||||
|
component4.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component5 = new JLabel("<html><h1>component 5 with BIGNESS</h1></html>");
|
||||||
|
component5.setBackground(Color.WHITE);
|
||||||
|
component5.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component6 = new JLabel("component 6");
|
||||||
|
component4.setBackground(Color.CYAN);
|
||||||
|
component4.setOpaque(true);
|
||||||
|
|
||||||
|
this.add(component1);
|
||||||
|
this.add(component2);
|
||||||
|
this.add(component3);
|
||||||
|
this.add(component4);
|
||||||
|
this.add(component5);
|
||||||
|
this.add(component6);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.layoutmanagers;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
import java.util.Random;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
*/
|
||||||
|
public class GridTo500 extends JPanel {
|
||||||
|
public GridTo500()
|
||||||
|
{
|
||||||
|
GridLayout gl = new GridLayout(0,15);
|
||||||
|
this.setLayout(gl);
|
||||||
|
|
||||||
|
for (int i = 1; i < 501; i++)
|
||||||
|
{
|
||||||
|
JLabel numberJL = new JLabel(String.valueOf(i+" "));
|
||||||
|
numberJL.setOpaque(true);
|
||||||
|
numberJL.setBackground(Color.getHSBColor(new Random().nextFloat(), 1.0F, 1.0F ));
|
||||||
|
this.add(numberJL);
|
||||||
|
}
|
||||||
|
JButton button = new JButton("spling");
|
||||||
|
// button.setActionCommand("abc");
|
||||||
|
button.setActionCommand("clickme");
|
||||||
|
button.addActionListener(Controller.getInstance());
|
||||||
|
this.add(button);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.layoutmanagers;
|
||||||
|
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
*/
|
||||||
|
public class LayoutManagers {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args the command line arguments
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// JPanel jp = new FlowLayoutExample();
|
||||||
|
//JPanel jp = new GridLayoutExample();
|
||||||
|
JPanel jp = new BorderLayoutExample();
|
||||||
|
// JPanel jp = new MigLayoutExample();
|
||||||
|
//JPanel jp = new GridTo500();
|
||||||
|
JFrame mainWindow = new JFrame("Layout Manager Demo");
|
||||||
|
System.out.println(mainWindow);
|
||||||
|
mainWindow.add(jp);
|
||||||
|
mainWindow.pack();
|
||||||
|
// mainWindow.setBounds(50,50,600,600);
|
||||||
|
mainWindow.setVisible(true);
|
||||||
|
Object o = new Object();
|
||||||
|
System.out.println(o.toString());
|
||||||
|
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.layoutmanagers;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import net.miginfocom.swing.MigLayout;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
*/
|
||||||
|
public class MigLayoutExample extends JPanel {
|
||||||
|
private JLabel component4;
|
||||||
|
public MigLayoutExample()
|
||||||
|
{
|
||||||
|
MigLayout migL = new MigLayout("wrap 2");
|
||||||
|
this.setLayout(migL);
|
||||||
|
|
||||||
|
JLabel component1 = new JLabel("component 1");
|
||||||
|
component1.setBackground(Color.YELLOW);
|
||||||
|
component1.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component2 = new JLabel("component 2");
|
||||||
|
component2.setBackground(Color.GREEN);
|
||||||
|
component2.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component3 = new JLabel("component 3");
|
||||||
|
component3.setBackground(Color.ORANGE);
|
||||||
|
component3.setOpaque(true);
|
||||||
|
|
||||||
|
component4 = new JLabel("component 4");
|
||||||
|
component4.setBackground(Color.CYAN);
|
||||||
|
component4.setOpaque(true);
|
||||||
|
|
||||||
|
JLabel component5 = new JLabel("component 5");
|
||||||
|
component5.setBackground(Color.WHITE);
|
||||||
|
component5.setOpaque(true);
|
||||||
|
|
||||||
|
this.add(component1,"width 70%");
|
||||||
|
this.add(component2,"grow");
|
||||||
|
this.add(component3);
|
||||||
|
this.add(component4);
|
||||||
|
this.add(component5,"span 2, width 100%");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package javateaching.layoutmanagers;
|
||||||
|
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author
|
||||||
|
*/
|
||||||
|
public class ScrollPaneDemo {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args the command line arguments
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//JPanel jp = new FlowLayoutExample();
|
||||||
|
//JPanel jp = new GridLayoutExample();
|
||||||
|
//JPanel jp = new BorderLayoutExample();
|
||||||
|
//JPanel jp = new MigLayoutExample();
|
||||||
|
GridTo500 g500 = new GridTo500();
|
||||||
|
JFrame mainWindow = new JFrame("ScrollPane Manager Demo");
|
||||||
|
JScrollPane js = new JScrollPane(g500);
|
||||||
|
mainWindow.add(js);
|
||||||
|
mainWindow.pack();
|
||||||
|
//mainWindow.setBounds(50,50,600,600);
|
||||||
|
mainWindow.setVisible(true);
|
||||||
|
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package javateaching.leanconsole;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.WindowConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paul
|
||||||
|
*/
|
||||||
|
public class GCDemo
|
||||||
|
{
|
||||||
|
public static void main(String[] args) throws InterruptedException
|
||||||
|
{
|
||||||
|
JFrame ourwindow = new JFrame("Yay! Window");
|
||||||
|
ourwindow.setLayout(new BorderLayout());
|
||||||
|
|
||||||
|
LeanConsoleWithInput console = new LeanConsoleWithInput();
|
||||||
|
ourwindow.add(console,BorderLayout.CENTER);
|
||||||
|
|
||||||
|
ourwindow.setSize(500,400);
|
||||||
|
ourwindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
ourwindow.setVisible(true);
|
||||||
|
|
||||||
|
console.println("Hello everyone!");
|
||||||
|
|
||||||
|
for (int i = 1; i <= 200; i++)
|
||||||
|
{
|
||||||
|
console.println("Hello times "+i);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.println("What is your name?");
|
||||||
|
String name = console.readln();
|
||||||
|
console.println("Hello, "+name);
|
||||||
|
console.println("*** end of program ***");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package javateaching.leanconsole;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.JTextArea;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paul
|
||||||
|
*/
|
||||||
|
public class LeanConsole extends JPanel
|
||||||
|
{
|
||||||
|
private JTextArea textarea = new JTextArea();
|
||||||
|
|
||||||
|
public LeanConsole()
|
||||||
|
{
|
||||||
|
this.setLayout(new BorderLayout());
|
||||||
|
JScrollPane scroll = new JScrollPane(textarea);
|
||||||
|
this.add(scroll,BorderLayout.CENTER);
|
||||||
|
textarea.setLineWrap(true); // otherwise everything is on one line
|
||||||
|
textarea.setEditable(false); // otherwise people can type over the text
|
||||||
|
}
|
||||||
|
|
||||||
|
public void print(String text)
|
||||||
|
{
|
||||||
|
String currentText = this.getTextarea().getText();
|
||||||
|
currentText += text;
|
||||||
|
this.getTextarea().setText(currentText);
|
||||||
|
this.getTextarea().setCaretPosition(this.getTextarea().getDocument().getLength());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void println(String text)
|
||||||
|
{
|
||||||
|
this.print(text+System.getProperty("line.separator"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear()
|
||||||
|
{
|
||||||
|
this.getTextarea().setText("");
|
||||||
|
}
|
||||||
|
|
||||||
|
public JTextArea getTextarea() {
|
||||||
|
return textarea;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package javateaching.leanconsole;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.KeyListener;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.JTextArea;
|
||||||
|
import javax.swing.JTextField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author paul
|
||||||
|
*/
|
||||||
|
public class LeanConsoleWithInput extends JPanel
|
||||||
|
{
|
||||||
|
private JTextArea textarea = new JTextArea();
|
||||||
|
private JTextField inputfield = new JTextField();
|
||||||
|
|
||||||
|
private boolean returnPressed = false;
|
||||||
|
|
||||||
|
public LeanConsoleWithInput()
|
||||||
|
{
|
||||||
|
this.setLayout(new BorderLayout());
|
||||||
|
JScrollPane scroll = new JScrollPane(textarea);
|
||||||
|
this.add(scroll,BorderLayout.CENTER);
|
||||||
|
textarea.setLineWrap(true); // otherwise everything is on one line
|
||||||
|
textarea.setEditable(false); // otherwise people can type over the text
|
||||||
|
// add input field at bottom
|
||||||
|
this.add(inputfield,BorderLayout.SOUTH);
|
||||||
|
inputfield.addKeyListener(new KeyListener(){
|
||||||
|
@Override
|
||||||
|
public void keyTyped(KeyEvent e) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyPressed(KeyEvent e) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
// while it might seem more natural to hook keyTyped,
|
||||||
|
// apparently getKeyCode only returns values for
|
||||||
|
// keyPressed and keyReleased.
|
||||||
|
if (e.getKeyCode() == KeyEvent.VK_ENTER) returnPressed = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void print(String text)
|
||||||
|
{
|
||||||
|
String currentText = this.getTextarea().getText();
|
||||||
|
currentText += text;
|
||||||
|
this.getTextarea().setText(currentText);
|
||||||
|
this.getTextarea().setCaretPosition(this.getTextarea().getDocument().getLength());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void println(String text)
|
||||||
|
{
|
||||||
|
this.print(text+System.getProperty("line.separator"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public String readln()
|
||||||
|
{
|
||||||
|
// clear any existing text
|
||||||
|
inputfield.setText("");
|
||||||
|
// put the cursor in the input field
|
||||||
|
inputfield.requestFocusInWindow();
|
||||||
|
this.returnPressed = false;
|
||||||
|
|
||||||
|
// returnPressed is going to get set when the
|
||||||
|
// user presses return on the KeyListener for
|
||||||
|
// the input field. We wait until it is...
|
||||||
|
while (!this.returnPressed)
|
||||||
|
{
|
||||||
|
// add in a brief pause to processing. Otherwise this loop would
|
||||||
|
// peg the CPU at 100%
|
||||||
|
try { Thread.sleep(200); } catch (InterruptedException ex) {}
|
||||||
|
}
|
||||||
|
String result = inputfield.getText();
|
||||||
|
// we've pressed return, so clear the field
|
||||||
|
inputfield.setText("");
|
||||||
|
this.returnPressed = false;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear()
|
||||||
|
{
|
||||||
|
this.getTextarea().setText("");
|
||||||
|
}
|
||||||
|
|
||||||
|
public JTextArea getTextarea() {
|
||||||
|
return textarea;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package singletonexample;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author localadmin
|
||||||
|
*/
|
||||||
|
public class MySingleton {
|
||||||
|
// this is the static attribute which contains the only object of this
|
||||||
|
// example class. being static it won't get swept up by the garbage collector
|
||||||
|
// it is null before the constructor is called
|
||||||
|
private static MySingleton uniqueInstance;
|
||||||
|
|
||||||
|
// instance variables here
|
||||||
|
// some data for our unique instance object to show you can use it
|
||||||
|
// to get model data in this case just Strings empty at first but it could be
|
||||||
|
// Collections of objects or anything
|
||||||
|
private String myUniqueInstanceData;
|
||||||
|
|
||||||
|
|
||||||
|
// this is the way of getting the unique instance
|
||||||
|
// printlines have been put in to show how it works
|
||||||
|
public static MySingleton getInstance() {
|
||||||
|
if (uniqueInstance == null) {
|
||||||
|
System.out.println("The uniqueInstance is null.");
|
||||||
|
System.out.println("Call the constructor");
|
||||||
|
uniqueInstance = new MySingleton();
|
||||||
|
}
|
||||||
|
System.out.println("We have a uniqueInstance so return it");
|
||||||
|
return uniqueInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the private constructor
|
||||||
|
private MySingleton() {
|
||||||
|
// a print line just to show when the constructor is called
|
||||||
|
// it is a private constructor and can only be called from the getInstance method
|
||||||
|
System.out.println("Creating our uniqueInstance");
|
||||||
|
}
|
||||||
|
|
||||||
|
// other useful methods here
|
||||||
|
|
||||||
|
// getters and setters for whatever data we have in our uniqueInstance
|
||||||
|
public String getMyUniqueInstanceData() {
|
||||||
|
return myUniqueInstanceData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMyUniqueInstanceData(String myUniqueInstanceData) {
|
||||||
|
this.myUniqueInstanceData = myUniqueInstanceData;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package singletonexample;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author localadmin
|
||||||
|
*/
|
||||||
|
public class SingletonExample {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args the command line arguments
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// We are going to get our uniqueInstance of a Singleton object
|
||||||
|
System.out.println("First call to get our uniqueInstance");
|
||||||
|
MySingleton theOnlyOne = MySingleton.getInstance();
|
||||||
|
System.out.println("Now I'm going to put some data in and it will be: theOnlyOne");
|
||||||
|
theOnlyOne.setMyUniqueInstanceData("theOnlyOne");
|
||||||
|
// now I am going to retrieve the data
|
||||||
|
System.out.println("Now I want to get my uniqueInstance again (I can get it from anywhere in my application)");
|
||||||
|
|
||||||
|
MySingleton theOnlyOneAgain = MySingleton.getInstance();
|
||||||
|
|
||||||
|
System.out.println("Let's see if it is the same object - what's the data?");
|
||||||
|
|
||||||
|
System.out.println(theOnlyOneAgain.getMyUniqueInstanceData());
|
||||||
|
|
||||||
|
System.out.println("Hey man it's the same object! That was easy :-) No more passing the object all over the place. Patterns rule!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user