gpt4 book ai didi

java - 双击 TitledBorder 即可编辑

转载 作者:行者123 更新时间:2023-12-02 02:02:07 24 4
gpt4 key购买 nike

这是: Make a TitledBorder title editable 的扩展

更具体地说,我希望在双击标题时可以编辑 TitledBorder。我将边框放置在使用 BoxLayout 的 java Box 周围。双击最好在此处打开一个 JTextField,但如果无法做到这一点,则可以打开另一个窗口来编辑标题。

TitledBorder editableBorder = new TitledBorder(editableString);
editableBorder.setTitleJustification(TitledBorder.CENTER);

Box containerBox = new Box(BoxLayout.PAGE_AXIS);
containerBox.setBorder(new CompoundBorder(editableBorder, new EmptyBorder(10, 0, 10, 0)));

Box insideBox = new Box(BoxLayout.PAGE_AXIS);
insideBox.add(new JLabel(new ImageIcon(new BufferedImage(200,40,BufferedImage.TYPE_INT_RGB))));
containerBox.add(insideBox);

最佳答案

这是一个例子。 MouseListener 使用 JPopupMenu 来显示文本字段。这意味着您可以使用 Escape 键取消编辑。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class TitledBorderListener extends MouseAdapter
{
private JPopupMenu editPopup;
private JTextField editTextField;
private TitledBorder titledBorder;

@Override
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount != 2)
return;

// Edit the border on a double click

JComponent component = (JComponent)e.getSource();
Border border = component.getBorder();

if (border instanceof TitledBorder)
{
titledBorder = (TitledBorder)border;
FontMetrics fm = component.getFontMetrics( titledBorder.getTitleFont() );
int titleWidth = fm.stringWidth(titledBorder.getTitle()) + 20;
Rectangle bounds = new Rectangle(0, 0, titleWidth, fm.getHeight());

if (bounds.contains(e.getPoint()))
{
if (editPopup == null)
createEditPopup();

// Position the popup editor over top of the title

editTextField.setText( titledBorder.getTitle() );
Dimension d = editTextField.getPreferredSize();
d.width = titleWidth;
editPopup.setPreferredSize(d);
editPopup.show(component, 0, 0);

editTextField.selectAll();
editTextField.requestFocusInWindow();
}
}
}

private void createEditPopup()
{
editTextField = new JTextField();

// Add an Action to the text field to save the new title text

editTextField.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String value = editTextField.getText();
titledBorder.setTitle( value );
editPopup.setVisible(false);
editPopup.getInvoker().revalidate();
editPopup.getInvoker().repaint();
}
});

// Add the editor to the popup

editPopup = new JPopupMenu();
editPopup.setBorder( new EmptyBorder(0, 0, 0, 0) );
editPopup.add(editTextField);
}

private static void createAndShowUI()
{

JPanel panel = new JPanel();
panel.setBorder( new TitledBorder("Double Click to Edit") );
panel.addMouseListener( new TitledBorderListener() );

JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.setSize(200, 200);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}

关于java - 双击 TitledBorder 即可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51426288/

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