JAVA图形用户界面实例
} }); jbUp.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) { msPanel.moveUp();
} }); jbDown.addActionListener(new ActionListener(){
} }); //设置整个框架布局 setLayout(new BorderLayout()); //创建面板,用来放置 label 和 JCheckBox JPanel panel=new JPanel(); panel.setLayout(new BorderLayout());
JPanel RadioPanel=new JPanel(); JLabel label=new JLabel("Select a message Panel Background"); panel.add(label,BorderLayout.NORTH); panel.add(RadioPanel,BorderLayout.CENTER); //框架添加组件 add(panel,BorderLayout.NORTH); add(msPanel,BorderLayout.CENTER); add(jpButton,BorderLayout.SOUTH);
public void actionPerformed(ActionEvent e) { msPanel.moveDown();
} }); jcCenter.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) { msPanel.setCentered(jcCenter.isSelected());
FontMetrics fm=g.getFontMetrics(); x=(getWidth()/2-fm.stringWidth(string)); y=(getHeight()/2+fm.getAscent()/2); } g.drawString(string, x, y); } }
});
jrGreen.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { msPanel.setBackground(Color.GREEN);
} });
//按下时移动字符串 msPanel.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) { msPanel.setXY(e.getX(), e.getY());
} }); } }
MessagePanel.java import java.awt.FontMetrics; import java.awt.Graphics; import javax.swing.JPanel; class MessagePanel extends JPanel{
});
jrWhite.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { msPanel.setBackground(Color.WHITE); }
});
jrGray.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { msPanel.setBackground(Color.GRAY); }
private String string; private boolean centered=false; private int x=20; private int y=20; private int interval=10;
MessagePanel(){ } MessagePanel(String string){
public void actionPerformed(ActionEvent e) { msPanel.moveLeft();
} }); jbRight.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) { msPanel.moveRight();
//设置 RadioBox 的布局方式 RadioPanel.setLayout(new FlowLayout(FlowLayout.LEADING)); //添加 RadioBox 的组件 RadioPanel.add(jrRed=new JRadioButton("Red")); RadioPanel.add(jrYellow=new JRadioButton("Yellow")); RadioPanel.add(jrWhite=new JRadioButton("White")); RadioPanel.add(jrGray=new JRadioButton("Gray")); RadioPanel.add(jrGreen=new JRadioButton("Green")); //限制只能选择一个单选框 ButtonGroup bg=new ButtonGroup(); bg.add(jrRed); bg.add(jrYellow); bg.add(jrWhite); bg.add(jrGray); bg.add(jrGreen);
//添加 JRadioBox 的监听器 jrRed.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) { msPanel.setBackground(Color.red);
} });
jrYellow.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { msPanel.setBackground(Color.YELLOW); }
if(x<=0) x=getWidth();
repaint(); } public void moveUp(){
y-=interval; if(y<=0)
y=getHeight(); repaint(); } public void moveDown(){ y+=interval; if(y>=getHeight())
this.string=string; } public void setXY(int x,int y){
this.x=x; this.y=y; repaint(); } public void moveRight(){ x+=interval; if(x>=getWidth())
x=0; repaint(); } public void moveLeft(){ x-=interval;
public class ButtonPanel extends JFrame{ private JRadioButton jrRed,jrYellow,jrWhite,jrGray,jrGreen; private JButton jbLeft,jbRight,jbDown,jbUp; private JCheckBox jcCenter; private MessagePanel msPanel=new MessagePanel("Hello World,China");
ButtonPanel.java
import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; 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 javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton;
y=0; repaint(); }
public boolean isCentered(){ return centered;
} public void setCentered(boolean centered){
this.centered=centered; repaint(); } protected void paintComponent(Graphics g){ super.paintComponent(g); if(centered){
jbRight.setMnemonic('R'); jbDown.setMnemonic('D'); jbUp.setMnemonic('U'); jcCenter.setMnemonic('C'); //添加按钮和单选框监听器 jbLeft.addActionListener(new ActionListener(){