java java更改文本字段的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26822626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java change size of Textfield
提问by kees mees
I want to make a frame where you have to enter your name and stuff. I try to make it like, name: textfield, next line, age: textfieldand so on... But the textfield is really high. If I add 2 textfields in one panel in the frame, one textfield contains half of my screen. I try to fix this with a layout manager but I have no idea how.
我想制作一个框架,您必须在其中输入您的姓名和内容。我试着让它像,名称:文本字段,下一行,年龄:文本字段等等......但文本字段真的很高。如果我在框架的一个面板中添加 2 个文本字段,则一个文本字段包含我屏幕的一半。我尝试使用布局管理器解决此问题,但我不知道如何解决。
Could someone please help me?
有人可以帮助我吗?
Here's my code:
这是我的代码:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class GUI{
public static void main(String [] args){
GUI g = new GUI();
g.Start();
}
public void Start(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(640, 640);
JPanel panel = new JPanel();
JPanel west = new JPanel();
JLabel name = new JLabel("Name: ");
JLabel age = new JLabel("Age: ");
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
west.setLayout(new BoxLayout(west, BoxLayout.Y_AXIS));
west.add(name);
west.add(age);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(field1);
panel.add(field2);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.WEST, west);
frame.setVisible(true);
}
}
回答by MadProgrammer
The combination of BoxLayout
and BorderLayout
are working against you, consider using something like GridBagLayout
instead.
的组合BoxLayout
和BorderLayout
正在攻击你,可以考虑使用类似GridBagLayout
代替。
Have a look at How to Use GridBagLayoutfor more details
查看如何使用 GridBagLayout了解更多详细信息
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LayoutTest {
public static void main(String[] args) {
new LayoutTest();
}
public LayoutTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JPanel form = new JPanel(new GridBagLayout());
JLabel name = new JLabel("Name: ");
JLabel age = new JLabel("Age: ");
JTextField field1 = new JTextField(10);
JTextField field2 = new JTextField(3);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
form.add(name, gbc);
gbc.gridy++;
form.add(age, gbc);
gbc.gridx++;
gbc.gridy = 0;
form.add(field1, gbc);
gbc.gridy++;
form.add(field2, gbc);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(form);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
回答by Kristoff
Not sure which IDE you're working in, but I do know that with Eclipse, WindowBuilder Pro helps a lot with things like this.
不确定您在哪个 IDE 中工作,但我知道使用 Eclipse,WindowBuilder Pro 对此类事情有很大帮助。
What you're looking for is setBounds(x, y, width, height). Use this on your objects of the GUI.
您正在寻找的是 setBounds(x, y, width, height)。在您的 GUI 对象上使用它。
x, y are the coordinates for the origin of the object (0,0) relative to the window we're working in. Origin is also in the top left of the window, and the further you go away from it in the x/y directions, the number increases. A little different from a typical graph in school.
x, y 是对象 (0,0) 原点相对于我们正在工作的窗口的坐标。原点也在窗口的左上角,在 x/ 中离它越远y 方向,数量增加。与学校的典型图表略有不同。
Here's a piece of code from one of my GUI's to act as an example.
这是我的一个 GUI 中的一段代码作为示例。
final TextField textField_1 = new TextField();
textField_1.setBounds(10, 231, 370, 22);
panel.add(textField_1);