import javax.swing.*;
import java.util.*;
import java.io.*;
// http://www.devx.com/tips/Tip/13176
public class SwingProperties {
public static void main(String[] args) {
String lineSep = "\n";
UIDefaults uid = UIManager.getDefaults();
Enumeration uidKeys = uid.keys();
BufferedWriter buff = null;
FileWriter fr = null;
try {
// 프로퍼티를 읽어서 파일로 저장할 것이다
fr = new java.io.FileWriter("swing_defaults.txt", true);
buff = new java.io.BufferedWriter(fr);
// 각각의 프로퍼티에 대해 값을 얻어온다
while (uidKeys.hasMoreElements()) {
Object aKey = uidKeys.nextElement();
Object aValue = uid.get(aKey);
String str = aKey + "\t = \t" + aValue + lineSep;
buff.write(str);
}
} catch (IOException ioe) {
} finally {
try {
buff.flush();
buff.close();
fr.close();
} catch (Exception e) {
}
}
}
}
import javax.swing.*;
import java.awt.*;
public class MyButton extends JFrame {
Font gulimItalicFont = new Font("굴림", Font.ITALIC, 20);
GridLayout gridLayout1 = new GridLayout();
JButton myButton;
JLabel myLabel;
public MyButton() {
initSwingProperties();
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
setSize(300,300);
setVisible(true);
}
private void initSwingProperties() {
UIDefaults defaults = UIManager.getDefaults();
defaults.put("Button.font", gulimItalicFont);
defaults.put("Label.font", gulimItalicFont);
defaults.put("ToolTip.background", Color.yellow);
}
public static void main(String[] args) {
MyButton myButton1 = new MyButton();
}
private void jbInit() throws Exception {
myButton = new JButton();
myLabel = new JLabel();
this.getContentPane().setLayout(gridLayout1);
gridLayout1.setColumns(1);
gridLayout1.setRows(2);
myButton.setToolTipText("My ToolTip");
myButton.setText("My Button");
myLabel.setHorizontalAlignment(SwingConstants.CENTER);
myLabel.setText("My
Label");
this.getContentPane().add(myButton, null);
this.getContentPane().add(myLabel, null);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyFileChooser extends JFrame {
Font gulimBoldFont = new Font("굴림", Font.PLAIN, 14);
Font gulimItalicFont = new Font("굴림", Font.ITALIC, 18);
Font batangFont = new Font("바탕", Font.PLAIN, 16);
JPanel jPanel1 = new JPanel();
JButton openButton = new JButton();
public MyFileChooser() {
initSwingProperties();
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
setSize(300,300);
setVisible(true);
}
private void initSwingProperties() {
UIDefaults defaults = UIManager.getDefaults();
defaults.put("List.font", gulimBoldFont);
defaults.put("Label.font", batangFont);
defaults.put("ComboBox.font", batangFont);
defaults.put("FileChooser.font", batangFont);
defaults.put("FileChooser.openButtonToolTipText", "Open");
defaults.put("FileChooser.saveButtonToolTipText", "Save");
defaults.put("FileChooser.cancelButtonToolTipText", "Cancel");
defaults.put("FileChooser.helpButtonToolTipText", "Help");
defaults.put("FileChooser.updateButtonToolTipText", "Update");
}
private void jbInit() throws Exception {
openButton.setText("Open");
openButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
openButton_actionPerformed(e);
}
});
this.getContentPane().add(jPanel1, BorderLayout.NORTH);
jPanel1.add(openButton, null);
}
void openButton_actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showSaveDialog(this);
}
public static void main(String[] args) {
MyFileChooser test = new MyFileChooser();
}
}
import javax.swing.*;
import java.awt.*;
public class MyScrollBar extends JFrame {
JScrollPane jScrollPane1;
JTextArea jTextArea1;
public MyScrollBar() {
initSwingProperties();
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void initSwingProperties() {
UIDefaults defaults = UIManager.getDefaults();
defaults.put("ScrollBar.thumb", Color.red);
defaults.put("ScrollBar.width", new Integer("30"));
}
public static void main(String[] args) {
MyScrollBar test = new MyScrollBar();
test.setSize(100,100);
test.setVisible(true);
}
private void jbInit() throws Exception {
jScrollPane1 = new JScrollPane();
jTextArea1 = new JTextArea();
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.setForeground(Color.red);
this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTextArea1, null);
}
}
import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
public class MyTree extends JFrame {
Font gulim_20 = new Font("굴림", Font.PLAIN, 20);
Font gulim_italic_16 = new Font("굴림", Font.ITALIC, 16);
JScrollPane scrollPane;
JTree tree;
public MyTree() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MyTree test = new MyTree();
}
private void jbInit() throws Exception {
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("ROOT", true);
DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
DefaultMutableTreeNode node01 = new DefaultMutableTreeNode("node01");
DefaultMutableTreeNode node02 = new DefaultMutableTreeNode("node02");
DefaultMutableTreeNode node03 = new DefaultMutableTreeNode("node03");
rootNode.add(node01);
node01.add(node02);
node02.add(node03);
tree = new JTree(treeModel);
tree.setCellRenderer(new MyTreeRenderer());
JScrollPane scrollPane = new JScrollPane(tree);
getContentPane().add(scrollPane, BorderLayout.CENTER);
scrollPane.getViewport().add(tree, null);
setSize(300,300);
setVisible(true);
}
class MyTreeRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
setFont(gulim_20);
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
Object obj = treeNode.getUserObject();
// 특정 노드만 다른 폰트를 사용하도록
if (obj.toString().equals("node02")) {
this.setFont(gulim_italic_16);
}
return MyTreeRenderer.this;
}
} // MyTreeRenderer
}
이전 글 : IDS 패킷 필터링
최신 콘텐츠