java编写三个按钮
发布网友
发布时间:2023-06-28 16:38
我来回答
共1个回答
热心网友
时间:2024-01-04 16:49
给你一段代码,应该能满足你的需求:
public class LoginIFrame extends JFrame {
public static void main(String[] args) {
JFrame jf = new JFrame("系统登录");
JPanel jpanel = new JPanel();
jf.setContentPane(jpanel);
// 设置窗口在屏幕中居中显示
double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
int x = (int) (width - 300) / 2;
int y = (int) (height - 250) / 2;
jf.setBounds(x, y, 300, 150);
// 添加标签组件
GridLayout gird = new GridLayout(3, 0);
JPanel jpanel1 = new JPanel();
JPanel jpanel2 = new JPanel();
JPanel jpanel3 = new JPanel();
jpanel.setLayout(gird);
jpanel.add(jpanel1);
jpanel.add(jpanel2);
jpanel.add(jpanel3);
JLabel name = new JLabel("姓 名:");
JTextField nametext = new JTextField(10);
JLabel pwd = new JLabel("密 码:");
JPasswordField pwd1 = new JPasswordField(10);
JButton login = new JButton("登录");
JButton exit = new JButton("退出");
JButton clear = new JButton("重置");
jpanel1.add(name);
jpanel1.add(nametext);
jpanel2.add(pwd);
jpanel2.add(pwd1);
jpanel3.add(login);
jpanel3.add(exit);
jpanel3.add(clear);
jf.getLayeredPane().setLayout(null);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jpanel.setOpaque(false);
jpanel1.setOpaque(false);
jpanel2.setOpaque(false);
jpanel3.setOpaque(false);
jf.setVisible(true);
}
}
如果满意请采纳!
追问能给我一个思路么?我要的是思路,不是程序
追答首先初始化一个窗体JFrame,然后在窗体上添加一个容器JPanel,用于放置一些标签、文本框,按钮等