从画图程序开始,走进GUI编程世界
style=font.getStyle();
size=font.getSize();
JPanel buttonPane=new JPanel();
buttonPane.add(ok=createButton("确认"));
buttonPane.add(cancel=createButton("取消"));
getContentPane().add(buttonPane,BorderLayout.SOUTH);
JPanel dataPane=new JPanel();
dataPane.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black),
BorderFactory.createEmptyBorder(5,5,5,5)));
GridBagLayout gbLayout=new GridBagLayout();
dataPane.setLayout(gbLayout);
GridBagConstraints constraints=new GridBagConstraints();
JLabel label=new JLabel("选择字体");
constraints.fill=GridBagConstraints.HORIZONTAL;
constraints.gridwidth=GridBagConstraints.REMAINDER;
gbLayout.setConstraints(label,constraints);
dataPane.add(label);
GraphicsEnvironment e=GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts=e.getAvailableFontFamilyNames();
fontList=new JList(fonts);
fontList.setValueIsAdjusting(true);
fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fontList.setSelectedValue(font.getFamily(),true);
fontList.addListSelectionListener(this);
JScrollPane chooseFont=new JScrollPane(fontList);
chooseFont.setMinimumSize(new Dimension(300,100));
JPanel display=new JPanel();
fontDisplay=new JLabel("字体样例:x X y Y z Z");
fontDisplay.setPreferredSize(new Dimension(300,100));
display.add(fontDisplay);
JSplitPane splitPane=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,chooseFont,display);
gbLayout.setConstraints(splitPane,constraints);
dataPane.add(splitPane);
JPanel sizePane=new JPanel();
label=new JLabel("选择字体大小");
sizePane.add(label);
String[] sizeList={"8","10","12","14","16","18","20","22","24"};
chooseSize=new JComboBox(sizeList);
chooseSize.setSelectedItem(Integer.toString(size));
chooseSize.addActionListener(this);
sizePane.add(chooseSize);
gbLayout.setConstraints(sizePane,constraints);
dataPane.add(sizePane);
JRadioButton bold=new JRadioButton("加粗",(style&Font.BOLD)>0);
JRadioButton italic=new JRadioButton("斜体",(style&Font.ITALIC)>0);
bold.addItemListener(new StyleListener(Font.BOLD));
italic.addItemListener(new StyleListener(Font.ITALIC));
JPanel stylePane=new JPanel();
stylePane.add(bold);
stylePane.add(italic);
gbLayout.setConstraints(stylePane,constraints);
dataPane.add(stylePane);
getContentPane().add(dataPane,BorderLayout.CENTER);
pack();
setVisible(false);
}
JButton createButton(String label){
JButton button=new JButton(label);
button.setPreferredSize(new Dimension(80,20));
button.addActionListener(this);
return button;
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
if(source==ok){
window.setFont(font);
setVisible(false);
}
else if(source==cancel)
setVisible(false);
else if(source==chooseSize){
size=Integer.parseInt((String)chooseSize.getSelectedItem());
font=font.deriveFont((float)size);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
}
public void valueChanged(ListSelectionEvent e){
if(!e.getValueIsAdjusting()){
font=new Font((String)fontList.getSelectedValue(),style,size);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
}
class StyleListener implements ItemListener{
private int astyle;
public StyleListener(int style){
this.astyle=style;
}
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==ItemEvent.SELECTED)
style|=astyle;
else style&=~astyle;
font=font.deriveFont(style);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
}
}
package boya;
import java.awt.*;
import java.io.File;
public interface ID{
int LINE=101;
int RECTANGLE=102;
int CIRCLE=103;
int CURVE=104;
int TEXT=105;
int DEFAULT_ELEMENT_TYPE=LINE;
Color DEFAULT_ELEMENT_COLOR=Color.red;
Font DEFAULT_FONT=new Font("宋体",Font.PLAIN,20);
File DEFAULT_DIR=new File("c:/My Draw");
String DEFAULT_FILENAME="Untitled.ice";
}
package boya;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class StatusBar extends JPanel implements ID{
private StatusPane colorPane=new StatusPane("红色");
private StatusPane typePane=new StatusPane("直线");
class StatusPane extends JLabel{
private Font paneFont=new Font("宋体",Font.PLAIN,12);
public StatusPane(String text){
setForeground(Color.black);
setFont(paneFont);
setHorizontalAlignment(CENTER);
setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));;
setPreferredSize(new Dimension(70,18));
setText(text);
}
}
public StatusBar(){
setLayout(new FlowLayout(FlowLayout.RIGHT,10,3));
setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
setColorPane(DEFAULT_ELEMENT_COLOR);
setTypePane(DEFAULT_ELEMENT_TYPE);
add(colorPane);
add(typePane);
}
public void setColorPane(Color color){
String text;
if(color.equals(Color.red))
text="红色";
else if(color.equals(Color.yellow))
text="黄色";
else if(color.equals(Color.green))
text="绿色";
else if(color.equals(Color.blue))
text="蓝色";
else text="未定义";
colorPane.setText(text);
}
public void setTypePane(int element){
String text;
switch(element){
case LINE:
text="直线";
break;
case RECTANGLE:
text="矩形";
break;
case CIRCLE:
text="圆";
break;
case CURVE:
text="曲线";
break;
case TEXT:
text="文字";
break;
default:
text="错误";
break;
}
typePane.setText(text);
}
}
最后还可以将生成的代码放置到JAR包中,这样就可以在安装JVM的环境下,使
此JAR包作为可执行文件了。
包的结构如下:
--Draw
----boya
----META-INF
----res
其分别为包、配置文件、资源
在META-INF中建立文件MANIFEST.MF写入:
Manifest-Version: 1.0
Main-Class: boya.Draw
这样这个包就完成了
Main-Class: boya.Draw
Tags:画图,程序,开始,走进,GUI,编程,世界

