add files
This commit is contained in:
parent
066116abdd
commit
dc3aba9d95
@ -1,2 +1,3 @@
|
|||||||
# SwingDemos
|
# SwingDemos
|
||||||
|
|
||||||
|
Not written by me but useful reference
|
116
src/GUIDemo.java
Normal file
116
src/GUIDemo.java
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
import javax.swing.*; // make the Swing GUI classes available
|
||||||
|
import java.awt.*; // used for Color and GridLayout classes
|
||||||
|
import java.awt.event.*; // used for ActionEvent and ActionListener classes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This simple program demonstrates several GUI components that are available in the
|
||||||
|
* Swing GUI library. The program shows a window containing a button, a text input
|
||||||
|
* box, a combo box (pop-up menu), and a text area. The text area is used for
|
||||||
|
* a "transcript" that records interactions of the user with the other components.
|
||||||
|
*/
|
||||||
|
public class GUIDemo extends JPanel implements ActionListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This main routine allows this class to be run as an application. The main
|
||||||
|
* routine simply creates a window containing a panel of type GUIDemo.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame window = new JFrame("GUI Demo");
|
||||||
|
window.setContentPane( new GUIDemo() );
|
||||||
|
window.pack();
|
||||||
|
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
window.setResizable(false);
|
||||||
|
window.setLocation(150,100);
|
||||||
|
window.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
private JTextArea transcript; // a message will be posted to this text area
|
||||||
|
// each time an event is generated by some
|
||||||
|
// some user action
|
||||||
|
|
||||||
|
private JComboBox<String> combobox; // The pop-up menu.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This constructor adds several GUI components to the panel and sets
|
||||||
|
* itself up to listen for action events from some of them.
|
||||||
|
*/
|
||||||
|
public GUIDemo() {
|
||||||
|
|
||||||
|
setBorder(BorderFactory.createLineBorder(Color.GRAY, 3));
|
||||||
|
setBackground(Color.WHITE);
|
||||||
|
|
||||||
|
setLayout(new GridLayout(1, 2, 3, 3));
|
||||||
|
// I will put the transcript area in the right half of the
|
||||||
|
// panel. The left half will be occupied by a grid of
|
||||||
|
// four lines. Each line contains a component and
|
||||||
|
// a label for that component.
|
||||||
|
|
||||||
|
transcript = new JTextArea();
|
||||||
|
transcript.setEditable(false);
|
||||||
|
transcript.setMargin(new Insets(4, 4, 4, 4));
|
||||||
|
JPanel left = new JPanel();
|
||||||
|
left.setLayout(new GridLayout(4, 2, 3, 10));
|
||||||
|
left.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
|
||||||
|
add(left);
|
||||||
|
add(new JScrollPane(transcript));
|
||||||
|
|
||||||
|
JLabel lab = new JLabel("Push Button: ", JLabel.RIGHT);
|
||||||
|
left.add(lab);
|
||||||
|
JButton b = new JButton("Click Me!");
|
||||||
|
b.addActionListener(this);
|
||||||
|
left.add(b);
|
||||||
|
|
||||||
|
lab = new JLabel("Checkbox: ", JLabel.RIGHT);
|
||||||
|
left.add(lab);
|
||||||
|
JCheckBox c = new JCheckBox("Click me!");
|
||||||
|
c.addActionListener(this);
|
||||||
|
left.add(c);
|
||||||
|
|
||||||
|
lab = new JLabel("Text Field: ", JLabel.RIGHT);
|
||||||
|
left.add(lab);
|
||||||
|
JTextField t = new JTextField("Type here!");
|
||||||
|
t.addActionListener(this);
|
||||||
|
left.add(t);
|
||||||
|
|
||||||
|
lab = new JLabel("Pop-up Menu: ", JLabel.RIGHT);
|
||||||
|
left.add(lab);
|
||||||
|
combobox = new JComboBox<String>();
|
||||||
|
combobox.addItem("First Option");
|
||||||
|
combobox.addItem("Second Option");
|
||||||
|
combobox.addItem("Third Option");
|
||||||
|
combobox.addItem("Fourth Option");
|
||||||
|
combobox.addActionListener(this);
|
||||||
|
left.add(combobox);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void post(String message) { // add a line to the transcript
|
||||||
|
transcript.append(message + "\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Respond to an ActionEvent from one of the GUI components in the panel.
|
||||||
|
* In each case, a message about the event is posted to the transcript.
|
||||||
|
* (This method is part of the ActionListener interface.)
|
||||||
|
*/
|
||||||
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
Object target = evt.getSource(); // which component produced this event?
|
||||||
|
if (target instanceof JButton) {
|
||||||
|
post("Button was clicked.");
|
||||||
|
} else if (target instanceof JTextField) {
|
||||||
|
post("Pressed return in TextField\nwith contents:\n "
|
||||||
|
+ evt.getActionCommand());
|
||||||
|
} else if (target instanceof JCheckBox) {
|
||||||
|
if (((JCheckBox) target).isSelected())
|
||||||
|
post("Checkbox was turned on.");
|
||||||
|
else
|
||||||
|
post("Checkbox was turned off.");
|
||||||
|
} else if (target == combobox) {
|
||||||
|
Object item = combobox.getSelectedItem();
|
||||||
|
post("Item \"" + item + "\" selected\nfrom pop-up menu.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end class GUIDemo
|
9
src/HelloWorldGUI1.java
Normal file
9
src/HelloWorldGUI1.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
|
public class HelloWorldGUI1 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JOptionPane.showMessageDialog( null, "Hello World!" );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
40
src/HelloWorldGUI2.java
Normal file
40
src/HelloWorldGUI2.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class HelloWorldGUI2 {
|
||||||
|
|
||||||
|
private static class HelloWorldDisplay extends JPanel {
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
g.drawString( "Hello World!", 20, 30 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ButtonHandler implements ActionListener {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
HelloWorldDisplay displayPanel = new HelloWorldDisplay();
|
||||||
|
JButton okButton = new JButton("OK");
|
||||||
|
ButtonHandler listener = new ButtonHandler();
|
||||||
|
okButton.addActionListener(listener);
|
||||||
|
|
||||||
|
JPanel content = new JPanel();
|
||||||
|
content.setLayout(new BorderLayout());
|
||||||
|
content.add(displayPanel, BorderLayout.CENTER);
|
||||||
|
content.add(okButton, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
JFrame window = new JFrame("GUI Test");
|
||||||
|
window.setContentPane(content);
|
||||||
|
window.setSize(250,100);
|
||||||
|
window.setLocation(100,100);
|
||||||
|
window.setVisible(true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
38
src/HelloWorldGUI3.java
Normal file
38
src/HelloWorldGUI3.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create and show a window that displays the message "Hello World",
|
||||||
|
* with an "OK" button. The program ends when the user clicks the button.
|
||||||
|
* The GUI is constructed in the main() routine, which is not the best
|
||||||
|
* way to do things. This program is a first example of using GUI components.
|
||||||
|
*/
|
||||||
|
public class HelloWorldGUI3 {
|
||||||
|
|
||||||
|
private static class HelloWorldDisplay extends JPanel {
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
g.drawString( "Hello World!", 20, 30 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
HelloWorldDisplay displayPanel = new HelloWorldDisplay();
|
||||||
|
JButton okButton = new JButton("OK");
|
||||||
|
okButton.addActionListener( e -> System.exit(0) );
|
||||||
|
|
||||||
|
JPanel content = new JPanel();
|
||||||
|
content.setLayout(new BorderLayout());
|
||||||
|
content.add(displayPanel, BorderLayout.CENTER);
|
||||||
|
content.add(okButton, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
JFrame window = new JFrame("GUI Test");
|
||||||
|
window.setContentPane(content);
|
||||||
|
window.setSize(250,100);
|
||||||
|
window.setLocation(100,100);
|
||||||
|
window.setVisible(true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
155
src/NullLayoutDemo.java
Normal file
155
src/NullLayoutDemo.java
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
|
||||||
|
/**
|
||||||
|
* This program demonstrates how to do your own layout. The layout
|
||||||
|
* manager for the panel is set to null. The setBounds() method
|
||||||
|
* of each component is called to set the size and position of the
|
||||||
|
* component.
|
||||||
|
*
|
||||||
|
* It is assumed that the panel is 520 pixels wide and 420 pixels high!
|
||||||
|
* If you want to deal with other sizes, you should implement the
|
||||||
|
* ComponentListener interface, and compute the sizes and positions of
|
||||||
|
* the components in terms of the actual width and height of the panel
|
||||||
|
* in response to a resize event.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class NullLayoutDemo extends JPanel implements ActionListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main routine just opens a window that shows a NullLayoutDemo
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame window = new JFrame("Null Layout Demo");
|
||||||
|
NullLayoutDemo content = new NullLayoutDemo();
|
||||||
|
window.setContentPane(content);
|
||||||
|
window.pack(); // Use preferred size of content to set size of window.
|
||||||
|
window.setResizable(false); // User can't change the window's size.
|
||||||
|
window.setLocation(100,100);
|
||||||
|
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
|
||||||
|
window.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
Checkerboard board; // A checkerboard. The Checkerboard class is
|
||||||
|
// nested inside the NullLayoutDemo class.
|
||||||
|
|
||||||
|
JButton resignButton; // Two buttons.
|
||||||
|
JButton newGameButton;
|
||||||
|
|
||||||
|
JLabel message; // A label for displaying messages to the user.
|
||||||
|
|
||||||
|
int clickCount; // Counts how many times the button was clicked.
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The constructor sets the layout manager for the panel to be null.
|
||||||
|
* It adds four components to the panel and sets their bounds explicitly.
|
||||||
|
* It is assumed that the panel has a fixed size of 520-by-420 pixels;
|
||||||
|
* it sets the preferred size of the panel to be that size.
|
||||||
|
*/
|
||||||
|
public NullLayoutDemo() {
|
||||||
|
|
||||||
|
setLayout(null); // I will do the layout myself!
|
||||||
|
|
||||||
|
setBackground(new Color(0,120,0)); // A dark green background.
|
||||||
|
|
||||||
|
setBorder( BorderFactory.createEtchedBorder() );
|
||||||
|
|
||||||
|
setPreferredSize( new Dimension(520,420) );
|
||||||
|
|
||||||
|
/* Create the components and add them to this panel. If you
|
||||||
|
don't add them to a container, they won't appear, even if
|
||||||
|
you set their bounds! */
|
||||||
|
|
||||||
|
board = new Checkerboard();
|
||||||
|
// (Checkerboard is a subclass of JPanel, defined below as a static
|
||||||
|
// nested class inside the main class.)
|
||||||
|
add(board);
|
||||||
|
|
||||||
|
newGameButton = new JButton("New Game");
|
||||||
|
newGameButton.addActionListener(this);
|
||||||
|
add(newGameButton);
|
||||||
|
|
||||||
|
resignButton = new JButton("Resign");
|
||||||
|
resignButton.addActionListener(this);
|
||||||
|
add(resignButton);
|
||||||
|
|
||||||
|
message = new JLabel("Click \"New Game\" to begin.");
|
||||||
|
message.setForeground( new Color(100,255,100) ); // Light green.
|
||||||
|
message.setFont(new Font("Serif", Font.BOLD, 14));
|
||||||
|
add(message);
|
||||||
|
|
||||||
|
/* Set the position and size of each component by calling
|
||||||
|
its setBounds() method. */
|
||||||
|
|
||||||
|
board.setBounds(20,20,324,324);
|
||||||
|
newGameButton.setBounds(370, 120, 120, 30);
|
||||||
|
resignButton.setBounds(370, 200, 120, 30);
|
||||||
|
message.setBounds(20, 370, 380, 30);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Respond to a click on the button by changing the displayed message.
|
||||||
|
* The message tells how many times the button has been clicked.
|
||||||
|
*/
|
||||||
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
String buttonText = evt.getActionCommand();
|
||||||
|
clickCount++;
|
||||||
|
if (clickCount == 1)
|
||||||
|
message.setText("First click: \"" + buttonText + "\" was clicked.");
|
||||||
|
else
|
||||||
|
message.setText("Click no. " + clickCount + ": \""
|
||||||
|
+ buttonText + "\" was clicked.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This canvas displays a 320-by-320 checkerboard pattern with
|
||||||
|
* a 2-pixel black border. It is assumed that the size of the
|
||||||
|
* board is set to exactly 324-by-324 pixels. This size is
|
||||||
|
* set as the preferred size of the board.
|
||||||
|
*/
|
||||||
|
private static class Checkerboard extends JPanel {
|
||||||
|
|
||||||
|
public Checkerboard() {
|
||||||
|
setPreferredSize( new Dimension(324, 324) );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
|
||||||
|
// Draw a 2-pixel black border around the edges of the board.
|
||||||
|
// (There is no need to call super.paintComponent() since
|
||||||
|
// this method paints the entire surface of the component.)
|
||||||
|
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
|
||||||
|
g.drawRect(1, 1, getSize().width - 3, getSize().height - 3);
|
||||||
|
|
||||||
|
// Draw checkerboard pattern in gray and lightGray.
|
||||||
|
|
||||||
|
for (int row = 0; row < 8; row++) {
|
||||||
|
for (int col = 0; col < 8; col++) {
|
||||||
|
if ( row % 2 == col % 2 )
|
||||||
|
g.setColor(Color.LIGHT_GRAY);
|
||||||
|
else
|
||||||
|
g.setColor(Color.GRAY);
|
||||||
|
g.fillRect(2 + col*40, 2 + row*40, 40, 40);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end nested class Checkerboard
|
||||||
|
|
||||||
|
|
||||||
|
} // end class NullLayoutDemo
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
15
src/RandomStrings.java
Normal file
15
src/RandomStrings.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import javax.swing.JFrame;
|
||||||
|
|
||||||
|
public class RandomStrings {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame window = new JFrame("Java!");
|
||||||
|
RandomStringsPanel content = new RandomStringsPanel();
|
||||||
|
window.setContentPane(content);
|
||||||
|
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
window.setLocation(120,70);
|
||||||
|
window.setSize(350,250);
|
||||||
|
window.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
101
src/RandomStringsPanel.java
Normal file
101
src/RandomStringsPanel.java
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This panel displays 25 copies of a message. The color and
|
||||||
|
* position of each message is selected at random. The font
|
||||||
|
* of each message is randomly chosen from among five possible
|
||||||
|
* fonts. The messages are displayed on a black background.
|
||||||
|
* Note: The style of drawing used here is poor, because every
|
||||||
|
* time the paintComponent() method is called, new random values are
|
||||||
|
* used. This means that a different picture will be drawn each time.
|
||||||
|
*/
|
||||||
|
public class RandomStringsPanel extends JPanel {
|
||||||
|
|
||||||
|
private String message; // The message to be displayed. This can be set in
|
||||||
|
// the constructor. If no value is provided in the
|
||||||
|
// constructor, then the string "Java!" is used.
|
||||||
|
|
||||||
|
private Font font1, font2, font3, font4, font5; // The five fonts.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default constructor creates a panel that displays the message "Java!".
|
||||||
|
*/
|
||||||
|
public RandomStringsPanel() {
|
||||||
|
this(null); // Call the other constructor, with parameter null.
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor creates a panel to display 25 copies of a specified message.
|
||||||
|
* @param messageString The message to be displayed. If this is null,
|
||||||
|
* then the default message "Java!" is displayed.
|
||||||
|
*/
|
||||||
|
public RandomStringsPanel(String messageString) {
|
||||||
|
|
||||||
|
message = messageString;
|
||||||
|
if (message == null)
|
||||||
|
message = "Java!";
|
||||||
|
|
||||||
|
font1 = new Font("Serif", Font.BOLD, 14);
|
||||||
|
font2 = new Font("SansSerif", Font.BOLD + Font.ITALIC, 24);
|
||||||
|
font3 = new Font("Monospaced", Font.PLAIN, 30);
|
||||||
|
font4 = new Font("Dialog", Font.PLAIN, 36);
|
||||||
|
font5 = new Font("Serif", Font.ITALIC, 48);
|
||||||
|
|
||||||
|
setBackground(Color.BLACK);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The paintComponent method is responsible for drawing the content of the panel.
|
||||||
|
* It draws 25 copies of the message string, using a random color, font, and
|
||||||
|
* position for each string.
|
||||||
|
*/
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
|
||||||
|
super.paintComponent(g); // Call the paintComponent method from the
|
||||||
|
// superclass, JPanel. This simply fills the
|
||||||
|
// entire panel with the background color, black.
|
||||||
|
|
||||||
|
Graphics2D g2 = (Graphics2D)g; // (To make the text smoother.)
|
||||||
|
g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
|
||||||
|
RenderingHints.VALUE_ANTIALIAS_ON );
|
||||||
|
|
||||||
|
int width = getWidth();
|
||||||
|
int height = getHeight();
|
||||||
|
|
||||||
|
for (int i = 0; i < 25; i++) {
|
||||||
|
|
||||||
|
// Draw one string. First, set the font to be one of the five
|
||||||
|
// available fonts, at random.
|
||||||
|
|
||||||
|
int fontNum = (int)(5*Math.random()) + 1;
|
||||||
|
switch (fontNum) {
|
||||||
|
case 1 -> g.setFont(font1);
|
||||||
|
case 2 -> g.setFont(font2);
|
||||||
|
case 3 -> g.setFont(font3);
|
||||||
|
case 4 -> g.setFont(font4);
|
||||||
|
case 5 -> g.setFont(font5);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the color to a bright, saturated color, with random hue.
|
||||||
|
|
||||||
|
float hue = (float)Math.random();
|
||||||
|
g.setColor( Color.getHSBColor(hue, 1.0F, 1.0F) );
|
||||||
|
|
||||||
|
// Select the position of the string, at random.
|
||||||
|
|
||||||
|
int x,y;
|
||||||
|
x = -50 + (int)(Math.random()*(width+40));
|
||||||
|
y = (int)(Math.random()*(height+20));
|
||||||
|
|
||||||
|
// Draw the message.
|
||||||
|
|
||||||
|
g.drawString(message,x,y);
|
||||||
|
|
||||||
|
} // end for
|
||||||
|
|
||||||
|
} // end paintComponent()
|
||||||
|
|
||||||
|
|
||||||
|
} // end class RandomStringsPanel
|
179
src/SimpleCalc.java
Normal file
179
src/SimpleCalc.java
Normal file
@ -0,0 +1,179 @@
|
|||||||
|
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In this program, the user can type in two real numbers. The
|
||||||
|
* user can click on buttons labeled +, - , *, and / to perform
|
||||||
|
* basic arithmetic operations on the numbers. When the user
|
||||||
|
* clicks on a button the answer is displayed.
|
||||||
|
* This class also contains a main() routine, so that
|
||||||
|
* it can be run as a stand-alone application.
|
||||||
|
*/
|
||||||
|
public class SimpleCalc extends JPanel implements ActionListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This main() routine makes it possible to run the SimpleCalc class
|
||||||
|
* as a stand-alone application.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame window = new JFrame("Simple Calculator");
|
||||||
|
SimpleCalc content = new SimpleCalc();
|
||||||
|
window.setContentPane(content);
|
||||||
|
window.pack(); // Sizes window to preferred size of contents.
|
||||||
|
window.setLocation(100,100);
|
||||||
|
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
|
||||||
|
window.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
private JTextField xInput, yInput; // Input boxes for the numbers.
|
||||||
|
|
||||||
|
private JLabel answer; // JLabel for displaying the answer, or an
|
||||||
|
// error message if appropriate.
|
||||||
|
|
||||||
|
|
||||||
|
public SimpleCalc() {
|
||||||
|
|
||||||
|
/* Assign a background color to the panel and its
|
||||||
|
content panel. This color will show through in the gaps
|
||||||
|
between components. */
|
||||||
|
|
||||||
|
setBackground(Color.GRAY);
|
||||||
|
|
||||||
|
/* Add an empty border around the panel, which will also
|
||||||
|
* appear in the gray background color. */
|
||||||
|
|
||||||
|
setBorder( BorderFactory.createEmptyBorder(5,5,5,5) );
|
||||||
|
|
||||||
|
/* Create the input boxes, and make sure that the background
|
||||||
|
color is white. (They are likely to be white by default.) */
|
||||||
|
|
||||||
|
xInput = new JTextField("0", 10);
|
||||||
|
xInput.setBackground(Color.WHITE);
|
||||||
|
yInput = new JTextField("0", 10);
|
||||||
|
yInput.setBackground(Color.WHITE);
|
||||||
|
|
||||||
|
/* Create panels to hold the input boxes and labels "x =" and
|
||||||
|
"y = ". These panels use the default FlowLayout layout manager. */
|
||||||
|
|
||||||
|
JPanel xPanel = new JPanel();
|
||||||
|
xPanel.add( new JLabel(" x = "));
|
||||||
|
xPanel.add(xInput);
|
||||||
|
|
||||||
|
JPanel yPanel = new JPanel();
|
||||||
|
yPanel.add( new JLabel(" y = "));
|
||||||
|
yPanel.add(yInput);
|
||||||
|
|
||||||
|
/* Create a panel to hold the four buttons for the four
|
||||||
|
operations. A GridLayout is used so that the buttons
|
||||||
|
will all have the same size and will fill the panel.
|
||||||
|
The main panel serves as ActionListener for the buttons. */
|
||||||
|
|
||||||
|
JPanel buttonPanel = new JPanel();
|
||||||
|
buttonPanel.setLayout(new GridLayout(1,4));
|
||||||
|
|
||||||
|
JButton plus = new JButton("+");
|
||||||
|
plus.addActionListener(this);
|
||||||
|
buttonPanel.add(plus);
|
||||||
|
|
||||||
|
JButton minus = new JButton("-");
|
||||||
|
minus.addActionListener(this);
|
||||||
|
buttonPanel.add(minus);
|
||||||
|
|
||||||
|
JButton times = new JButton("*");
|
||||||
|
times.addActionListener(this);
|
||||||
|
buttonPanel.add(times);
|
||||||
|
|
||||||
|
JButton divide = new JButton("/");
|
||||||
|
divide.addActionListener(this);
|
||||||
|
buttonPanel.add(divide);
|
||||||
|
|
||||||
|
/* Create the label for displaying the answer in red
|
||||||
|
on a white background. The label is set to be
|
||||||
|
"opaque" to make sure that the white background
|
||||||
|
is painted. */
|
||||||
|
|
||||||
|
answer = new JLabel("x + y = 0", JLabel.CENTER);
|
||||||
|
answer.setForeground(Color.RED);
|
||||||
|
answer.setBackground(Color.WHITE);
|
||||||
|
answer.setOpaque(true);
|
||||||
|
|
||||||
|
/* Set up the layout for the main panel, using a GridLayout,
|
||||||
|
and add all the components that have been created. */
|
||||||
|
|
||||||
|
setLayout(new GridLayout(4,1,3,3));
|
||||||
|
add(xPanel);
|
||||||
|
add(yPanel);
|
||||||
|
add(buttonPanel);
|
||||||
|
add(answer);
|
||||||
|
|
||||||
|
} // end constructor
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When the user clicks a button, get the numbers from the input boxes
|
||||||
|
* and perform the operation indicated by the button. Put the result in
|
||||||
|
* the answer label. If an error occurs, an error message is put in the label.
|
||||||
|
*/
|
||||||
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
|
||||||
|
double x, y; // The numbers from the input boxes.
|
||||||
|
|
||||||
|
/* Get a number from the xInput JTextField. Use
|
||||||
|
xInput.getText() to get its contents as a String.
|
||||||
|
Convert this String to a double. The try...catch
|
||||||
|
statement will check for errors in the String. If
|
||||||
|
the string is not a legal number, the error message
|
||||||
|
"Illegal data for x." is put into the answer and
|
||||||
|
the actionPerformed() method ends. */
|
||||||
|
|
||||||
|
try {
|
||||||
|
String xStr = xInput.getText();
|
||||||
|
x = Double.parseDouble(xStr);
|
||||||
|
}
|
||||||
|
catch (NumberFormatException e) {
|
||||||
|
// The string xStr is not a legal number.
|
||||||
|
answer.setText("Illegal data for x.");
|
||||||
|
xInput.requestFocusInWindow();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get a number from yInput in the same way. */
|
||||||
|
|
||||||
|
try {
|
||||||
|
String yStr = yInput.getText();
|
||||||
|
y = Double.parseDouble(yStr);
|
||||||
|
}
|
||||||
|
catch (NumberFormatException e) {
|
||||||
|
answer.setText("Illegal data for y.");
|
||||||
|
yInput.requestFocusInWindow();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Perform the operation based on the action command
|
||||||
|
from the button. Note that division by zero produces
|
||||||
|
an error message. */
|
||||||
|
|
||||||
|
String op = evt.getActionCommand();
|
||||||
|
if (op.equals("+"))
|
||||||
|
answer.setText( "x + y = " + (x+y) );
|
||||||
|
else if (op.equals("-"))
|
||||||
|
answer.setText( "x - y = " + (x-y) );
|
||||||
|
else if (op.equals("*"))
|
||||||
|
answer.setText( "x * y = " + (x*y) );
|
||||||
|
else if (op.equals("/")) {
|
||||||
|
if (y == 0)
|
||||||
|
answer.setText("Can't divide by zero!");
|
||||||
|
else
|
||||||
|
answer.setText( "x / y = " + (x/y) );
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end actionPerformed()
|
||||||
|
|
||||||
|
|
||||||
|
} // end class SimpleCalc
|
155
src/SimpleDialogDemo.java
Normal file
155
src/SimpleDialogDemo.java
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
|
||||||
|
/**
|
||||||
|
* This program demonstrates four easy-to-use routines for showing
|
||||||
|
* a dialog box and, in three cases, getting back some information
|
||||||
|
* from the user. The methods are:
|
||||||
|
*
|
||||||
|
* JOptionPane.showMessageDialog
|
||||||
|
* JOptionPane.showConfirmDialog
|
||||||
|
* JOptionPane.showInputDialog
|
||||||
|
* JColorChooser.showDialog
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class SimpleDialogDemo extends JPanel implements ActionListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A main routine allows this class to be run as an application.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame window = new JFrame("Four Simple Dialogs");
|
||||||
|
SimpleDialogDemo content = new SimpleDialogDemo();
|
||||||
|
window.setContentPane(content);
|
||||||
|
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
window.setLocation(250,200);
|
||||||
|
window.pack();
|
||||||
|
window.setResizable(false);
|
||||||
|
window.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
JLabel message; // A label for giving some feedback to the user.
|
||||||
|
// It appears at the top of the panel.
|
||||||
|
|
||||||
|
Color selectedColor = Color.GRAY; // This color will be used as the
|
||||||
|
// initial color in the color chooser.
|
||||||
|
// It is used to remember the user's
|
||||||
|
// color choice, so that the color
|
||||||
|
// chooser can show the same color,
|
||||||
|
// if it is opened twice.
|
||||||
|
|
||||||
|
|
||||||
|
public SimpleDialogDemo() {
|
||||||
|
// Set up the panel with a message label and four buttons.
|
||||||
|
// Each button will open a different type of dialog.
|
||||||
|
|
||||||
|
setBackground(Color.GRAY);
|
||||||
|
setBackground(Color.GRAY);
|
||||||
|
setLayout( new GridLayout(3,1,3,3) );
|
||||||
|
message = new JLabel("Click a button to open a dialog", JLabel.CENTER);
|
||||||
|
message.setForeground(new Color(180,0,0));
|
||||||
|
message.setBackground(Color.WHITE);
|
||||||
|
message.setOpaque(true);
|
||||||
|
add(message);
|
||||||
|
|
||||||
|
JPanel buttonBar;
|
||||||
|
JButton button;
|
||||||
|
|
||||||
|
buttonBar = new JPanel();
|
||||||
|
buttonBar.setLayout(new GridLayout(1,2,3,3));
|
||||||
|
buttonBar.setBackground(Color.GRAY);
|
||||||
|
add(buttonBar);
|
||||||
|
button = new JButton("Message Dialog");
|
||||||
|
button.addActionListener(this);
|
||||||
|
buttonBar.add(button);
|
||||||
|
button = new JButton("Confirm Dialog");
|
||||||
|
button.addActionListener(this);
|
||||||
|
buttonBar.add(button);
|
||||||
|
|
||||||
|
buttonBar = new JPanel();
|
||||||
|
buttonBar.setLayout(new GridLayout(1,2,3,3));
|
||||||
|
buttonBar.setBackground(Color.GRAY);
|
||||||
|
add(buttonBar);
|
||||||
|
button = new JButton("Input Dialog");
|
||||||
|
button.addActionListener(this);
|
||||||
|
buttonBar.add(button);
|
||||||
|
button = new JButton("Color Chooser");
|
||||||
|
button.addActionListener(this);
|
||||||
|
buttonBar.add(button);
|
||||||
|
|
||||||
|
setBorder(BorderFactory.createLineBorder(Color.GRAY,3));
|
||||||
|
|
||||||
|
} // end constructor
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Respond to a button click by showing a dialog and setting the
|
||||||
|
* message label to describe the user's response.
|
||||||
|
*/
|
||||||
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
|
||||||
|
String command = evt.getActionCommand();
|
||||||
|
|
||||||
|
if (command.equals("Message Dialog")) {
|
||||||
|
message.setText("Displaying message dialog.");
|
||||||
|
JOptionPane.showMessageDialog(this,
|
||||||
|
"This is an example of JOptionPane.showMessageDialog.");
|
||||||
|
message.setText("You closed the message dialog.");
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (command.equals("Confirm Dialog")) {
|
||||||
|
message.setText("Displaying confirm dialog.");
|
||||||
|
int response = JOptionPane.showConfirmDialog(this,
|
||||||
|
"This is an example of JOptionPane.showConfirmDialog.\n"
|
||||||
|
+ "Click any button to indicate your response.");
|
||||||
|
switch(response) {
|
||||||
|
case JOptionPane.YES_OPTION:
|
||||||
|
message.setText("You clicked \"Yes\".");
|
||||||
|
break;
|
||||||
|
case JOptionPane.NO_OPTION:
|
||||||
|
message.setText("You clicked \"No\".");
|
||||||
|
break;
|
||||||
|
case JOptionPane.CANCEL_OPTION:
|
||||||
|
message.setText("You clicked \"Cancel\".");
|
||||||
|
break;
|
||||||
|
case JOptionPane.CLOSED_OPTION:
|
||||||
|
message.setText("You closed the box without making a selection.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (command.equals("Input Dialog")) {
|
||||||
|
message.setText("Displaying input dialog.");
|
||||||
|
String response = JOptionPane.showInputDialog(this,
|
||||||
|
"This is an example of JOptionPane.showInputDialog.\n"
|
||||||
|
+ "Type your response, and click a button.");
|
||||||
|
if (response == null)
|
||||||
|
message.setText("You canceled the input.");
|
||||||
|
else if (response.trim().length() == 0)
|
||||||
|
message.setText("You left the input box empty.");
|
||||||
|
else
|
||||||
|
message.setText("You entered \"" + response + "\".");
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (command.equals("Color Chooser")) {
|
||||||
|
message.setText("Displaying color chooser dialog.");
|
||||||
|
Color c = JColorChooser.showDialog(this,"Select a Color",selectedColor);
|
||||||
|
if (c == null)
|
||||||
|
message.setText("You canceled without selecting a color.");
|
||||||
|
else {
|
||||||
|
selectedColor = c; // Remember selected color for next time.
|
||||||
|
int r = c.getRed();
|
||||||
|
int g = c.getGreen();
|
||||||
|
int b = c.getBlue();
|
||||||
|
message.setText("You selected RGB = (" + r + "," + g + "," + b + ").");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end actionPerformed()
|
||||||
|
|
||||||
|
|
||||||
|
} // end class SimpleDialogDemo
|
||||||
|
|
138
src/SliderAndButtonDemo.java
Normal file
138
src/SliderAndButtonDemo.java
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Font;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.event.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This program is a simple demo of using a JLabel, some JSliders, and some buttons
|
||||||
|
* and laying them out in a GridLayout. This class contains a main() routine,
|
||||||
|
* so that it can also be run as a stand-alone application.
|
||||||
|
* */
|
||||||
|
public class SliderAndButtonDemo extends JPanel implements ActionListener, ChangeListener {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main() routine simply opens a window that shows a panel of type
|
||||||
|
* SliderAndButtonDemo. Note that the main() routine uses the pack()
|
||||||
|
* method of the JFrame, so that the size of the panel will be
|
||||||
|
* equal to its preferred size.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame window = new JFrame("Slider and Button Demo");
|
||||||
|
SliderAndButtonDemo content = new SliderAndButtonDemo();
|
||||||
|
window.setContentPane(content);
|
||||||
|
window.pack();
|
||||||
|
window.setLocation(100,100);
|
||||||
|
window.setResizable(false);
|
||||||
|
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
|
||||||
|
window.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
/* Declare some controls as global variables so that they can be used
|
||||||
|
* in the event-handling methods. Note that the buttons don't have to
|
||||||
|
* be global.
|
||||||
|
*/
|
||||||
|
|
||||||
|
private JLabel displayLabel; // label displaying "Hello World
|
||||||
|
private JSlider bgColorSlider; // controls background color of label
|
||||||
|
private JSlider fgColorSlider; // controls foreground color of label
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The constructor creates components, sets up listening, and adds
|
||||||
|
* the components to the panel.
|
||||||
|
*/
|
||||||
|
public SliderAndButtonDemo() {
|
||||||
|
|
||||||
|
setBorder(BorderFactory.createEmptyBorder(6,6,6,6));
|
||||||
|
|
||||||
|
/* Create the display label, with properties to match the
|
||||||
|
values of the sliders and with a BOLD font. */
|
||||||
|
|
||||||
|
displayLabel = new JLabel("Hello World!", JLabel.CENTER);
|
||||||
|
displayLabel.setOpaque(true);
|
||||||
|
displayLabel.setBackground( new Color(100,100,100) );
|
||||||
|
displayLabel.setForeground( Color.RED );
|
||||||
|
displayLabel.setFont( new Font("Serif", Font.BOLD, 30) );
|
||||||
|
displayLabel.setBorder(BorderFactory.createEmptyBorder(0,8,0,8));
|
||||||
|
|
||||||
|
/* Create the sliders, and set up the panel to listen for
|
||||||
|
ChangeEvents that are generated by the sliders. */
|
||||||
|
|
||||||
|
bgColorSlider = new JSlider(0,255,100);
|
||||||
|
bgColorSlider.addChangeListener(this);
|
||||||
|
|
||||||
|
fgColorSlider = new JSlider(0,100,0);
|
||||||
|
fgColorSlider.addChangeListener(this);
|
||||||
|
|
||||||
|
/* Create three buttons to control the font style, and set up the
|
||||||
|
panel to listen for ActionEvents from the buttons. */
|
||||||
|
|
||||||
|
JButton plainButton = new JButton("Plain Font");
|
||||||
|
plainButton.addActionListener(this);
|
||||||
|
JButton italicButton = new JButton("Italic Font");
|
||||||
|
italicButton.addActionListener(this);
|
||||||
|
JButton boldButton = new JButton("Bold Font");
|
||||||
|
boldButton.addActionListener(this);
|
||||||
|
|
||||||
|
|
||||||
|
/* Set the layout for the panel, and add the six components.
|
||||||
|
Use a GridLayout with 3 rows and 2 columns, and with
|
||||||
|
5 pixels between components. */
|
||||||
|
|
||||||
|
setLayout(new GridLayout(3,2,5,5));
|
||||||
|
add(displayLabel);
|
||||||
|
add(plainButton);
|
||||||
|
add(bgColorSlider);
|
||||||
|
add(italicButton);
|
||||||
|
add(fgColorSlider);
|
||||||
|
add(boldButton);
|
||||||
|
|
||||||
|
} // end constructor
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be called when the user clicks one of the buttons.
|
||||||
|
* The method just changes the label's font to the style specified by
|
||||||
|
* the button.
|
||||||
|
*/
|
||||||
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
String cmd = evt.getActionCommand();
|
||||||
|
if (cmd.equals("Plain Font")) {
|
||||||
|
displayLabel.setFont( new Font("Serif", Font.PLAIN, 30) );
|
||||||
|
}
|
||||||
|
else if (cmd.equals("Italic Font")) {
|
||||||
|
displayLabel.setFont( new Font("Serif", Font.ITALIC, 30) );
|
||||||
|
}
|
||||||
|
else if (cmd.equals("Bold Font")) {
|
||||||
|
displayLabel.setFont( new Font("Serif", Font.BOLD, 30) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called when the value is changed on either of the
|
||||||
|
* sliders. It sets the foreground or background color of the label
|
||||||
|
* to match the value on the slider that has changed.
|
||||||
|
*/
|
||||||
|
public void stateChanged(ChangeEvent evt) {
|
||||||
|
if (evt.getSource() == bgColorSlider) {
|
||||||
|
int bgVal = bgColorSlider.getValue();
|
||||||
|
displayLabel.setBackground( new Color(bgVal,bgVal,bgVal) );
|
||||||
|
// NOTE: The background color is a shade of gray,
|
||||||
|
// determined by the setting on the slider.
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
float hue = fgColorSlider.getValue()/100.0f;
|
||||||
|
displayLabel.setForeground( Color.getHSBColor(hue, 1.0f, 1.0f) );
|
||||||
|
// Note: The foreground color ranges through all the colors
|
||||||
|
// of the spectrum.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // end class SliderAndButtonDemo
|
||||||
|
|
68
src/SliderDemo.java
Normal file
68
src/SliderDemo.java
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
|
||||||
|
// A little program that demonstrates JSliders.
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.event.*;
|
||||||
|
|
||||||
|
public class SliderDemo extends JPanel implements ChangeListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A main routine allows this class to be run as an application.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame window = new JFrame("Slider Demo");
|
||||||
|
SliderDemo content = new SliderDemo();
|
||||||
|
window.setContentPane(content);
|
||||||
|
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
window.setLocation(120,70);
|
||||||
|
window.setSize(350,200);
|
||||||
|
window.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
JSlider slider1, slider2, slider3; // The sliders.
|
||||||
|
|
||||||
|
JLabel label; // A label for reporting changes in the sliders' values.
|
||||||
|
|
||||||
|
public SliderDemo() {
|
||||||
|
|
||||||
|
setLayout(new GridLayout(4,1));
|
||||||
|
setBorder(BorderFactory.createCompoundBorder(
|
||||||
|
BorderFactory.createLineBorder(Color.DARK_GRAY, 2),
|
||||||
|
BorderFactory.createEmptyBorder(8,8,8,8)));
|
||||||
|
|
||||||
|
label = new JLabel("Try dragging the sliders!", JLabel.CENTER);
|
||||||
|
add(label);
|
||||||
|
|
||||||
|
slider1 = new JSlider(0,10,0);
|
||||||
|
slider1.addChangeListener(this);
|
||||||
|
add(slider1);
|
||||||
|
|
||||||
|
slider2 = new JSlider();
|
||||||
|
slider2.addChangeListener(this);
|
||||||
|
slider2.setMajorTickSpacing(25);
|
||||||
|
slider2.setMinorTickSpacing(5);
|
||||||
|
slider2.setPaintTicks(true);
|
||||||
|
add(slider2);
|
||||||
|
|
||||||
|
slider3 = new JSlider(2000,2100,2014);
|
||||||
|
slider3.addChangeListener(this);
|
||||||
|
slider3.setLabelTable(slider3.createStandardLabels(50));
|
||||||
|
slider3.setPaintLabels(true);
|
||||||
|
add(slider3);
|
||||||
|
|
||||||
|
} // end constructor
|
||||||
|
|
||||||
|
public void stateChanged(ChangeEvent evt) {
|
||||||
|
if (evt.getSource() == slider1)
|
||||||
|
label.setText("Slider one changed to " + slider1.getValue());
|
||||||
|
else if (evt.getSource() == slider2)
|
||||||
|
label.setText("Slider two changed to " + slider2.getValue());
|
||||||
|
else if (evt.getSource() == slider3)
|
||||||
|
label.setText("Slider three changed to " + slider3.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
62
src/SliderDemoWithLambda.java
Normal file
62
src/SliderDemoWithLambda.java
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
|
||||||
|
// A little program that demonstrates JSliders. This is an alternative
|
||||||
|
// version of SliderDemo.java that uses lambda expressions to define
|
||||||
|
// the ChangeListeners are registered with the sliders.
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class SliderDemoWithLambda extends JPanel {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A main routine allows this class to be run as an application.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame window = new JFrame("Slider Demo");
|
||||||
|
SliderDemoWithLambda content = new SliderDemoWithLambda();
|
||||||
|
window.setContentPane(content);
|
||||||
|
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
window.setLocation(120,70);
|
||||||
|
window.setSize(350,200);
|
||||||
|
window.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
JSlider slider1, slider2, slider3; // The sliders.
|
||||||
|
|
||||||
|
JLabel label; // A label for reporting changes in the sliders' values.
|
||||||
|
|
||||||
|
public SliderDemoWithLambda() {
|
||||||
|
|
||||||
|
setLayout(new GridLayout(4,1));
|
||||||
|
setBorder(BorderFactory.createCompoundBorder(
|
||||||
|
BorderFactory.createLineBorder(Color.DARK_GRAY, 2),
|
||||||
|
BorderFactory.createEmptyBorder(8,8,8,8)));
|
||||||
|
|
||||||
|
label = new JLabel("Try dragging the sliders!", JLabel.CENTER);
|
||||||
|
add(label);
|
||||||
|
|
||||||
|
slider1 = new JSlider(0,10,0);
|
||||||
|
slider1.addChangeListener(
|
||||||
|
evt -> label.setText("Slider one changed to " + slider1.getValue()) );
|
||||||
|
add(slider1);
|
||||||
|
|
||||||
|
slider2 = new JSlider();
|
||||||
|
slider2.addChangeListener(
|
||||||
|
evt -> label.setText("Slider two changed to " + slider2.getValue()) );
|
||||||
|
slider2.setMajorTickSpacing(25);
|
||||||
|
slider2.setMinorTickSpacing(5);
|
||||||
|
slider2.setPaintTicks(true);
|
||||||
|
add(slider2);
|
||||||
|
|
||||||
|
slider3 = new JSlider(2000,2100,2014);
|
||||||
|
slider3.addChangeListener(
|
||||||
|
evt -> label.setText("Slider three changed to " + slider3.getValue()) );
|
||||||
|
slider3.setLabelTable(slider3.createStandardLabels(50));
|
||||||
|
slider3.setPaintLabels(true);
|
||||||
|
add(slider3);
|
||||||
|
|
||||||
|
} // end constructor
|
||||||
|
|
||||||
|
}
|
54
src/TextAreaDemo.java
Normal file
54
src/TextAreaDemo.java
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Font;
|
||||||
|
import java.awt.Insets;
|
||||||
|
|
||||||
|
import javax.swing.BorderFactory;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.JTextArea;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This program simply demonstrates using a JTextArea in a JScrollPane.
|
||||||
|
*/
|
||||||
|
public class TextAreaDemo extends JPanel {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A main routine allows this class to be run as an application.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame window = new JFrame("TextArea Demo");
|
||||||
|
TextAreaDemo content = new TextAreaDemo();
|
||||||
|
window.setContentPane(content);
|
||||||
|
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
window.setLocation(120,70);
|
||||||
|
window.setSize(400,250);
|
||||||
|
window.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
public TextAreaDemo() {
|
||||||
|
|
||||||
|
String text = "So, naturalists observe, a flea\n"
|
||||||
|
+ "Has smaller fleas that on him prey;\n"
|
||||||
|
+ "And these have smaller still to bite 'em;\n"
|
||||||
|
+ "And so proceed ad infinitum.\n\n"
|
||||||
|
+ " --Jonathan Swift";
|
||||||
|
|
||||||
|
JTextArea textArea = new JTextArea();
|
||||||
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
||||||
|
|
||||||
|
textArea.setText(text);
|
||||||
|
textArea.setFont( new Font("Serif", Font.PLAIN, 24 ));
|
||||||
|
textArea.setMargin( new Insets(7,7,7,7) );
|
||||||
|
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||||
|
add(scrollPane, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
234
src/ToolBarDemo.java
Normal file
234
src/ToolBarDemo.java
Normal file
@ -0,0 +1,234 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Demonstrates a toolbar and the use of image icons on buttons. The toolbar
|
||||||
|
* contains a radio group with three JRadioButtons and a button, all of which
|
||||||
|
* use custom icons. The actual program just lets the user draw curves
|
||||||
|
* in three different colors. This class can be run as a stand-alone program.
|
||||||
|
*/
|
||||||
|
public class ToolBarDemo extends JPanel {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main routine simply opens a window that shows a ToolBarDemo panel.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame window = new JFrame("ToolBarDemo");
|
||||||
|
ToolBarDemo content = new ToolBarDemo();
|
||||||
|
window.setContentPane(content);
|
||||||
|
window.pack();
|
||||||
|
window.setResizable(false);
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
window.setLocation( (screenSize.width - window.getWidth())/2,
|
||||||
|
(screenSize.height - window.getHeight())/2 );
|
||||||
|
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
|
||||||
|
window.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines the display area of the program, where the user can draw curves
|
||||||
|
* in various colors. (This class demonstrates resizing an off-screen canvas
|
||||||
|
* when the size of the display changes. The size can change if the user
|
||||||
|
* drags the toolbar out of the window or to a new position.)
|
||||||
|
*/
|
||||||
|
private class Display extends JPanel implements MouseListener, MouseMotionListener {
|
||||||
|
|
||||||
|
private BufferedImage OSC; // Off-screen canvas.
|
||||||
|
private Color currentColor = Color.RED; // Current drawing color.
|
||||||
|
private int prevX, prevY; // Previous mouse position, during mouse drags.
|
||||||
|
private BasicStroke stroke; // Stroke used for drawing.
|
||||||
|
|
||||||
|
Display() { // constructor.
|
||||||
|
addMouseListener(this);
|
||||||
|
addMouseMotionListener(this);
|
||||||
|
setPreferredSize(new Dimension(300,300));
|
||||||
|
stroke = new BasicStroke(3,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setCurrentColor(Color c) { // change current drawing color
|
||||||
|
currentColor = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear() { // clear the drawing area by filling it with white
|
||||||
|
if (OSC != null) {
|
||||||
|
Graphics g = OSC.getGraphics();
|
||||||
|
g.setColor(Color.WHITE);
|
||||||
|
g.fillRect(0,0,getWidth(),getHeight());
|
||||||
|
g.dispose();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void paintComponent(Graphics g) { // just copies OSC to screen
|
||||||
|
checkImage();
|
||||||
|
g.drawImage(OSC,0,0,null);
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkImage() { // create or resize OSC if necessary
|
||||||
|
if (OSC == null) {
|
||||||
|
// Create the OSC, with a size to match the size of the panel.
|
||||||
|
OSC = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
else if (OSC.getWidth() != getWidth() || OSC.getHeight() != getHeight()) {
|
||||||
|
// OSC size does not match the panel's size, so create a new OSC and
|
||||||
|
// copy the picture in the old OSC to the new one. This will scale
|
||||||
|
// the current image to fit the new size.
|
||||||
|
BufferedImage newOSC;
|
||||||
|
newOSC = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics g = newOSC.getGraphics();
|
||||||
|
g.drawImage(OSC,0,0,getWidth(),getHeight(),null);
|
||||||
|
g.dispose();
|
||||||
|
OSC = newOSC;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void mousePressed(MouseEvent e) {
|
||||||
|
prevX = e.getX();
|
||||||
|
prevY = e.getY();
|
||||||
|
}
|
||||||
|
public void mouseDragged(MouseEvent e) {
|
||||||
|
Graphics2D g2 = (Graphics2D)OSC.getGraphics();
|
||||||
|
g2.setColor(currentColor);
|
||||||
|
g2.setStroke(stroke);
|
||||||
|
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
|
g2.drawLine(prevX,prevY,e.getX(),e.getY());
|
||||||
|
g2.dispose();
|
||||||
|
repaint();
|
||||||
|
prevX = e.getX();
|
||||||
|
prevY = e.getY();
|
||||||
|
}
|
||||||
|
public void mouseReleased(MouseEvent e) { }
|
||||||
|
public void mouseMoved(MouseEvent e) { }
|
||||||
|
public void mouseClicked(MouseEvent e) { }
|
||||||
|
public void mouseEntered(MouseEvent e) { }
|
||||||
|
public void mouseExited(MouseEvent e) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Display display; // The display area of the program.
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor adds a display and a toolbar to the program. The
|
||||||
|
* advice for using a toolbar is to place the toolbar in one of
|
||||||
|
* the edge positions of a BorderLayout, and to put nothing in any
|
||||||
|
* of the other three edge positions. The user might be able
|
||||||
|
* to drag the toolbar from one edge position to another.
|
||||||
|
*/
|
||||||
|
public ToolBarDemo() {
|
||||||
|
|
||||||
|
setLayout(new BorderLayout(2,2));
|
||||||
|
setBackground(Color.GRAY);
|
||||||
|
setBorder(BorderFactory.createLineBorder(Color.GRAY,2));
|
||||||
|
|
||||||
|
display = new Display();
|
||||||
|
add(display, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
JToolBar toolbar = new JToolBar();
|
||||||
|
add(toolbar, BorderLayout.NORTH);
|
||||||
|
|
||||||
|
ButtonGroup group = new ButtonGroup();
|
||||||
|
toolbar.add( makeColorRadioButton(Color.RED,group,true) );
|
||||||
|
toolbar.add( makeColorRadioButton(Color.GREEN,group,false) );
|
||||||
|
toolbar.add( makeColorRadioButton(Color.BLUE,group,false) );
|
||||||
|
toolbar.addSeparator(new Dimension(20,20));
|
||||||
|
|
||||||
|
toolbar.add( makeClearButton() );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a JRadioButton and add it to a specified button group. The button
|
||||||
|
* is meant for selecting a drawing color in the display. The color is used to
|
||||||
|
* create two custom icons, one for the unselected state of the button and one
|
||||||
|
* for the selected state. These icons are used instead of the usual
|
||||||
|
* radio button icons.
|
||||||
|
* @param c the color of the button, and the color to be used for drawing.
|
||||||
|
* (Note that c has to be "final" since it is used in the anonymous inner
|
||||||
|
* class that defines the response to ActionEvents on the button.)
|
||||||
|
* @param grp the ButtonGroup to which the radio button will be added.
|
||||||
|
* @param selected if true, then the state of the button is set to selected.
|
||||||
|
* @return the radio button that was just created.
|
||||||
|
*/
|
||||||
|
private JRadioButton makeColorRadioButton(final Color c, ButtonGroup grp, boolean selected) {
|
||||||
|
|
||||||
|
/* Create an ImageIcon for the normal, unselected state of the button,
|
||||||
|
using a BufferedImage that is drawn here from scratch. */
|
||||||
|
|
||||||
|
BufferedImage image = new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics g = image.getGraphics();
|
||||||
|
g.setColor(Color.LIGHT_GRAY);
|
||||||
|
g.fillRect(0,0,30,30);
|
||||||
|
g.setColor(c);
|
||||||
|
g.fill3DRect(1, 1, 24, 24, true);
|
||||||
|
g.dispose();
|
||||||
|
Icon unselectedIcon = new ImageIcon(image);
|
||||||
|
|
||||||
|
/* Create an ImageIcon for the selected state of the button. */
|
||||||
|
|
||||||
|
image = new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB);
|
||||||
|
g = image.getGraphics();
|
||||||
|
g.setColor(Color.DARK_GRAY);
|
||||||
|
g.fillRect(0,0,30,30);
|
||||||
|
g.setColor(c);
|
||||||
|
g.fill3DRect(3, 3, 24, 24, false);
|
||||||
|
g.dispose();
|
||||||
|
Icon selectedIcon = new ImageIcon(image);
|
||||||
|
|
||||||
|
/* Create and configure the button. */
|
||||||
|
|
||||||
|
JRadioButton button = new JRadioButton(unselectedIcon);
|
||||||
|
button.setSelectedIcon(selectedIcon);
|
||||||
|
button.addActionListener( new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
// The action for this button sets the current drawing color
|
||||||
|
// in the display to c.
|
||||||
|
display.setCurrentColor(c);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
grp.add(button);
|
||||||
|
if (selected)
|
||||||
|
button.setSelected(true);
|
||||||
|
|
||||||
|
return button;
|
||||||
|
} // end makeColorRadioButton
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a JButton that can be used to clear the display. The button has
|
||||||
|
* no text and its icon is a custom icon that shows a big X. The icon is
|
||||||
|
* created by drawing it on a Buffered image and using that image to
|
||||||
|
* create an ImageIcon. The button has tooltip text "Clear the Display".
|
||||||
|
*/
|
||||||
|
private JButton makeClearButton() {
|
||||||
|
BufferedImage image = new BufferedImage(24,24,BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics2D g2 = (Graphics2D)image.getGraphics();
|
||||||
|
g2.setColor(Color.LIGHT_GRAY);
|
||||||
|
g2.fillRect(0,0,24,24);
|
||||||
|
g2.setStroke( new BasicStroke(3));
|
||||||
|
g2.setColor(Color.BLACK);
|
||||||
|
g2.drawLine(4,4,20,20);
|
||||||
|
g2.drawLine(4,20,20,4);
|
||||||
|
g2.dispose();
|
||||||
|
Icon clearIcon = new ImageIcon(image);
|
||||||
|
|
||||||
|
Action clearAction = new AbstractAction(null,clearIcon) {
|
||||||
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
display.clear();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
clearAction.putValue(Action.SHORT_DESCRIPTION, "Clear the Display");
|
||||||
|
JButton button = new JButton(clearAction);
|
||||||
|
|
||||||
|
return button;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user