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 main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Demo frame = new Demo();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Demo() {
        swing();
        ActionListener();
        judgeAction();
        jt.setFont(new Font("宋体",Font.BOLD,80));
        jt.setEditable(false);
        
    }
    
    private void judgeAction() {
        //写减一的判断方法
        j1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String s=jt.getText();
                //截取最后一个字符串
                int a=s.length();
                String t=s.substring(0, a-1);
                jt.setText(t);
            }
        });
        //写退出的方法
        j2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    dispose();       
            }
        });
        //写清除的方法
        j3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jt.setText("");
                
            }
        });
        //写归零的方法
        j4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jt.setText("00");
                
            }
        });
        //写校正的方法 .只能输入11位
        j5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            //获取字符串,再把转成合适的格式
                String t=jt.getText();
                if(t.length()>11){
                    //把多余的截取
                    String str=t.substring(0, 11);
                    jt.setText(str);
                }
                
                //使用
            }
        });
        
    }

    /**
     * 确定点击按钮事件,然后将数字显示到页面上 
     * 输入的一串数字连续
     */
    //都是独立的事件
    private void ActionListener() {
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numAction("1");
                
            }
        });
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numAction("2");
            }
        });
        jb3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numAction("3");
            }
        });
        jb4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numAction("4");
            }
        });
        jb5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numAction("5");
            }
        });
        jb6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numAction("6");
            }
        });
        jb7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numAction("7");
            }
        });
        jb8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numAction("8");
            }
        });
        jb9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numAction("9");
            }
        });
        jb0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numAction("0");
            }
        });
        jb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numAction("00");
            }
        });
    }

    public void numAction(String string) {
         if(num_txt == "" && opflag == false) {    //判断是否num_txt是否为空,opflag是否为false,若满足这两个
                jt.setText(null);                 //条件,则为一次运算完毕,切新运算不以该次结果继续做运算。
                num_txt = "";
            } 
            numflag = true;                           //只要输入一个数,则把numflag置为true,表示已有数字输入
            num_txt += string;                           //把输入的数加入到最终要转换成都double运算的字符串
            txt = jt.getText() + string;             //加到txt,使输入的数显示到文本框中。
            jt.setText(txt);
        
    }
    
    
     private void swing() {
        setTitle("\u6D4B\u8BD5");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 698, 711);
        contentPane = new JPanel();
        contentPane.setBackground(Color.ORANGE);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JPanel panel = new JPanel();
        panel.setBackground(Color.PINK);
        panel.setForeground(Color.RED);
        panel.setBounds(30, 10, 630, 653);
        contentPane.add(panel);
        panel.setLayout(null);
        
        jb1 = new JButton("1");
        jb1.setForeground(Color.RED);
        jb1.setFont(new Font("Andalus", Font.BOLD, 60));
        jb1.setBounds(31, 237, 133, 115);
        panel.add(jb1);
        
        jb2 = new JButton("2");
        jb2.setForeground(Color.RED);
        jb2.setFont(new Font("Andalus", Font.BOLD, 60));
        jb2.setBounds(174, 237, 133, 115);
        
        panel.add(jb2);
        
        jb3 = new JButton("3");
        jb3.setForeground(Color.RED);
        jb3.setFont(new Font("Andalus", Font.BOLD, 60));
        jb3.setBounds(317, 237, 133, 115);
        
        panel.add(jb3);
        
        jb4 = new JButton("4");
        jb4.setForeground(Color.RED);
        jb4.setFont(new Font("Andalus", Font.BOLD, 60));
        jb4.setBounds(31, 375, 133, 115);
        panel.add(jb4);
        
        jb5= new JButton("5");
        jb5.setForeground(Color.RED);
        jb5.setFont(new Font("Andalus", Font.BOLD, 60));
        jb5.setBounds(174, 375, 133, 115);
        panel.add(jb5);
        
        jb6 = new JButton("6");
        jb6.setForeground(Color.RED);
        jb6.setFont(new Font("Andalus", Font.BOLD, 60));
        jb6.setBounds(317, 375, 133, 115);
        panel.add(jb6);
        
        jb7 = new JButton("7");
        jb7.setForeground(Color.RED);
        jb7.setFont(new Font("Andalus", Font.BOLD, 60));
        jb7.setBounds(31, 510, 133, 115);
        panel.add(jb7);
        
        jb8= new JButton("8");
        jb8.setForeground(Color.RED);
        jb8.setFont(new Font("Andalus", Font.BOLD, 60));
        jb8.setBounds(174, 510, 133, 115);
        panel.add(jb8);
        
        jb9 = new JButton("9");
        jb9.setForeground(Color.RED);
        jb9.setFont(new Font("Andalus", Font.BOLD, 60));
        jb9.setBounds(317, 510, 133, 115);
        panel.add(jb9);
        
        jt = new JTextField();
        jt.setBounds(31, 35, 562, 115);
        panel.add(jt);
        jt.setColumns(10);
        
        j1 = new JButton("减一");
        j1.setForeground(Color.BLUE);
        j1.setFont(new Font("宋体", Font.PLAIN, 50));
        j1.setBounds(31, 160, 133, 68);
        j1.setMargin(new Insets(0, 0, 0, 0));
        panel.add(j1);
        
        j2 = new JButton("退出");
        j2.setForeground(Color.BLUE);
        j2.setFont(new Font("宋体", Font.PLAIN, 50));
        j2.setBounds(174, 160, 133, 68);
        j2.setMargin(new Insets(0, 0, 0, 0));
        panel.add(j2);
        
        j3= new JButton("清除");
        j3.setForeground(Color.BLUE);
        j3.setFont(new Font("宋体", Font.PLAIN, 50));
        j3.setBounds(317, 160, 133, 68);
        j3.setMargin(new Insets(0, 0, 0, 0));
        panel.add(j3);
        
        jb0 = new JButton("0");
        jb0.setForeground(Color.RED);
        jb0.setFont(new Font("Andalus", Font.BOLD, 60));
        jb0.setBounds(460, 510, 133, 115);
        panel.add(jb0);
        
        jb = new JButton("00");
        jb.setForeground(Color.RED);
        jb.setFont(new Font("宋体", Font.PLAIN, 60));
        jb.setBounds(460, 375, 133, 115);
        panel.add(jb);
        
        j4 = new JButton("归零");
        j4.setForeground(Color.BLUE);
        j4.setFont(new Font("宋体", Font.PLAIN, 50));
        j4.setBounds(460, 238, 133, 115);
        j4.setMargin(new Insets(0, 0, 0, 0));
        panel.add(j4);
        
        j5 = new JButton("校正");
        j5.setForeground(Color.BLUE);
        j5.setFont(new Font("宋体", Font.PLAIN, 50));
        j5.setBounds(460, 160, 133, 68);
        j5.setMargin(new Insets(0, 0, 0, 0));
        panel.add(j5);
        
        JPanel panel_1 = new JPanel();
        panel_1.setBackground(Color.CYAN);
        panel_1.setBounds(26, 160, 567, 464);
        panel.add(panel_1);
    }
}

本人代码

阅读 3.3k
1 个回答

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏