这学期刚开始学JAVA,老师说要先学会看别人的程序,
大家可不可以把自己写的程序(最好是有关界面布局的,自己的布局写出来N难看)发个给我看看怎么写
(书上有关界面布局的只有10几页)
我的邮箱:ruyi512@sina.com
新年到来,祝大家事事如意
[此贴子已经被作者于2006-12-31 21:17:38编辑过]
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
/**
* 上下箭头变化框
* @author cgs
* @since 2006.12.25
* @version 1.0
*/
public class Raiser extends JLabel{private static final long serialVersionUID = 1L;
private int raiseCount = 1;
private RaiserButton up;
private DeRaiserButton down;
private JTextField base;
public Raiser(){
base = new JTextField();
base.setEditable(false);
base.setDisabledTextColor(Color.BLACK);
base.setText(\"2008\");
up = new RaiserButton();
down = new DeRaiserButton();
JPanel rp = new JPanel();
rp.setLayout(new GridLayout(2,1));
rp.add(up);rp.add(down);
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(base);add(rp);
setBorder(new LineBorder(Color.GRAY));
}
public Raiser(int count){
this();
raiseCount = count;
base.setText(\"\"+getRaiseCount());
}
/**
* 获得上箭头按钮
* @return 上箭头按钮
*/
public RaiserButton getRaiserButton(){
return up;
}
/**
* 获得下箭头按钮
* @return 下箭头按钮
*/
public DeRaiserButton getDeRaiserButton(){
return down;
}
/**
* 设置文本框显示的内容
* @param count 显示的内容
*/
public void setRaiseCount(int count){
raiseCount = count;
base.setText(\"\"+getRaiseCount());
}
/**
* 获得文本框显示的数目
* @return 文本框显示的数目
*/
public int getRaiseCount(){
return raiseCount;
}
/**
* 上箭头按钮
* @author cgs
*/
public class RaiserButton extends JButton{
private static final long serialVersionUID = 1L;
RaiserButton(){
super();
setPreferredSize(new Dimension(20, 15));
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
g.setColor(Color.BLACK);
g.drawLine(width/2, height/2-2, width/2, height/2-2);
g.drawLine(width/2-1, height/2-1, width/2+1, height/2-1);
g.drawLine(width/2-2, height/2, width/2+2, height/2);
g.drawLine(width/2-3, height/2+1, width/2+3, height/2+1);
g.drawLine(width/2-4, height/2+2, width/2+4, height/2+2);
}
}
/**
* 下箭头按钮
* @author cgs
*/
public class DeRaiserButton extends JButton{
private static final long serialVersionUID = 1L;
DeRaiserButton(){
super(\"\");
setPreferredSize(new Dimension(20, 15));
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
g.setColor(Color.BLACK);
g.drawLine(width/2-4, height/2-2, width/2+4, height/2-2);
g.drawLine(width/2-3, height/2-1, width/2+3, height/2-1);
g.drawLine(width/2-2, height/2, width/2+2, height/2);
g.drawLine(width/2-1, height/2+1, width/2+1, height/2+1);
g.drawLine(width/2, height/2+2, width/2, height/2+2);
}
}
}
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Vector;import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
/**
* 日期
* @author cgs
* @since 2006.12.25
*/
public class Date extends JPanel implements ItemListener, ActionListener{
private static final long serialVersionUID = 1L;
public static void main(String []args){
Date date = new Date();
JFrame f = new JFrame(\"日期\");
f.add(date);
f.setBounds(300, 200, 180, 220);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
private JComboBox month;
private Raiser year;
private SLabel []days = new SLabel[42];
private SLabel []week = new SLabel[7];
private JPanel dayc, up;
private String []months = {\"一月\", \"二月\", \"三月\", \"四月\", \"五月\", \"六月\",
\"七月\", \"八月\", \"九月\", \"十月\", \"十一月\", \"十二月\"};
private String []weeks = {\"日\", \"一\", \"二\", \"三\", \"四\", \"五\", \"六\"};
private Calendar option;
private JButton one, two;
private Component focus = null;
private int focusday = 1;
Color oldb, oldf;
Date(){
option = Calendar.getInstance();
focusday = option.get(Calendar.DAY_OF_MONTH);
setBorder(new TitledBorder(new LineBorder(Color.GRAY), \"日期\"));
month = new JComboBox(months);
month.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH));
year = new Raiser(Calendar.getInstance().get(Calendar.YEAR));
month.addItemListener(this);
one = year.getRaiserButton();
two = year.getDeRaiserButton();
one.addActionListener(this);two.addActionListener(this);
up = new JPanel();
up.setLayout(new GridLayout(1,2));
up.add(month);up.add(year);
dayc = new JPanel();
dayc.setBorder(new BevelBorder(BevelBorder.LOWERED));
dayc.setLayout(new GridLayout(7,7));
for(int i=0; i<7; i++){
week[i] = new SLabel(weeks[i]);
week[i].setBackground(new Color(0, 132, 255));
week[i].setForeground(Color.WHITE);
dayc.add(week[i]);
}
for(int i=0; i<42; i++){
days[i] = new SLabel();
days[i].setPreferredSize(new Dimension(20,20));
dayc.add(days[i]);
}
refresh();
oldb = days[41].getBackground();
oldf = days[41].getForeground();
dayc.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
Component c = dayc.getComponentAt(e.getX(), e.getY());
if(c instanceof SLabel){
int text;
try{
text = Integer.parseInt(((SLabel)c).getText());
}catch(NumberFormatException ne){
return;
}
if(Date.this.focusday == text)return;
if(Date.this.focus!=null){
Date.this.focus.setBackground(oldb);
Date.this.focus.setForeground(oldf);
}
Date.this.focus = c;
Date.this.focusday = text;
c.setBackground(new Color(0, 132, 255));
c.setForeground(Color.WHITE);
}
}
});
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(up);
add(dayc);
}
private void refresh(){
int yyyy = year.getRaiseCount();
int mm = month.getSelectedIndex();
option.set(yyyy, mm, 1);
int begin = option.get(Calendar.DAY_OF_WEEK);
GregorianCalendar gc = new GregorianCalendar();
Vector<Integer> f = new Vector<Integer>(7);
f.add(1);f.add(3);f.add(5);f.add(7);f.add(8);f.add(10);f.add(12);
int dayss = (mm+1)==2
?(gc.isLeapYear(yyyy) ? 29 :28) : (f.contains(mm+1) ? 31 : 30);
for(int i=0; i<42; i++){
if(i>=begin-1&&i<begin-1+dayss) {
days[i].setText(\"\"+(i-begin+2));
if(i-begin+2==focusday){
if(focus!=null){
focus.setBackground(oldb);
focus.setForeground(oldf);
}
focus = days[i];
focus.setBackground(new Color(0, 132, 255));
focus.setForeground(Color.WHITE);
}
}
else days[i].setText(\"\");
}
}
public void itemStateChanged(ItemEvent e) {
refresh();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==one)
year.setRaiseCount(year.getRaiseCount()+1);
if(e.getSource()==two)
year.setRaiseCount(year.getRaiseCount()-1);
refresh();
}
private class SLabel extends JLabel{
private static final long serialVersionUID = 1L;
private Color c = null;
SLabel(String str){
super(str);
}
SLabel(){
super();
}
public void setBackground(Color c){
this.c = c;
repaint();
}
public void paintComponent(Graphics g){
if(c!=null){
Color old = g.getColor();
g.setColor(c);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(old);
}
super.paintComponent(g);
}
}
}
我有一个自己编写的比较简单的计算器解界面
用的是GridLayout布局管理器
import java.awt.*;
import java.awt.event.*;
public class Jisuanqi extends Frame implements WindowListener,ItemListener{
protected void makebutton(String name,GridBagLayout gridbag,GridBagConstraints c){
Button button=new Button(name);
gridbag.setConstraints(button,c);
add(button);
}
public Jisuanqi(){
MenuBar mb;
Menu m1,m2,m3;
MenuItem m1_1,m1_2,m2_1,m2_2,m2_3,m3_1,m3_2;
mb=new MenuBar();
setMenuBar(mb);
m1=new Menu("编辑");
mb.add(m1);
m1_1=new MenuItem("复制");
m1_1.setShortcut(new MenuShortcut(KeyEvent.VK_C));
m1.add(m1_1);
m1_2=new MenuItem("粘贴");
m1_2.setShortcut(new MenuShortcut(KeyEvent.VK_V));
m1.add(m1_2);
m2=new Menu("查看");
mb.add(m2);
m2_1=new CheckboxMenuItem("标准型");
m2.add(m2_1);
m2_2=new CheckboxMenuItem("科学型");
m2.add(m2_2);
m2.addSeparator();
m2_3=new CheckboxMenuItem("数字分组");
m2.add(m2_3);
m3=new Menu("帮助");
mb.setHelpMenu(m3);
m3_1=new MenuItem("帮助主题");
m3.add(m3_1);
m3_2=new MenuItem("关于计算器");
m3.add(m3_2);
GridBagLayout gridbag=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
setLayout(gridbag);
TextField text1=new TextField();
c.weightx=1;
c.weighty=1;
c.fill=GridBagConstraints.BOTH;
c.gridwidth=GridBagConstraints.REMAINDER;
gridbag.setConstraints(text1, c);
add(text1);
c.weightx=1;
c.weighty=1;
c.gridwidth=4;
makebutton("Backspace",gridbag,c);
makebutton("CE",gridbag,c);
c.gridwidth=GridBagConstraints.REMAINDER;
makebutton("C",gridbag,c);
c.weightx=1;
c.weighty=1;
c.gridwidth=2;
makebutton("MC",gridbag,c);
makebutton("7",gridbag,c);
makebutton("8",gridbag,c);
makebutton("9",gridbag,c);
makebutton("/",gridbag,c);
c.gridwidth=GridBagConstraints.REMAINDER;
makebutton("sqrt",gridbag,c);
c.weightx=1;
c.weighty=1;
c.gridwidth=2;
makebutton("MR",gridbag,c);
makebutton("4",gridbag,c);
makebutton("5",gridbag,c);
makebutton("6",gridbag,c);
makebutton("*",gridbag,c);
c.gridwidth=GridBagConstraints.REMAINDER;
makebutton("%",gridbag,c);
c.weightx=1;
c.weighty=1;
c.gridwidth=2;
makebutton("MS",gridbag,c);
makebutton("1",gridbag,c);
makebutton("2",gridbag,c);
makebutton("3",gridbag,c);
makebutton("-",gridbag,c);
c.gridwidth=GridBagConstraints.REMAINDER;
makebutton("1/x",gridbag,c);
c.weightx=1;
c.weighty=1;
c.gridwidth=2;
makebutton("M+",gridbag,c);
makebutton("0",gridbag,c);
makebutton("+/-",gridbag,c);
makebutton(".",gridbag,c);
makebutton("+",gridbag,c);
c.gridwidth=GridBagConstraints.REMAINDER;
makebutton("=",gridbag,c);
}
public void itemStateChanged(ItemEvent e){};
public static void main(String args[]){
Jisuanqi fr=new Jisuanqi();
fr.setTitle("计算器");
fr.addWindowListener(fr);
fr.pack();
fr.setVisible(true);
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void windowClosed(WindowEvent e){};
public void windowOpened(WindowEvent e){};
public void windowIconified(WindowEvent e){};
public void windowDeiconified(WindowEvent e){};
public void windowActivated(WindowEvent e){};
public void windowDeactivated(WindowEvent e){};
}