panel = new JPanel();//以下四句是对类的成员变量进行初始化,
mainpanel = new JPanel();
qita = new JPanel();
yunsuan = new JPanel();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);显示显示
qita.setLayout(new GridLayout(1,3));
yunsuan.setLayout(new GridLayout(3, 5));//弹出的窗口是以网格的形式(3行5列)
catch (Exception e) {
textAnswer.setText("操作非法");
clickable = false;
}
附小程序一枚:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FirstSwing {
private static final int width = 351;
private static final int heigh = 152;
private static final int startX = 341;
private static final int startY = 256;
private JLabel label;
private int count = 0;
public FirstSwing(){
JFrame frame = new JFrame("第一个小swing程序");
frame.setLocation(new Point(startX, startY));
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setPreferredSize(new Dimension(width,heigh));
label = new JLabel("标签名称",JLabel.CENTER);//创建标签
contentPane.add(label,BorderLayout.CENTER);
//按钮。激发。位置
JButton button = new JButton("按钮");
button.addActionListener(new SimpleActionListenser());//激发
contentPane.add(button,BorderLayout.SOUTH);//将按钮加入到面板中
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();//都已经设定窗口初始位置与大小了,还要这句?
frame.setVisible(true);
}
//定义触发事件
private class SimpleActionListenser implements ActionListener{
public void actionPerformed(ActionEvent evt){
count = count + 1;
System.out.println("第"+count+"次 按按钮");
label.setText("标签内容");
}
}
public static void main(String[] args){
FirstSwing demo = new FirstSwing();
}
}