gpt4 book ai didi

java - 无法调整 JTextFields 的大小

转载 作者:行者123 更新时间:2023-11-29 10:14:18 26 4
gpt4 key购买 nike

我需要编写一个将 char 值转换为 ascii 值的小程序。现在我只是想使小程序的布局正确,但由于某种原因,我的 JTextFields 显示得很奇怪,我不知道如何修复它。一个非常大,另一个非常小。

我在让 GridLayout 按我想要的方式工作时遇到了一些麻烦,我觉得问题与此有关,但我不确定。

Here's a screenshot of the applet

代码如下:

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class Ex1_2 extends JApplet implements ActionListener
{
private JTextField charInput;
private JLabel display1;
private JLabel dummy;
private JLabel display2;
private JLabel welcome;
private JTextField ascii;
private JLabel error;

Container pane = getContentPane();

public void init()
{
pane.setLayout(new BorderLayout());
pane.setBackground(Color.orange);

display1 = new JLabel("Character", SwingConstants.CENTER);
display2 = new JLabel("ASCII code", SwingConstants.CENTER);
dummy = new JLabel("", SwingConstants.CENTER);
dummy.setBackground(Color.orange);

welcome = new JLabel("Char <-> ASCII converter", SwingConstants.CENTER);
welcome.setForeground(Color.blue);
pane.add(welcome, BorderLayout.NORTH);
pane.add(dummy, BorderLayout.SOUTH);
//charInput.addActionListener(this);

add(addMiddle());
}

JPanel addMiddle()
{
JPanel p = new JPanel();
p.setLayout(new GridLayout(0, 2, 10, -5));
p.setBackground(Color.green);

p.add(display1, BorderLayout.SOUTH);
p.add(display2, BorderLayout.SOUTH);
p.add(dummy);

p.add(addInnerLeft());
p.add(addInnerRight());
p.add(dummy);

return p;
}

JPanel addInnerLeft()
{
JPanel p2 = new JPanel(new GridLayout(0, 2));
p2.setBackground(Color.yellow);
pane.add(p2);

charInput = new JTextField("");
charInput.setForeground(Color.black);
//p2.add(charInput);

return p2;
}

JPanel addInnerRight()
{
JPanel p3 = new JPanel();
p3.setBackground(Color.yellow);

ascii = new JTextField("");
ascii.setForeground(Color.black);
//p3.add(ascii);

return p3;
}

public void actionPerformed(ActionEvent e)
{

}
}

最佳答案

快速修复:
考虑设置 JTextFields 的列属性。例如,如果您使用:

ascii = new JTextField("", 5);

您的 ascii JTextField 现在获得了一些首选大小宽度。

更好的长期修复:
研究、学习和使用布局管理器以更好地发挥作用。

请注意,JApplets 的一个问题是大小是由 HTML 代码指定的,而不是由布局管理器指定的(据我所知),因此您必须注意确保 GUI 的大小足够大.

例如,对于布局,我得到了这个:

enter image description here

关于java - 无法调整 JTextFields 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22440632/

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