0
A
回答
3
你可以重新定义自己的类,它扩展了JTextField
类,让你想要的风格。
编辑:
我创造你想要什么一个小例子。这只是一个草案,可以改进,但你现在有一个起点。
public class MainPanel extends JPanel{
public MainPanel() {
super(new BorderLayout());
final JPanel p = new JPanel(new GridLayout(5,1,5,5));
final JTextPane tp = new JTextPane();
final StyledDocument doc = tp.getStyledDocument();
final SimpleAttributeSet attr = new SimpleAttributeSet();
JTextField textField01 = new JTextField(20) {
//Unleash Your Creativity with Swing and the Java 2D API!
//http://java.sun.com/products/jfc/tsc/articles/swing2d/index.html
@Override protected void paintComponent(Graphics g) {
if(!isOpaque()) {
int w = getWidth();
int h = getHeight();
Graphics2D g2 = (Graphics2D)g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(UIManager.getColor("TextField.background"));
g2.fillRoundRect(0, 0, w-1, h-1, h, h);
g2.setColor(Color.GRAY);
g2.drawRoundRect(0, 0, w-1, h-1, h, h);
g2.dispose();
}
super.paintComponent(g);
}
};
textField01.setOpaque(false);
textField01.setBackground(new Color(0,0,0,0));
textField01.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));
textField01.setText("Test");
textField01.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
// TODO: Do something for the keyTyped event
}
public void keyPressed(KeyEvent e) {
// TODO: Do something for the keyPressed event
if(e.getKeyCode() == KeyEvent.VK_ENTER){
JTextField textField = (JTextField) e.getSource();
String text = textField.getText();
textField.setText("");
JLabel label = new JLabel(text);
label.setOpaque(true);
label.setBackground(Color.lightGray);
label.setBorder(BorderFactory.createLineBorder(Color.black,1));
tp.setCaretPosition(tp.getDocument().getLength());
tp.insertComponent(label);
try {
doc.insertString(doc.getLength(), " ", attr);
}
catch (BadLocationException ex){
ex.printStackTrace();
}
}
}
});
p.add(textField01);
p.add(tp);
add(p);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
setPreferredSize(new Dimension(320, 200));
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame("Tags editor");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class RoundedCornerBorder extends AbstractBorder {
@Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D)g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int r = height-1;
RoundRectangle2D round = new RoundRectangle2D.Float(x, y, width-1, height-1, r, r);
Container parent = c.getParent();
if(parent!=null) {
g2.setColor(parent.getBackground());
Area corner = new Area(new Rectangle2D.Float(x, y, width, height));
corner.subtract(new Area(round));
g2.fill(corner);
}
g2.setColor(Color.GRAY);
g2.draw(round);
g2.dispose();
}
@Override public Insets getBorderInsets(Component c) {
return new Insets(4, 8, 4, 8);
}
@Override public Insets getBorderInsets(Component c, Insets insets) {
insets.left = insets.right = 8;
insets.top = insets.bottom = 4;
return insets;
}
}
相关问题
- 1. Highcharts Bubble with text
- 2. Bubble Sort Display
- 3. Bubble Sort Strings
- 4. Bubble sort descending
- 5. jQuery Event Bubble
- 6. Java Bubble Sort Trouble
- 7. Bubble Sort 2D Array
- 8. AVX和Bubble Sort
- 9. Java String Bubble Sorting
- 10. Flex Packed Bubble Chart
- 11. Bubble sort arraylist
- 12. Bubble Plot - R
- 13. Iphone Speech Bubble
- 14. Pascal Bubble排序
- 15. Promotion/bubble-wrap TableScreen data
- 16. C++ Bubble Sort Negative Numbers
- 17. Google Bubble Chart标签
- 18. C#Event Passing/Bubble Up
- 19. Bubble Sort Outer Loop and N-1
- 20. Bubble Sort in C array swapping
- 21. Bubble-sort算法的分析
- 22. jquery bubble pop up not working
- 23. Bubble排序错误输出
- 24. JQuery Bubble Popup:innerhtml隐藏div div
- 25. Java Bubble Sort Acting奇怪
- 26. Calculate Big Theta Notation Bubble排序
- 27. JSQMessagesCollectionView(Message Bubble size all identical ..)
- 28. 从PipedOutputStream到PipedInputStream的Bubble IOException
- 29. Eclipse Android Text Text Text = String text
- 30. JTextField BeansBinding
有趣。我正在寻找更多的方法来创建一个带有所有泡泡的JTextField。我想我可以有一个四舍五入的JLabels的JPanel和一个JTextField来输入东西。 – nrubin29
你能更准确地知道你想达到什么吗? –
基本上,应用程序的工作方式是输入一些文本并按','键,并将该文本放在泡泡内。您可以左右移动气泡,而不是单个字母。按删除删除整个泡泡。还有其他问题吗? – nrubin29