gpt4 book ai didi

java - 如何添加 JTextField

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

我想添加 JTextField。我的 JFrame 包含一个 JButton 和一个 JTextField 。首先,我添加了 JButton,并且它正在工作。但没有看到 JTextField。

import java.awt.*;import java.awt.event.*import java.awt.geom.*;import javax.swing.*;import java.sql.*;import java.util.Random;import java.awt.geom.*;    
public class DrawMap extends JFrame{//private JLabel LabelTitle,MapNo;Private JTextField MapField;private final JButton Load,Back,Logout;private final JPanel DrawPanel;private String UserName,Password,City;public boolean check;private int r,g,b;private int flag =0;private Shape shape;private final int w = 20;


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.sql.*;
import java.util.Random;
import java.awt.geom.*;

public class DrawMap extends JFrame{
//private JLabel LabelTitle,MapNo;
private JTextField MapField;
private final JButton Load,Back,Logout;
private final JPanel DrawPanel;
private String UserName,Password,City;
public boolean check;
private int r,g,b;
private int flag =0;
private Shape shape;
private final int w = 20;
private final int h = 20;

private Object lastButtonPressed;

public DrawMap(String UserName,String Password,String City){

setTitle("Draw Map");
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Random p = new Random();
//r = p.nextInt(255);
//g = p.nextInt(255);
//b = p.nextInt(255);

this.UserName = UserName;
this.Password = Password;
this.City = City;

final Container contentPane = getContentPane();

JPanel buttonPanel = new JPanel();
JPanel buttonPanel1 = new JPanel();
JPanel TextField = new JPanel();

/*Draw = new JButton("Draw");
Shape = new JButton("Shape");*/
Load = new JButton("Load");
Back = new JButton("Back");
Logout = new JButton("Logout");

MapField = new JTextField();
//MapField.setBounds(130,130,100,30);

TextField.add(MapField);

//buttonPanel.add(Draw);
//buttonPanel.add(Shape);
buttonPanel1.add(Load);
buttonPanel1.add(Back);
buttonPanel1.add(Logout);

contentPane.add(buttonPanel, BorderLayout.NORTH);
contentPane.add(buttonPanel1, BorderLayout.SOUTH);
contentPane.add(TextField, BorderLayout.CENTER);

DrawPanel = new JPanel(){

};

contentPane.add(DrawPanel, BorderLayout.CENTER);

final ActionListener buttonPressed = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
lastButtonPressed = event.getSource();
}
};

//Draw.addActionListener(buttonPressed);
//Shape.addActionListener(buttonPressed);
Load.addActionListener(buttonPressed);
Back.addActionListener(buttonPressed);
Logout.addActionListener(buttonPressed);

contentPane.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

int x = e.getX();
int y = e.getY();


if (lastButtonPressed == Load){
//shape = new Rectangle2D.Double(x, y, w, w);
//echo("Square",x,y);
//color = new Color(0,0,0);

}
else if (lastButtonPressed == Back){
UserHome ush = new UserHome(UserName,Password,City);
ush.setVisible(true);
DrawMap.this.setVisible(false);
}
else if (lastButtonPressed == Logout){
Login L = new Login();
L.setVisible(true);
DrawMap.this.setVisible(false);
}

//DrawPanel.repaint();


}

});

}

}

最佳答案

基本上,您已将两个组件添加到同一位置...

contentPane.add(TextField, BorderLayout.CENTER);

DrawPanel = new JPanel() {

};

contentPane.add(DrawPanel, BorderLayout.CENTER);

由于 BorderLayout 的工作方式,它只会管理添加到该位置的最后一个组件。

您可能需要考虑使用复合布局,即将按钮和文本字段添加到单独的容器中,然后可以将其与 DrawPanel 一起添加到框架中

您共同的支持之湖Coding Conventions很难推断出你的代码在做什么。这些约定的存在是为了让您更容易理解其他人的代码,也让其他人更容易理解您的代码,我建议花时间尝试学习它们

关于java - 如何添加 JTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50749321/

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