例子:
public class ButtonDemo extends JFrame {
private JButton leftbtn = new JButton(">=");
private JButton rightBtn = new JButton("<=");
//构造方法
public ButtonDemo (){
//创建Panel
JPanel panel = new JPanel();
panel.add(new JLabel("welcome to Java"));
JPanel bottomPanel = new JPanel();
//当前模型上的键盘助记符
leftbtn.setMnemonic('L');
leftbtn.setToolTipText("Move message to left");
rightBtn.setMnemonic('R');
rightBtn.setToolTipText("move message to right ");
//添加到panel
bottomPanel.add(leftbtn);
bottomPanel.add(rightBtn);
//设置布局
BorderLayout layout = new BorderLayout();
setLayout(layout);
add(panel,layout.CENTER);
add(bottomPanel,layout.SOUTH);
//添加事件
leftbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveLeft();
}
});
rightBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveRight();
}
});
}
public void moveLeft(){
System.out.println("向左移动");
}
public void moveRight(){
System.out.println("向右移动");
}
public static void main(String[] args) {
ButtonDemo frame = new ButtonDemo();
//自动调整框架大小
frame.pack();
frame.setTitle("buttonDemo");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
结果:
2、案例2、
public class DespritionPanel extends JPanel{
private JLabel disLable = new JLabel();
private JTextArea textArea = new JTextArea();
public DespritionPanel(){
disLable.setForeground(Color.red);
disLable.setHorizontalAlignment(JLabel.CENTER);
disLable.setHorizontalTextPosition(JLabel.CENTER);
disLable.setVerticalTextPosition(JLabel.BOTTOM);
disLable.setFont(new Font("",Font.BOLD,22));
//可以滚动的panel
JScrollPane displayPanel = new JScrollPane(textArea);
textArea.setFont(new Font("",Font.ITALIC,15));
textArea.setLineWrap(true);//自动换行
textArea.setWrapStyleWord(true);//按单词而不是字符
textArea.setEditable(true);
setLayout(new BorderLayout(5,5));
add(disLable,BorderLayout.CENTER);
add(displayPanel,BorderLayout.WEST);
}
//设置数据
public void settitle(String title){
disLable.setText(title);
}
//设置数据
public void setImageIcon(ImageIcon icon){
disLable.setIcon(icon);
}
//设置数据
public void setdesp(String desp){
textArea.setText(desp);
}
}
ComboBoxclass 实现
public class ComboBoxclass extends JFrame {
//combobox 的标题
private String [] titleArr = {"china","canada","USA","England","Franch"};
//描述的数组
private String [] despArr = new String[titleArr.length];
//图片数组
private ImageIcon [] flagimgArr = {
new ImageIcon("ImageSource/1.png"),
new ImageIcon("ImageSource/1.png"),
new ImageIcon("ImageSource/1.png"),
new ImageIcon("ImageSource/1.png"),
new ImageIcon("ImageSource/1.png"),
};
//下拉框
private JComboBox comboBox = new JComboBox(titleArr);
//
private DespritionPanel despPanel = new DespritionPanel();
public ComboBoxclass(){
despArr[0] = "xdgfhjklpoliuytrdfgyhujikopoiuygfdfghjkllknbvbnm,xdgfhjklpoliuytrdfgyhujikopoiuygfdfghjkllknbvbnm,xdgfhjklpoliuytrdfgyhujikopoiuygfdfghjkllknbvbnm,xdgfhjklpoliuytrdfgyhujikopoiuygfdfghjkllknbvbnm,xdgfhjklpoliuytrdfgyhujikxdgfhjklpoliuytrdfgyhujikopoiuygfdfghjkllknbvbnm,opoiuygfdfghjkllknbvbnm,./;lkiuygtf";
despArr[1] = "qwwwwwwww";
despArr[2] = "assszxcxc";
despArr[3] = "yyy6666666";
despArr[4] = "211223ddfggdss";
add(comboBox, BorderLayout.NORTH);
add(despPanel,BorderLayout.CENTER);
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
setdata(comboBox.getSelectedIndex());
}
});
//设置默认数据
setdata(0);
}
public static void main(String[] args) {
ComboBoxclass frame = new ComboBoxclass();
frame.setTitle("combox");
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
//设置数据
public void setdata(int index){
despPanel.settitle(titleArr[index]);
despPanel.setImageIcon(flagimgArr[index]);
despPanel.setdesp(despArr[index]);
}
}