如何从另一个类调用ComponentResizer来调整JTextFields的大小?

时间:2018-11-10 23:24:02

标签: java swing

我目前正在一个项目中,我希望能够通过拖动鼠标而不是使用按钮来调整JTextField组件的大小,我遇到了Rob Camick的Resizing Components Class,但是我不确定如何在我的课堂上实现/调用它,并且希望得到任何帮助,我的代码在下面。预先感谢。

import java.awt.BorderLayout;
import java.awt.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
import java.io.*;



import javax.swing.*;

public class Sheet implements Runnable {


LinkedList<Field> fieldList = new LinkedList<Field>();
JFrame frame = new JFrame("Swing Testing...");
public void run() {



    Insets insets = frame.getInsets();


    frame.setLayout(null);





    JButton btn = new JButton("New");
    JMenuBar menuBar = new JMenuBar();
    JButton saveBtn = new JButton("Save");
    JButton loadBtn = new JButton("Load");
    JButton loadTempBtn = new JButton("Load From Template");
    Dimension size = btn.getPreferredSize();

    btn.setBounds(insets.left,insets.top,
             size.width, size.height);
    btn.setLocation(25, 50);

    menuBar.setBounds(insets.left,insets.top,
             size.width, size.height);

    size = saveBtn.getPreferredSize();
    saveBtn.setBounds(insets.left,insets.top,
             size.width, size.height);
    saveBtn.setLocation(500, 30);

    size = loadBtn.getPreferredSize();
    loadBtn.setBounds(insets.left,insets.top,
             size.width, size.height);
    loadBtn.setLocation(550, 30);
    size = loadTempBtn.getPreferredSize();
    loadTempBtn.setBounds(insets.left,insets.top,
             size.width, size.height);
    loadTempBtn.setLocation(600, 30);


    btn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            btn.setText("You Clicked me!");

            FieldTypeFrame(e);




        }
    });

    saveBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            save(arg0);

        }



    });

    loadBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            load(arg0, frame);

        }



    });

    loadTempBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            loadFromTemp(arg0, frame);

        }
    });


    menuBar.setSize(1000, 25);


    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.addComponentListener(new ComponentListener() {

        @Override
        public void componentHidden(ComponentEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void componentMoved(ComponentEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void componentResized(ComponentEvent arg0) {
            menuBar.setSize(arg0.getComponent().getWidth(), 25);

        }

        @Override
        public void componentShown(ComponentEvent arg0) {
            // TODO Auto-generated method stub

        }


    });


    frame.setSize(1000, 1000);
    frame.add(btn);
    frame.add(menuBar);
    frame.add(saveBtn);
    frame.add(loadBtn);
    frame.setVisible(true);

}



public static void main(String args[]) {

    SwingUtilities.invokeLater(new Sheet());


}

public void save(ActionEvent e) {




    try {
        FileOutputStream fileStream = new FileOutputStream("page.dat");
        ObjectOutputStream objStream = new ObjectOutputStream(fileStream);
        objStream.writeObject(fieldList);
        objStream.close();

        System.out.println("Done");


    }
    catch (Exception error) {

        error.printStackTrace();

    }



}


public void load(ActionEvent e, JFrame frame) {
    int i = 0;

    try {
        LinkedList<Field> list;
        FileInputStream inputStream = new FileInputStream("page.dat");
        ObjectInputStream objInput = new ObjectInputStream(inputStream);
        list = (LinkedList<Field>)objInput.readObject();
        objInput.close();




            for(int j = 0; j < list.size(); j++) {

            Field f = list.get(j);
            f.setFrame(frame);
            f.initializeButtons();
            f.add();
        }

        fieldList = list;
        System.out.println("Done");


    }
    catch(Exception error) {


    }



}



public void loadFromTemp(ActionEvent e, JFrame frame) {
    int i = 0;

    try {
        LinkedList<Field> list;
        FileInputStream inputStream = new FileInputStream("Template.dat");
        ObjectInputStream objInput = new ObjectInputStream(inputStream);
        list = (LinkedList<Field>)objInput.readObject();
        objInput.close();




        for(int j = 0; j < list.size(); j++) {

            Field f = list.get(j);
            f.setFrame(frame);
            f.initializeButtons();
            f.add();
            f.revalidate();
        }

        fieldList = list;
        System.out.println("Done");


    }
    catch(Exception error) {


    }



}

public void SaveAs(ActionEvent e) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override 
        public void run() {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            chooser.showSaveDialog(null);

        }


    });


}

public void FieldTypeFrame(ActionEvent e) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {


            JFrame fieldType = new JFrame("FieldType");
            JButton textBtn = new JButton("Text");
            JButton dataBtn = new JButton("Data");
            JButton checkBtn = new JButton("Check");


            textBtn.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    TextField f = new TextField(frame, 50, 50, 200, 150, "Type Here", "Text Box", "TEXT");
                    fieldList.add(f);       
                    f.add();
                    fieldType.dispose();

                }
            });

            dataBtn.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    DataField f = new DataField(frame, 50, 50, 200, 150, "0", "Data Box", "DATA");
                    fieldList.add(f);       
                    f.add();
                    fieldType.dispose();
                }
            });

            checkBtn.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {

                    JSlider howMany = new JSlider(0, 25, 0);
                    JButton confirm = new JButton("Confirm");
                    howMany.setMajorTickSpacing(5);
                    howMany.setMinorTickSpacing(1);
                    howMany.setPaintTicks(true);
                    howMany.setPaintLabels(true);

                    confirm.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            CheckField f = new CheckField(frame, 50, 50, 200, 150, "", "Check Box", "CHECK", howMany.getValue());
                            fieldList.add(f);
                            f.add();
                            fieldType.dispose();
                        }



                    });

                    fieldType.remove(textBtn);
                    fieldType.remove(dataBtn);


                    fieldType.add(howMany);
                    fieldType.add(confirm);
                    fieldType.revalidate();
                    /*
                    Field f = new Field(frame, 50, 50, 200, 150, "Type Here!", "Check Box", "CHECK");
                    fieldList.add(f);       
                    f.add();
                    fieldType.dispose();
                    */
                }
            });


            fieldType.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            fieldType.setSize(300, 300);
            fieldType.setLayout(new FlowLayout());

            fieldType.add(textBtn);
            fieldType.add(dataBtn);
            fieldType.add(checkBtn);





            fieldType.setVisible(true);

        }

    });

}



}

1 个答案:

答案 0 :(得分:0)

ComponentResize设计为仅在使用空布局的面板上工作,因为您实际上是在控制组件的大小和位置。

因此,假设您要将组件添加到空白框中,您将执行以下操作:

frame.setLayout(null);

JTextField textField = new JTextField(10);
textField.setSize( textField.getPreferredSize() );
textField.setLocation(100, 100);
frame.add(textField);

ComponentResizer cr = new ComponentResizer();
cr.registerComponent(textField);

当您尝试学习新课程或新概念时,请从简单的事情开始。您发布的代码过于复杂,无法学习如何使用此类。您只需要一个JFrame和一个JTextField即可测试该类。