gpt4 book ai didi

Java Graphics2d : text field

转载 作者:行者123 更新时间:2023-12-01 12:37:51 25 4
gpt4 key购买 nike

我搜索了很多主题,但没有找到任何有具体答案的主题。我需要在 Graphics2D 上输入字段数据(例如 JTextFieldTextField)。

有人知道如何创建这个组件吗?或者在 Graphics2D 中放置一个文本字段(Component)?

谢谢!

最佳答案

不太确定您要什么,但这里有一些代码,当您双击时,它将向面板添加一个文本字段。然后您可以将文本添加到文本字段。

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

public class InvisibleTextField extends JTextField
implements ActionListener, FocusListener, MouseListener, DocumentListener
{
public InvisibleTextField()
{
setOpaque( false );
setColumns( 1 );
setBorder( null );
setSize( getPreferredSize() );
setColumns( 0 );
addActionListener( this );
addFocusListener( this );
addMouseListener( this );
getDocument().addDocumentListener( this );
}

// Implement ActionListener

public void actionPerformed(ActionEvent e)
{
setEditable( false );
}

// Implement FocusListener

public void focusLost(FocusEvent e)
{
setEditable( false );
}

public void focusGained(FocusEvent e) {}

// Implement MouseListener

public void mouseClicked( MouseEvent e )
{
if (e.getClickCount() == 2)
setEditable( true );
}

public void mouseEntered( MouseEvent e ) {}

public void mouseExited( MouseEvent e ) {}

public void mousePressed( MouseEvent e ) {}

public void mouseReleased( MouseEvent e ) {}

// Implement DocumentListener

public void insertUpdate(DocumentEvent e)
{
updateSize();
}

public void removeUpdate(DocumentEvent e)
{
updateSize();
}

public void changedUpdate(DocumentEvent e) {}

private void updateSize()
{
setSize( getPreferredSize() );
}

public static void main(String[] args)
{
JPanel panel = new JPanel();
panel.setFocusable( true );
panel.setLayout( null );
panel.addMouseListener( new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
JPanel panel = (JPanel)e.getSource();

if (e.getClickCount() == 1)
{
panel.requestFocusInWindow();
}

if (e.getClickCount() == 2)
{
InvisibleTextField tf = new InvisibleTextField();
tf.setText("Enter Text");
tf.setLocation(e.getPoint());
panel.add( tf );
tf.requestFocusInWindow();
tf.selectAll();
}
}
});

JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add(new JLabel("Double Click to Add Text"), BorderLayout.NORTH);
frame.add(panel);
frame.setSize(650, 300);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}

关于Java Graphics2d : text field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25411278/

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