我正在使用jtextfield,但它没有出现。从搞乱代码我认为它被吸引了。我在文本字段中遇到的另一个问题是,当我重新验证时,它会将其大小从我需要的内容更改为整个屏幕。我通过在循环中设置大小来解决这个问题。如果有更好的方法,我会喜欢它,但我的首要任务是首先看到它。这是代码。提前谢谢。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clientgui;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JTextField;
/**
*
* @author Zzzz
*/
public class ClientGUI extends JFrame{
/**
* @param args the command line arguments
*/
Socket s ;
PrintWriter out;
String username;
BufferedReader in;
ListeningThread lt;
Image dbi;
Graphics dbg;
JTextField textField;
BufferedImage background;
public ClientGUI() throws IOException, InterruptedException
{
background = ImageIO.read(new File("Background.png"));
s = new Socket("127.0.0.1", 4382);
out = new PrintWriter(s.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
lt = new ListeningThread(s);
lt.start();
setTitle("Simple Chat");
setSize(500,500);
setDefaultCloseOperation(3);
setVisible(true);
textField = new JTextField();
textField.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
out.println(textField.getText());
}
});
add(textField);
while(true)
{
textField.setBounds(100, 420, 325, 25);
textField.repaint();
Thread.sleep(20);
}
}
@Override
public void paint(Graphics g)
{
dbi = createImage(getWidth(), getHeight());
dbg = dbi.getGraphics();
paintComponent(dbg);
g.drawImage(dbi, 0, 0, rootPane);
}
public void paintComponent(Graphics g)
{
g.setColor(Color.white);
g.drawImage(background, 0, 0, rootPane);
g.drawString((String) ListeningThread.messages.get(0), 50, 65);
repaint();
}
public static void main(String[] args) throws IOException, InterruptedException {
new ClientGUI();
}
}
答案 0 :(得分:2)
关于Swing和AWT如何在Oracle网站上进行绘画,有一个great article。我强烈建议你阅读它。在关于Swing绘画方法的section中,它解释了你所面临的错误。
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.3.1/css/buttons.dataTables.min.css"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/fixedheader/3.1.2/css/fixedHeader.dataTables.min.css"/>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.3.1/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.3.1/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/fixedheader/3.1.2/js/dataTables.fixedHeader.min.js"></script>
(大错误)基本上,paint()
是重新绘制所有内容的方法,而paint()
是放置自定义代码的地方。 paintComponent()
实际调用这些函数(按顺序):
paint()
protected void paintComponent(Graphics g)
protected void paintBorder(Graphics g)
当您覆盖绘画时,您会阻止protected void paintChildren(Graphics g)
完全绘制文本框。引用文章:
Swing程序应该覆盖
paintChildren()
而不是覆盖paintComponent()
paint()
(小错误)同一篇文章也有关于重绘正常如何运作的部分。查看它的一种非常简单的方法是,当AWT事件处理线程到达时,调用repaint()
会在不久的将来调度对repaint()
的调用。您无需在paint()
内拨打repaint()
。
paintComponent()
中的所有内容放入paint()
。paintComponent()
。