我遇到了JFrame文本字段的问题 我正在尝试制作文字游戏,但问题是当我尝试创建文本字段来设置输入然后检查它是否是正确的单词时,我已经覆盖了单词。 问题是,当我尝试添加Textfield时,它会在我向JFrame渲染时消失。
public teksti() {
setTitle("Hirsipuu");
setSize(leveys,korkeus);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
setBackground(Color.white);
jp.add(tf); // adding JtextField (JTextField tf = new JTextField(30);)
add(jp);
}
和我的render()
就像这样(仅用于测试目的)。
private void render() {
BufferStrategy bs = this.getBufferStrategy(); // tehdään uusi bufferi
if (bs == null) {
createBufferStrategy(3);
return;
}
int R = (int) (Math.random( )*256);
int G = (int)(Math.random( )*256);
int B= (int)(Math.random( )*256);
Color randomColor = new Color(R, G, B);
Graphics g = bs.getDrawGraphics();
g.drawString("Arvaa sana", 100 , 100);
g.setColor(Color.white);
g.fillRect(0, 50, leveys, korkeus);
g.setColor(randomColor);
g.setFont(h);
// g.drawLine(0,0,liikey*2-1,liikex);
for(int i = 0; i < salat.size(); i ++) {
g.drawString(salat.get(i),liikex+rand.nextInt(50),liikey+rand.nextInt(50));
}
System.out.println(liikex + " " + liikey);
g.dispose();
bs.show();
g.dispose();
}
我可以让文本字段在开头显示在顶部,但随后它就会消失 谁知道有没有更好的方法呢?
答案 0 :(得分:3)
例如:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import javax.swing.*;
public class BackgroundPanel extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = 600;
private static final Font PROMPT_FONT = new Font(Font.SANS_SERIF, Font.BOLD,
24);
private Paint gradientPaint;
private JTextField textField = new JTextField(20);
public BackgroundPanel() {
int red = (int) (Math.random() * 256);
int green = (int) (Math.random() * 256);
int blue = (int) (Math.random() * 256);
Color color1 = new Color(red, green, blue);
red = (int) (Math.random() * 256);
green = (int) (Math.random() * 256);
blue = (int) (Math.random() * 256);
Color color2 = new Color(red, green, blue);
gradientPaint = new GradientPaint(0, 0, color1, 20, 20, color2, true);
JLabel promptLabel = new JLabel("Input:");
promptLabel.setFont(PROMPT_FONT);
add(promptLabel);
add(textField);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(gradientPaint);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.black);
g.setFont(PROMPT_FONT);
g.drawString("Arvaa sana", 100, 100);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
BackgroundPanel mainPanel = new BackgroundPanel();
JFrame frame = new JFrame("BackgroundPanel");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
答案 1 :(得分:0)
这基本上是我现在完成的全部事情。
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class teksti extends JFrame implements Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
ArrayList<String> sanat = new ArrayList<String>();
ArrayList<String> salat = new ArrayList<String>();
String dir = System.getProperty("user.dir");
String pathname = dir + "\\src\\kotus_sanat.txt";
Random rand = new Random();
Scanner lukija = new Scanner(System.in);
String syote,salasana,salasana2;
int leveys=500,korkeus=500;
int liikex=300, liikey=300;
Font h = new Font("Helvetica", Font.PLAIN, 18);
JTextField tf = new JTextField(30);
JPanel jp = new JPanel();
JFrame jf = new JFrame();
public teksti() {
setTitle("Hirsipuu");
setSize(leveys,korkeus);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
setBackground(Color.white);
jp.add(tf);
add(jp);
}
public static void main(String args[]){
teksti teksti = new teksti();
teksti.start();
}
private void start() {
run();
}
public void run() {
sanat = lataa();
System.out.println(pathname);
System.out.println(sanat.size() + " sanaa ladattu..");
int sanoja=sanat.size();
for (int i = 0; i < 10; i++){
salat.add(sanat.get(rand.nextInt(sanoja)));
}
System.out.println("salasana on " + salasana);
long lastTime = System.nanoTime(); // fps sälää
long timer = System.currentTimeMillis();
final double ns = 1000000000.0 / 60.0;
double delta = 0;
int frames = 0;
int updates = 0;
requestFocus();
boolean running=true;
while (running){
long now = System.nanoTime();
delta += (now-lastTime) / ns;
lastTime = now;
while ( delta >= 1) {
update();
updates++;
delta--;
}
render();
frames++;
if (System.currentTimeMillis() - timer > 1000){
timer += 1000;
setTitle("Hirsipuu"+ " | " + updates + " ups " + frames + " fps" );
updates = 0;
frames = 0;
}
}
}
private void update() {
liikex = liikex + 1;
liikey= liikey - 4;
if (liikey>korkeus)
liikey = 1;
if (liikex>leveys)
liikex= 1;
if (liikey<20)
liikey = korkeus;
}
private void render() {
BufferStrategy bs = this.getBufferStrategy(); // tehdään uusi bufferi
if (bs == null) {
createBufferStrategy(3);
return;
}
int R = (int) (Math.random( )*256);
int G = (int)(Math.random( )*256);
int B= (int)(Math.random( )*256);
Color randomColor = new Color(R, G, B);
Graphics g = bs.getDrawGraphics();
g.drawString("Arvaa sana", 100 , 100);
g.setColor(Color.white);
g.fillRect(0, 50, leveys, korkeus);
g.setColor(randomColor);
g.setFont(h);
// g.drawLine(0,0,liikey*2-1,liikex);
for(int i = 0; i < salat.size(); i ++) {
g.drawString(salat.get(i), liikex+rand.nextInt(50),liikey+rand.nextInt(50));
}
System.out.println(liikex + " " + liikey);
g.dispose();
bs.show();
g.dispose();
}
private void stop() {
}
private void vertaa() {
System.out.println("Anna sana niin tarkastaan onko se oikea suomenkielinen sana: ");
syote = lukija.next();
boolean oikea = false;
int i = 0;
int z = 0;
while (i < sanat.size()) {
if (syote.equals(sanat.get(i))) {
oikea = true;
System.out.println (syote + " on oikea suomalainen sana.");
break;
}
else{
}
}
if (!oikea) {
System.out.println(syote + " ei ole oikea suomalainen sana.");
}
}
public ArrayList<String> lataa() {
String line = null;
try {
BufferedReader reader = new BufferedReader(new FileReader(pathname));
while((line = reader.readLine()) != null){
sanat.add(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return sanat;
}
}
vertaa() is not used.