-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathGridBagTest.java
More file actions
93 lines (81 loc) · 2.64 KB
/
Copy pathGridBagTest.java
File metadata and controls
93 lines (81 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package read;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
/**
* This program shows how to use an XML file to describe a gridbag layout
* @version 1.11 2012-06-03
* @author Cay Horstmann
*/
public class GridBagTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFileChooser chooser = new JFileChooser(".");
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
JFrame frame = new FontFrame(file);
frame.setTitle("GridBagTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
/**
* This frame contains a font selection dialog that is described by an XML file.
* @param filename the file containing the user interface components for the dialog.
*/
class FontFrame extends JFrame
{
private GridBagPane gridbag;
private JComboBox<String> face;
private JComboBox<String> size;
private JCheckBox bold;
private JCheckBox italic;
@SuppressWarnings("unchecked")
public FontFrame(File file)
{
gridbag = new GridBagPane(file);
add(gridbag);
face = (JComboBox<String>) gridbag.get("face");
size = (JComboBox<String>) gridbag.get("size");
bold = (JCheckBox) gridbag.get("bold");
italic = (JCheckBox) gridbag.get("italic");
face.setModel(new DefaultComboBoxModel<String>(new String[] { "Serif",
"SansSerif", "Monospaced", "Dialog", "DialogInput" }));
size.setModel(new DefaultComboBoxModel<String>(new String[] { "8",
"10", "12", "15", "18", "24", "36", "48" }));
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setSample();
}
};
face.addActionListener(listener);
size.addActionListener(listener);
bold.addActionListener(listener);
italic.addActionListener(listener);
setSample();
pack();
}
/**
* This method sets the text sample to the selected font.
*/
public void setSample()
{
String fontFace = face.getItemAt(face.getSelectedIndex());
int fontSize = Integer.parseInt(size.getItemAt(size.getSelectedIndex()));
JTextArea sample = (JTextArea) gridbag.get("sample");
int fontStyle = (bold.isSelected() ? Font.BOLD : 0)
+ (italic.isSelected() ? Font.ITALIC : 0);
sample.setFont(new Font(fontFace, fontStyle, fontSize));
sample.repaint();
}
}