java swing中点击扩大,页面组件也扩大

package com.Eyou.demo; import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Insets; import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField; import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List; @SuppressWarnings"serial"
public class Demo extends JFrame{ //设置窗体 private JPanel contentPane; //设置文本输入框 private JTextField jt; //数字按钮 JButton jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9,jb0,jb,j1,j2,j3,j4,j5; private boolean opflag = false; //用于标识是否输入运算操作符 private String txt = null; //显示结果的文本框的文本内容,用于存放计算结果的字符串形式 private String num_txt = ""; private boolean numflag = false; //用于标识是否输入数字 /** * Launch the application. */ public static void mainString[] args { EventQueue.invokeLaternew Runnable { public void run { try { Demo frame = new Demo; frame.setVisibletrue; } catch Exception e { e.printStackTrace; } } }; } /** * Create the frame. */ public Demo { swing; ActionListener; judgeAction; jt.setFontnew Font"宋体",Font.BOLD,80; jt.setEditablefalse; } private void judgeAction { //写减一的判断方法 j1.addActionListenernew ActionListener { public void actionPerformedActionEvent e { String s=jt.getText; //截取最后一个字符串 int a=s.length; String t=s.substring0, a-1; jt.setTextt; } }; //写退出的方法 j2.addActionListenernew ActionListener { public void actionPerformedActionEvent e { dispose; } }; //写清除的方法 j3.addActionListenernew ActionListener { public void actionPerformedActionEvent e { jt.setText""; } }; //写归零的方法 j4.addActionListenernew ActionListener { public void actionPerformedActionEvent e { jt.setText"00"; } }; //写校正的方法 .只能输入11位 j5.addActionListenernew ActionListener { public void actionPerformedActionEvent e { //获取字符串,再把转成合适的格式 String t=jt.getText; ift.length>11{ //把多余的截取 String str=t.substring0, 11; jt.setTextstr; } //使用 } }; } /** * 确定点击按钮事件,然后将数字显示到页面上 * 输入的一串数字连续 */ //都是独立的事件 private void ActionListener { jb1.addActionListenernew ActionListener { public void actionPerformedActionEvent e { numAction"1"; } }; jb2.addActionListenernew ActionListener { public void actionPerformedActionEvent e { numAction"2"; } }; jb3.addActionListenernew ActionListener { public void actionPerformedActionEvent e { numAction"3"; } }; jb4.addActionListenernew ActionListener { public void actionPerformedActionEvent e { numAction"4"; } }; jb5.addActionListenernew ActionListener { public void actionPerformedActionEvent e { numAction"5"; } }; jb6.addActionListenernew ActionListener { public void actionPerformedActionEvent e { numAction"6"; } }; jb7.addActionListenernew ActionListener { public void actionPerformedActionEvent e { numAction"7"; } }; jb8.addActionListenernew ActionListener { public void actionPerformedActionEvent e { numAction"8"; } }; jb9.addActionListenernew ActionListener { public void actionPerformedActionEvent e { numAction"9"; } }; jb0.addActionListenernew ActionListener { public void actionPerformedActionEvent e { numAction"0"; } }; jb.addActionListenernew ActionListener { public void actionPerformedActionEvent e { numAction"00"; } }; } public void numActionString string { ifnum_txt == "" && opflag == false { //判断是否num_txt是否为空,opflag是否为false,若满足这两个 jt.setTextnull; //条件,则为一次运算完毕,切新运算不以该次结果继续做运算。 num_txt = ""; } numflag = true; //只要输入一个数,则把numflag置为true,表示已有数字输入 num_txt += string; //把输入的数加入到最终要转换成都double运算的字符串 txt = jt.getText + string; //加到txt,使输入的数显示到文本框中。 jt.setTexttxt; } private void swing { setTitle"u6D4Bu8BD5"; setDefaultCloseOperationJFrame.EXIT_ON_CLOSE; setBounds100, 100, 698, 711; contentPane = new JPanel; contentPane.setBackgroundColor.ORANGE; contentPane.setBordernew EmptyBorder5, 5, 5, 5; setContentPanecontentPane; contentPane.setLayoutnull; JPanel panel = new JPanel; panel.setBackgroundColor.PINK; panel.setForegroundColor.RED; panel.setBounds30, 10, 630, 653; contentPane.addpanel; panel.setLayoutnull; jb1 = new JButton"1"; jb1.setForegroundColor.RED; jb1.setFontnew Font"Andalus", Font.BOLD, 60; jb1.setBounds31, 237, 133, 115; panel.addjb1; jb2 = new JButton"2"; jb2.setForegroundColor.RED; jb2.setFontnew Font"Andalus", Font.BOLD, 60; jb2.setBounds174, 237, 133, 115; panel.addjb2; jb3 = new JButton"3"; jb3.setForegroundColor.RED; jb3.setFontnew Font"Andalus", Font.BOLD, 60; jb3.setBounds317, 237, 133, 115; panel.addjb3; jb4 = new JButton"4"; jb4.setForegroundColor.RED; jb4.setFontnew Font"Andalus", Font.BOLD, 60; jb4.setBounds31, 375, 133, 115; panel.addjb4; jb5= new JButton"5"; jb5.setForegroundColor.RED; jb5.setFontnew Font"Andalus", Font.BOLD, 60; jb5.setBounds174, 375, 133, 115; panel.addjb5; jb6 = new JButton"6"; jb6.setForegroundColor.RED; jb6.setFontnew Font"Andalus", Font.BOLD, 60; jb6.setBounds317, 375, 133, 115; panel.addjb6; jb7 = new JButton"7"; jb7.setForegroundColor.RED; jb7.setFontnew Font"Andalus", Font.BOLD, 60; jb7.setBounds31, 510, 133, 115; panel.addjb7; jb8= new JButton"8"; jb8.setForegroundColor.RED; jb8.setFontnew Font"Andalus", Font.BOLD, 60; jb8.setBounds174, 510, 133, 115; panel.addjb8; jb9 = new JButton"9"; jb9.setForegroundColor.RED; jb9.setFontnew Font"Andalus", Font.BOLD, 60; jb9.setBounds317, 510, 133, 115; panel.addjb9; jt = new JTextField; jt.setBounds31, 35, 562, 115; panel.addjt; jt.setColumns10; j1 = new JButton"减一"; j1.setForegroundColor.BLUE; j1.setFontnew Font"宋体", Font.PLAIN, 50; j1.setBounds31, 160, 133, 68; j1.setMarginnew Insets0, 0, 0, 0; panel.addj1; j2 = new JButton"退出"; j2.setForegroundColor.BLUE; j2.setFontnew Font"宋体", Font.PLAIN, 50; j2.setBounds174, 160, 133, 68; j2.setMarginnew Insets0, 0, 0, 0; panel.addj2; j3= new JButton"清除"; j3.setForegroundColor.BLUE; j3.setFontnew Font"宋体", Font.PLAIN, 50; j3.setBounds317, 160, 133, 68; j3.setMarginnew Insets0, 0, 0, 0; panel.addj3; jb0 = new JButton"0"; jb0.setForegroundColor.RED; jb0.setFontnew Font"Andalus", Font.BOLD, 60; jb0.setBounds460, 510, 133, 115; panel.addjb0; jb = new JButton"00"; jb.setForegroundColor.RED; jb.setFontnew Font"宋体", Font.PLAIN, 60; jb.setBounds460, 375, 133, 115; panel.addjb; j4 = new JButton"归零"; j4.setForegroundColor.BLUE; j4.setFontnew Font"宋体", Font.PLAIN, 50; j4.setBounds460, 238, 133, 115; j4.setMarginnew Insets0, 0, 0, 0; panel.addj4; j5 = new JButton"校正"; j5.setForegroundColor.BLUE; j5.setFontnew Font"宋体", Font.PLAIN, 50; j5.setBounds460, 160, 133, 68; j5.setMarginnew Insets0, 0, 0, 0; panel.addj5; JPanel panel_1 = new JPanel; panel_1.setBackgroundColor.CYAN; panel_1.setBounds26, 160, 567, 464; panel.addpanel_1; }
}

本人代码

修改你的JPanel的布局管理器
默认的JPanel的布局管理器是FlowLayout,当容器变化时组件相对位置改变,但其大小不变

发表评论

电子邮件地址不会被公开。 必填项已用*标注