SwingCodeExamplesNB as a Maven Netbeans Project

This commit is contained in:
David Livingstone committed 2024-01-11 09:39:02 +00:00
1 parent bd5d9d6304
commit e0a3b009b6
30 files changed
+1428

No files matched your search

@@ -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);
}
}