gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:03:15 26 4
gpt4 key购买 nike

我目前正在开发一个项目,我希望能够通过拖动鼠标而不是使用按钮来调整 JTextField 组件的大小,并且我遇到了 Rob Camick 的 Resizing Components Class但我不确定如何在我的类(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);

}

});

}



}

最佳答案

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);

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

关于java - 如何从另一个类调用 ComponentResizer 来调整 JTextField 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53244399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com