你的frame没布局,加一个就可以了 代码如下。
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Example2
{
public static void main(String args[]) {
WindowEventAction win = new WindowEventAction();
win.setBounds(100, 100, 300, 300);
win.setTitle("图书馆查询系统");
}
}
class WindowEventAction extends JFrame {
GridLayout grid;
JPanel panel;
JButton an1, an2, an3, an4, an5;
JTextField text;
JTextArea area;
JComboBox box;
public WindowEventAction() {
Menu();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void Menu() {
setLayout(new FlowLayout());
grid = new GridLayout(2, 2);
panel = new JPanel();
panel.setLayout(grid);
an1 = new JButton("查询");
an2 = new JButton("录入");
an3 = new JButton("修改");
an4 = new JButton("删除");
an5 = new JButton("退出");
area = new JTextArea(10, 30);
box = new JComboBox();
text = new JTextField(10);
box.addItem("请选择查找方式:");
box.addItem("作者");
box.addItem("价格");
JLabel label[][] = new JLabel[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (i == 0) {
if (j == 0)
label[i][j] = new JLabel("1");
else
label[i][j] = new JLabel("2");
} else {
if (j == 0)
label[i][j] = new JLabel("3");
else
label[i][j] = new JLabel("4");
}
panel.add(label[i][j]);
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
switch (Integer.parseInt(label[i][j].getText())) {
case 1:
add(an1);
add(an2);
add(an3);
add(an4);
break;
case 2:
add(box);
add(text);
break;
case 3:
case 4:
add(new JScrollPane(area));
}
}
}
}
}
取消
评论