这是一个用下拉式列表控制标签位置的程序,编译可以通过,但无法运行,高手看下哈(程序长了点,监听器就省了哈)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exercise15_9 extends JFrame {
private JLabel jlbIcon;
private JLabel[] jlbExplain = new JLabel[2];
private JLabel[] jlbImage = new JLabel[2];
private JLabel[] jlbText = new JLabel[2];
private JComboBox[] jcbImage = new JComboBox[2];
private JComboBox[] jcbText = new JComboBox[2];
private String[] horizontal = {"LEFT", "RIGHT", "CENTER"};
private String[] vertical = {"TOP", "BOTTOM", "CENTER"};
public Exercise15_9() {
JPanel[] jpanel = new JPanel[7];
//设置图像标签
ImageIcon icon = new ImageIcon("image/grapes.gif");
jlbIcon = new JLabel("Grapes", icon, SwingConstants.RIGHT);
jlbIcon.setHorizontalTextPosition(SwingConstants.LEFT);
jlbIcon.setVerticalTextPosition(SwingConstants.TOP);
jlbIcon.setIconTextGap(5);
//创建控制图像,图像标签位置的面板的标签
jlbExplain[0] = new JLabel("Horizontal Alignment");
jlbExplain[1] = new JLabel("Text Position");
//创建控制图像位置的面板
jlbImage[0] = new JLabel("Horizontal");
jlbImage[1] = new JLabel("Vertical");
jcbImage[0] = new JComboBox(horizontal);
jcbImage[1] = new JComboBox(vertical);
jpanel[0].setLayout(new GridLayout(2, 1));
jpanel[0].add(jlbImage[0]);
jpanel[0].add(jlbImage[1]);
jpanel[1].setLayout(new GridLayout(2, 1));
jpanel[1].add(jcbImage[0]);
jpanel[1].add(jcbImage[1]);
jpanel[2].setLayout(new GridLayout(1, 2));
jpanel[2].add(jpanel[0]);
jpanel[2].add(jpanel[1]);
//创建控制图像标签位置的面板
jlbText[0] = new JLabel("Horizontal");
jlbText[1] = new JLabel("Vertical");
jcbText[0] = new JComboBox(horizontal);
jcbText[1] = new JComboBox(vertical);
jpanel[3].setLayout(new GridLayout(2, 1));
jpanel[3].add(jlbText[0]);
jpanel[3].add(jlbText[1]);
jpanel[4].setLayout(new GridLayout(2, 1));
jpanel[4].add(jcbText[0]);
jpanel[4].add(jcbText[1]);
jpanel[5].setLayout(new GridLayout(1, 2));
jpanel[5].add(jpanel[3]);
jpanel[5].add(jpanel[4]);
//将控制面板及其标签加在一个面板上
jpanel[6].setLayout(new GridLayout(2, 2));
jpanel[6].add(jlbExplain[1]);
jpanel[6].add(jlbExplain[1]);
jpanel[6].add(jpanel[2]);
jpanel[6].add(jpanel[5]);
//将上面图像标签和控制面板加在一个容器上
setLayout(new GridLayout(2, 1));
add(jlbIcon);
add(jpanel[6]);
}
public static void main(String[] args) {
Exercise15_9 frame = new Exercise15_9();
frame.setTitle("Exercise15_9");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
frame.setVisible(true);
}
}